I have a front-end service to download some content from the Internet.
Unfortunately, due to the error reported here , my service was killed when the recipient received the broadcast in my service, but the notification will not be killed, and I saw after entering my logcat:
I/ActivityManager(449): Killing 11073:my-package-name/u0a102 (adj 0): remove task
Is there a way to destroy the foreground notification when its parent service is killed by the OS?
Use stopForeground:
@Override public void onDestroy() { // as the bug seems to exists for android 4.4 if (android.os.Build.VERSION.SDK_INT == android.os.Build.VERSION_CODES.KITKAT) { stopForeground(true); } super.onDestroy(); }
or
public void onTaskRemoved (Intent rootIntent) { // as the bug seems to exists for android 4.4 if (android.os.Build.VERSION.SDK_INT == android.os.Build.VERSION_CODES.KITKAT) { stopForeground(true); } }
(, Handler), :
Handler
public static boolean isMyServiceRunning(Context context) { ActivityManager manager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE); for (ActivityManager.RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) { if (MyService.class.getName().equals(service.service.getClassName())) { return true; } } return false; }
.
private static final int DEALY = 10000; private Handler handler = new Handler(); ... handler.postDelayed(ensureSericeIsRunning, DELAY); ... private Runnable ensureSericeIsRunning = new Runnable() { @Override public void run() { if (!isMyServiceRunning(getActivity())){ //shut down notification } else { handler.postDelayed(ensureSericeIsRunning, DELAY); } } };
onStop onDestroy . P.S , , api 14 , START_STICKY, , , .
onStop
onDestroy
@Override public int onStartCommand(Intent intent, int flags, int startId) { return START_STICKY; }
use onDestroy(). Even the documentation says that it is not guaranteed that it is called, I tested several times, and the only cases that it does not cause are when the system kills the process, but by this moment your notification will be killed, so this is normal.
onDestroy()
End of service deletes the foreground notification.
@Override public void onDestroy() { // mycleanup first stopForeground(true); super.onDestroy(); }
use the onTaskRemoved () method service .
Source: https://habr.com/ru/post/1536405/More articles:Android: extracting an image or thumbnail from ContactsProvider for API level 10 - androidCompile F # source while working with Codedom? - .net-assemblyПолностью отключить/скрыть строку состояния на 4.4.2 - androidSearching for row indices where there are nonzero entries in sparse csc_matrix - pythonRemoving all rows from the database except the first tow lines in oracle - sqlУдалить из базы данных все выбранные строки, кроме первых двух - sqlHow to make basic remote procedure call (RPC) in Telegram? - rpcScala nested flattening arrays - collectionsFiguring out where to add punctuation to bad user content? - language-agnosticUnable to interact with elements at adorner level - c #All Articles