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