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

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

问题描述

如何在应用程序退出时执行一些代码?我想删除应用程序退出时的临时数据.通过应用程序退出,我的意思是应用程序没有在后台最小化并且完全消失了.

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.

我不能在 OnDestroy() 中做到这一点.

I can't do this in OnDestroy().

服务代码:

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

例如,我曾经有一个正在运行服务的应用程序.当我杀死应用程序时 - 服务也消失了,但我希望它保持活力.通过使用 onTaskRemoved 我能够安排服务的重启:

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

效果是 -> 我正在杀死我的应用程序 -> 服务看到正在删除任务并调用 onTaskRemoved -> 我计划在 1 秒内重新启动服务 -> 服务终止 -> 一秒后它唤醒 -> 结果:应用程序被杀死,我执行了重新启动我的服务的代码,因此它仍在后台运行(仅在首选项中可见 -> 作为进程的应用程序)

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/服务.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天全站免登陆