如何检测应用程序退出android上? [英] How to detect application exit on android?

查看:362
本文介绍了如何检测应用程序退出android上?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何执行的应用程序退出一些code?我想删除应用程序退出临时数据。通过应用程序退出我的意思是应用程序没有运行最小化背景和完全消失了。

我试图让运行在单独的进程中,该服务是检查,如果应用程序没有运行,应该删除临时文件夹服务。使用这种方法的临时文件夹并不总是被删除,因为进程仍然具有最低优先级运行。

我不能的OnDestroy这样做()。

服务code:

  [服务(过程=com.lobomonitor)
[IntentFilter的(新的String [] {com.androidgui.ProcessMonitorService})]
公共类ProcessMonitorService:服务
{
    公众覆盖StartCommandResult OnStartCommand(意向意图,StartCommandFlags标志,INT startId)
    {
        螺纹processThread =新主题(()=>
                                          {
                                              ActivityManager actManager =(ActivityManager)this.GetSystemService(ActivityService);
                                              而(真)
                                              {
                                                  Thread.sleep代码(1000);
                                                  IList的< ActivityManager.RunningAppProcessInfo> procList = actManager.RunningAppProcesses;
                                                  procList.Any(ⅰ=> i.ProcessName.Equals(com.androidgui));
                                                  如果(procList.Any(ⅰ=>!i.ProcessName.Equals(com.androidgui)))
                                                  {
                                                          FolderManager.Singleton.DeleteTempFolder();
                                                          打破;
                                                  }
                                              }
                                          });
        processThread.Start();
        返回StartCommandResult.RedeliverIntent;
    }

    公众覆盖的IBinder OnBind(意向意图)
    {
        返回null;
    }
}
 

解决方案

我不知道这是否帮助,但如果你杀了你的应用程序,然后运行在后台的服务调用方式:

  @覆盖
公共无效onTaskRemoved(意向rootIntent){

    super.onTaskRemoved(rootIntent);
}
 

例如我曾经应用程序,正在运行的服务。当我杀的应用程序 - 服务也死了,但我希望它维持生命。通过使用onTaskRemoved我能安排服务的重新启动:

  @覆盖
公共无效onTaskRemoved(意向rootIntent){
    意图restartServiceIntent =新的意图(getApplicationContext(),this.getClass());
    restartServiceIntent.setPackage(getPackageName());

    PendingIntent restartServicePendingIntent = PendingIntent.getService(getApplicationContext(),1,restartServiceIntent,PendingIntent.FLAG_ONE_SHOT);
    AlarmManager alarmService =(AlarmManager)getApplicationContext()getSystemService(Context.ALARM_SERVICE)。
    alarmService.set(
            AlarmManager.ELAPSED_REALTIME,
            SystemClock.elapsedRealtime()+ 1000,
            restartServicePendingIntent);

    super.onTaskRemoved(rootIntent);
}
 

效果是 - >我杀死我的应用程序 - >服务发现任务被删除,并呼吁onTaskRemoved - >我调度服务的启动时间为1秒 - >服务模具 - >一秒后,它唤醒 - >结果:应用程序被杀害,我执行code,它重新启动我的服务,因此它仍然是在后台运行(只在preferences可见 - >应用程序,进程)

http://developer.android.com/reference/android/app/ Service.html

 无效onTaskRemoved(意向rootIntent)
 

此被称为如果服务正在运行,并且用户已经除去来自该服务的应用的任务。

How to execute some code on application exit? I want to delete temp data on application exit. By application exit i mean that app is not running minimized in background and its totally gone.

I tried to make service that runs in separate process, the service is checking if app process is not running it should delete temp folder. With this approach temp folder is not always deleted because process is still running with the lowest priority.

I can't do this in OnDestroy().

Service code:

[Service(Process = "com.lobomonitor")]
[IntentFilter(new string[] { "com.androidgui.ProcessMonitorService" })]
public class ProcessMonitorService : Service
{
    public override StartCommandResult OnStartCommand(Intent intent, StartCommandFlags flags, int startId)
    {
        Thread processThread = new Thread(() =>
                                          {
                                              ActivityManager actManager = (ActivityManager) this.GetSystemService(ActivityService);
                                              while (true)
                                              {
                                                  Thread.Sleep(1000);
                                                  IList<ActivityManager.RunningAppProcessInfo> procList = actManager.RunningAppProcesses;
                                                  procList.Any(i => i.ProcessName.Equals("com.androidgui"));
                                                  if (!procList.Any(i => i.ProcessName.Equals("com.androidgui")))
                                                  {
                                                          FolderManager.Singleton.DeleteTempFolder();
                                                          break;
                                                  }
                                              }
                                          });
        processThread.Start();
        return StartCommandResult.RedeliverIntent;
    }

    public override IBinder OnBind(Intent intent)
    {
        return null;
    }
}

解决方案

I don't know if that help, but if you kill your app, then service that runs in background calls method:

@Override
public void onTaskRemoved(Intent rootIntent){

    super.onTaskRemoved(rootIntent);
}

For example I had once app that was running service. When I killed app - service died too, but I wanted it to stay alive. By using onTaskRemoved I was able to schedule restart of service:

@Override
public void onTaskRemoved(Intent rootIntent){
    Intent restartServiceIntent = new Intent(getApplicationContext(), this.getClass());
    restartServiceIntent.setPackage(getPackageName());

    PendingIntent restartServicePendingIntent = PendingIntent.getService(getApplicationContext(), 1, restartServiceIntent, PendingIntent.FLAG_ONE_SHOT);
    AlarmManager alarmService = (AlarmManager) getApplicationContext().getSystemService(Context.ALARM_SERVICE);
    alarmService.set(
            AlarmManager.ELAPSED_REALTIME,
            SystemClock.elapsedRealtime() + 1000,
            restartServicePendingIntent);

    super.onTaskRemoved(rootIntent);
}

Effect was -> I am killing my app -> Service see that tasks are being removed and calls onTaskRemoved -> I am scheduling restart of service in 1 sec -> Service dies -> After one sec it wakes up -> RESULT: App is killed, i executed code that restarted my service so it is still running in background (visible only in preferences -> applications as process)

http://developer.android.com/reference/android/app/Service.html

void     onTaskRemoved(Intent rootIntent)

This is called if the service is currently running and the user has removed a task that comes from the service's application.

这篇关于如何检测应用程序退出android上?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆