简体   繁体   中英

How to remove notification when application is killed by the system?

I am developing an Android application that notifies a user when data is uploaded to the internet from one of my activities. When the user closes the application through the menu I have implemented, I execute code to remove the notifications I have placed:

notificationManager.cancelAll();

The problem is the user can kill my application through the multitasking menu (the more likely way to quit) and the code is not executed.

I thought over riding the onDestroy method would solve my problem:

    @Override
public void onDestroy()
{
    super.onDestroy();
    notificationManager.cancelAll();
}

however, this method does not seem to be called every time the application is quit from the multi-tasking menu.

Is there a way to remove my notifications before the system kills my process?

Thank You!

Unfortunately, I think better way to handle this doesn't exist. System should invoke method onDestroy() , but you have no guarantee it does.

onDestroy Android Reference

There are situations where the system will simply kill the activity's hosting process without calling this method (or any others) in it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM