【问题标题】:A timer that will kill android app after idle for certain time?空闲一段时间后会杀死android应用程序的计时器?
【发布时间】:2014-03-29 00:53:49
【问题描述】:

我正在开发一项服务,该服务将检查应用程序是否空闲(在后台)一段时间,如果超过给定时间则终止应用程序。此外,如果用户带回了活动,那么它将重置计时器

问题是,如果我的应用程序中的活动很少,我该如何实现呢?我找到了一些类似的代码,但是如何调整它以适合我的情况?谢谢。

示例代码:

超时类及其服务

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

}

杀死应用

android.os.Process.killProcess(android.os.Process.myPid());

【问题讨论】:

  • 只是好奇:为什么您认为您需要管理应用程序的后台状态? Android 会自动执行此操作...
  • 如果应用处于空闲状态,似乎永远不会被杀死
  • 那么系统可能不需要内存...为什么要杀死应用程序而不是在用户切换回应用程序时利用系统恢复应用程序状态的能力?
  • 因为我想让应用更新数据,所以它是一个新闻应用
  • 如果我对您的理解正确,您希望每次将应用程序中的数据带回前台时都刷新它?如果是这样,使用活动的适当生命周期回调可能是更好的选择。

标签: android android-intent service timeout


【解决方案1】:

你可以使用 handler.postDelayed(runnable, time) 当你带回你的活动时你可以调用 handler.removeCallbacks(runnable);取消 postDelayed

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-11-03
    • 2010-10-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-05-12
    • 2017-06-17
    • 1970-01-01
    相关资源
    最近更新 更多