简体   繁体   English

X时间后杀死android应用程序

[英]Kill android application after X time

I have been searching around for a way to kill my application after the user put it in the background for at least 30min. 在用户将其放在后台至少30分钟后,我一直在寻找一种杀死我的应用程序的方法。

So far what i have found things like : 到目前为止,我发现了以下内容:

getActivity().finish();
Process.killProcess(Process.myPid());
System.exit(1);

private void stopServices() {        
    final ActivityManager activityManager = SystemServices.getActivityManager(context);
    final List<ActivityManager.RunningServiceInfo> runningServices = activityManager.getRunningServices(Integer.MAX_VALUE);
    final int pid = Process.myPid();
    for (ActivityManager.RunningServiceInfo serviceInfo : runningServices) {
        if (serviceInfo.pid == pid && !SenderService.class.getName().equals(serviceInfo.service.getClassName())) {
            try {
                final Intent intent = new Intent();
                intent.setComponent(serviceInfo.service);
                context.stopService(intent);
            } catch (SecurityException e) { 
                 // handle exception
            }
        }
    }
}

Pretty much what i have figered so far is taht after API 21 use 到目前为止,我所了解的几乎是API 21使用后的结果

finishAndRemoveTask();

before that use this.finishAffinity(); 在此之前使用this.finishAffinity();

But how to start timer once the app is put in the background for X time and then call the app kill method. 但是,一旦应用程序在background放置X次后如何启动计时器,然后调用应用程序kill方法。

So what I understand that you need 2 things: 因此,据我了解,您需要两件事:

  1. Figure out when your app is in the background 找出您的应用何时在后台
  2. Schedule a job. 安排工作。

So here are 2 links for this questions: 因此,这里有2个有关此问题的链接:

  1. How to detect if your app is in background 如何检测您的应用程序是否在后台
  2. Schedule a job on Android 在Android上安排工作

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM