0

I am working on a service that will check whether the app has idle (at the background) for certain time, and kill the app if it is exceed that given time. Also , if the user has bring back the activity, then it will reset the timer

The problem is, if there are few activities in my app , how can I achieve it ? and I found some similiar code , but how to adjust it to fit my case? Thanks.

The sample code:

timeout class and its service

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

    }

}



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

}

kill app

android.os.Process.killProcess(android.os.Process.myPid());
user782104
  • 13,233
  • 55
  • 172
  • 312
  • Just curious : why do you think you need to manage the background state of your app ? Android does that automatically ... – 2Dee Feb 26 '14 at 09:55
  • It seems the app never get killed if it is at idle – user782104 Feb 26 '14 at 10:23
  • Then the system probably doesn't need the memory... Why would you want to kill the app instead of leveraging the ability of the system to recover your app's state when the user switches back to it ? – 2Dee Feb 26 '14 at 10:25
  • Since I would like the app to update data, it is a news app – user782104 Feb 26 '14 at 10:51
  • If I understand you correctly, you want to refresh the data in your app every time it is brought back to the foreground ? If so, using the proper lifecycle callbacks of your Activities is probably a better option. – 2Dee Feb 26 '14 at 10:53

1 Answers1

1

You could use handler.postDelayed(runnable, time) and when You bring back Your activity You could call handler.removeCallbacks(runnable); to cancel postDelayed

pepe-pa
  • 552
  • 1
  • 5
  • 13