Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I dealt with this by using the AlarmManager to schedule and cancel timeout action.</p> <p>Then in the onPause() event of all of my activites, I schedule the alarm. In the onResume() event of all of my activities, I check to see if the alarm goes off. If the alarm went off, I shutdown my app. If the alarm hasn't gone off yet I cancel it.</p> <p>I created Timeout.java to manage my alarms. When the alarm goes off a intent is fired:</p> <pre><code>public class Timeout { private static final int REQUEST_ID = 0; private static final long DEFAULT_TIMEOUT = 5 * 60 * 1000; // 5 minutes private static PendingIntent buildIntent(Context ctx) { Intent intent = new Intent(Intents.TIMEOUT); PendingIntent sender = PendingIntent.getBroadcast(ctx, REQUEST_ID, intent, PendingIntent.FLAG_CANCEL_CURRENT); return sender; } public static void start(Context ctx) { ctx.startService(new Intent(ctx, TimeoutService.class)); long triggerTime = System.currentTimeMillis() + DEFAULT_TIMEOUT; AlarmManager am = (AlarmManager) ctx.getSystemService(Context.ALARM_SERVICE); am.set(AlarmManager.RTC, triggerTime, buildIntent(ctx)); } public static void cancel(Context ctx) { AlarmManager am = (AlarmManager) ctx.getSystemService(Context.ALARM_SERVICE); am.cancel(buildIntent(ctx)); ctx.startService(new Intent(ctx, TimeoutService.class)); } } </code></pre> <p>Then, I created a service to capture the intent generated by the alarm. It sets some global state in my instance of the application class to indicate that the app should lock:</p> <pre><code>public class TimeoutService extends Service { private BroadcastReceiver mIntentReceiver; @Override public void onCreate() { super.onCreate(); mIntentReceiver = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { String action = intent.getAction(); if ( action.equals(Intents.TIMEOUT) ) { timeout(context); } } }; IntentFilter filter = new IntentFilter(); filter.addAction(Intents.TIMEOUT); registerReceiver(mIntentReceiver, filter); } private void timeout(Context context) { App.setShutdown(); NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); nm.cancelAll(); } @Override public void onDestroy() { super.onDestroy(); unregisterReceiver(mIntentReceiver); } public class TimeoutBinder extends Binder { public TimeoutService getService() { return TimeoutService.this; } } private final IBinder mBinder = new TimeoutBinder(); @Override public IBinder onBind(Intent intent) { return mBinder; } } </code></pre> <p>Finally, I created a subclass of Activity that all of my app's activities subclass from to manage locking and unlocking:</p> <pre><code>public class LockingActivity extends Activity { @Override protected void onPause() { super.onPause(); Timeout.start(this); } @Override protected void onResume() { super.onResume(); Timeout.cancel(this); checkShutdown(); } private void checkShutdown() { if ( App.isShutdown() ) { finish(); } } } </code></pre> <p>Using onPause and onResume to start and stop the timeout gives me the following semantics. As long as one of my application's activities is active, the timeout clock is not running. Since I used an Alarm type of AlarmManager.RTC, whenever the phone goes to sleep the timeout clock runs. If the timeout happens while the phone is asleep, then my service will pick up the timeout as soon as the phone wakes up. Additionally, the clock runs when any other activity is open.</p> <p>For a more detailed version of these, you can see how I actually implemented them in my application <a href="https://github.com/bpellin/keepassdroid" rel="nofollow noreferrer"><a href="https://github.com/bpellin/keepassdroid" rel="nofollow noreferrer">https://github.com/bpellin/keepassdroid</a></a></p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload