How to detect the output of an application on Android?

How to execute some code when exiting the application? I want to delete temporary data when exiting the application. At the end of the application, I mean that the application does not work with a minimal background, and it completely disappeared.

I tried to make a service that runs in a separate process, the service checks if the application process is working, it must delete the temporary folder. With this approach, the temporary folder is not always deleted, because the process still works with the lowest priority.

I can not 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; } } 
+6
android xamarin xamarin.android
source share
2 answers

I don’t know if this help will help, but if you kill your application, then the service that works in the background calls method:

 @Override public void onTaskRemoved(Intent rootIntent){ super.onTaskRemoved(rootIntent); } 

For example, I had an application that launched a service. When I killed the application, I also died, but I wanted it to stay alive. Using onTaskRemoved, I was able to schedule a service restart:

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

The effect was β†’ I kill my application β†’ The service sees that the tasks are deleted and calls onTaskRemoved β†’ I plan to restart the service in 1 second β†’ Utility dies β†’ After one second it wakes up β†’ RESULT: The application is killed, I executed the code that restarted my service, therefore, it still works in the background (visible only in the settings β†’ application as a 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 deleted the task that comes from the service application.

+12
source share

My experience shows that most applications really don't need the exit callback you describe.

The Android method, which usually works fine, relates to the component (Activity, Service ..) of the life cycle management.

In some applications, the "Exit" function is provided (via a button, menu, etc.), which, when activated by the user, the application allows you to close all open components and basically go down.

But, if exit-callback is really what you want, the closest to it is probably to create a specialized service without any logic other than the onDestroy () function and activate it when the application starts, without closing it!

 class ExitListenerService extends Service { @Override public void onDestroy() { super.onDestroy(); // App exit logic here. Not deterministic! } } 

Most likely, such a service is likely to be the last component that will be fixed by the operating system. This should work in most cases, it works fine for me. But this is not 100% guaranteed.

But .. if you should have a bulletproof solution, the only other way I know is to create a peer-to-peer application, call it a watchdog that will wake up periodically to check the weather or not to use the main application, it still works and if it doesn't work activates the exit logic.

To complete this last check, you will need to call

  List<RunningAppProcessInfo> runningApps = activityManager.getRunningAppProcesses(); 

and iterating over runApps that are looking for your own.

+1
source share

All Articles