Retrieve an activity after Time out warning notification

送分小仙女□ 提交于 2019-12-18 07:08:39

问题


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();
}

Then In onStart() method I added following code:

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

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?

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;
    }
}

Then in AndroidManifest.xml in Application tag I added this

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

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

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

And finally in onCreate() method I added:

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

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?


回答1:


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">

Then if through your application you needed to recreate the instance entirely to refresh the webView, you can use finish(); if it's inside of the activity or if you are in another activity and you want to recreate your webView activity, read this link to do it.




回答2:


First of all, you should use the AlarmManager to trigger your notification from a Service.

Intent intent = new Intent(context, YourNotifService.class);
PendingIntent pendingService = PendingIntent.getService(context, 0, intent, 0);
AlarmManager alarm = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
long triggerTime =  SystemClock.elapsedRealtime()  + (5 * 60 * 1000);
alarm.set(AlarmManager.ELAPSED_REALTIME, triggerTime, pendingService);

About saving the complete state of your Activity, that's really up to you to go through each View and save the state. Best way would be to save it in a specific file and not in the SharedPreferences, but both could work.



来源:https://stackoverflow.com/questions/26142255/retrieve-an-activity-after-time-out-warning-notification

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!