检索后超时警告通知活动通知

2023-09-04 13:31:34 作者:∝ 画眉如黛〃

我想实现超时通知。这是我想做的事情。如果应用程序转到后台的心情,5分钟后它应该警告该应用程序将注销用户。此通知后,如果用户打开应用程序,就应该要求输入密码,如果用户输入正确的通行,应该检索画面,他离开5分钟前。我的问题是,我不能保存用户离开,以便检索后,他们输入密码的活动。这是我到目前为止,完成的: 我添加了一个倒计时的onSaveInstanceState()方法里,创造目前意向的一个实例。然后在TimeOutActivity予以传递当前活动中该实例定义了一个名为savedActivity属性

I'm trying to implement time out notification. Here is what I want to do. If app goes to the background mood, after 5 minutes it should warn the user that app will be signed out. After this notification, if user opens the app, it should ask for the password, if user inputs correct pass, it should retrieve the screen that he left 5 mins ago. My problem is that I cannot save the activity that user left in order to retrieve after they input password. Here is what I've done so far: I added a countdown inside of onSaveInstanceState() method, and create an instance of current intent. Then in TimeOutActivity I defined a property called savedActivity in order to pass the current activity in that instance.

private boolean isTimedOut;
private TimeOutActivity timOutActivity;
private CountDownTimer timer;

@Override
protected void onSaveInstanceState(Bundle state) 
{
    super.onSaveInstanceState(state);
    if (timOutActivity == null)
    {
        timOutActivity = new TimeOutActivity();
        timOutActivity.setIntent(new Intent(getApplicationContext(), TimeOutActivity.class));
        **timOutActivity.savedActivity = this;**
        initializeTimer();
    }
}
private void initializeTimer()
{
    timer = new CountDownTimer(60000, 1000)
    {        
        public void onTick(long millisUntilFinished)
        {
            if (millisUntilFinished / 1000 == 55)
            {
                showSignOutWarning();
                isTimedOut = true;
            }
        }

        public void onFinish()
        {
            // Push local notification
            isSignedOut = true;
        }
    }.start();
}

然后在ONSTART()方法,我加了以下code:

Then In onStart() method I added following code:

@Override
public void onStart()
{
    super.onStart();
    if (isTimedOut)
    {
        // Load timedOut activity
        startActivity(timOutActivity.getIntent());
        isTimedOut = false;
    }
}

但是,当我想找回TimeOutActivity内的活动,我的财产(savedActivity)为空。我究竟做错了什么?有没有更好的方法来保存当前的活动?

But when I want to retrieve the activity inside of TimeOutActivity, my property (savedActivity) is null. What am I doing wrong? Is there a better way to save the current activity?

更新2014年10月31日 我定义一个类:App.java扩展应用。此类我可以定义他们的setter方法​​和getter全局变量。这就像波纹管

Update Oct 31 2014 I defined a class called: App.java that extends Application. By this class I can define global variables with their setters and getter. It's like bellow

public class NGVApp extends Application
{
    private WebView webView;

    public WebView getWebView()
    {
        return webView;
    }

    public void setWebView(WebView webView)
    {
        this.webView = webView;
    }
}

然后在AndroidManifest.xml中的应用标签我说这

Then in AndroidManifest.xml in Application tag I added this

android:name="your.app.bundle.name.App"

然后在我的WebViewActivity的onStop()方法,我设置web视图实例像波纹管:

Then in my WebViewActivity onStop() method, I set the webView instance like bellow:

((App) this.getApplication()).setWebView(webView); 

最后,在的onCreate()方法,我说:

And finally in onCreate() method I added:

@Override
    protected void onCreate(Bundle savedInstanceState)
    {
        if(((NGVApp) this.getApplication()).getSavedInstanceBundle() != null)
        {
            savedInstanceState = ((NGVApp) this.getApplication()).getSavedInstanceBundle();
        }
}

我能顺利拿到web视图的实例,它不是空了,但它不包含web视图内容(即用户之前超时互动的)任何想法?

I could successfully get the instance of webView and it's not null anymore but it's not containing the webView contents (The one that user interacted before time-out) any idea?

推荐答案

最后,我想通了该解决方案。有没有办法挽救的WebView的状态,所有的内容,所以我们需要prevent重装或重新创建web视图。为了让我们的活动的一个实​​例包含web视图,我们应该增加以下code到我们的清单文件:

Finally I figured out the solution. There is no way to save the state of WebView and have all of the content, so we need to prevent reloading or recreating the webView. In order to have one instance of our activity that contains webView we should add the following code to our Manifest file:

<activity ...
        android:launchMode="singleInstance"
        android:alwaysRetainTaskState="true">

那么,如果通过你的应用程序,你需要完全重新创建实例刷新web视图,您可以使用结束();如果它是活动的内部或者如果你是在另一个活动,并要重新创建你的web视图的活动,阅读的这个链接做到这一点。