Application doesn't have onDestroy method. It has onTerminate but it's never called =(
Here is my solution. I have MainActivity in my app which for sure have to be active if app working. So in broadcast receiver I always check if MainActiviy is running.
public void onReceive(final Context context, final Intent intent){
if(isAppRunning(context)){
// Do my handling
}
else{
// You can disable receiver here
Log.w(LOGTAG, "App not running. Ignore " + LOGTAG + " call.");
}
}
protected boolean isAppRunning(Context context){
String activity = MainActivity.class.getName();
ActivityManager activityManager = (ActivityManager)context.
getSystemService(Context.ACTIVITY_SERVICE);
List<RunningTaskInfo> tasks = activityManager.
getRunningTasks(Integer.MAX_VALUE);
for(RunningTaskInfo task : tasks){
if(activity.equals(task.baseActivity.getClassName())){
return true;
}
}
return false;
}