I'm developing an app that has to be active all the time, It's going to be used on a device that will be used exclusively for the app.
I've read that to not let the app be killed by the OS I need to set an ongoing notification, I also applied a partial wakelock.
The problem is the notification doesn't go away if the app is killed, I call this on the onCreate
method of my activity
notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
Intent intent = new Intent(getApplicationContext(), PlayerActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(getApplicationContext(), 8000, intent, PendingIntent.FLAG_CANCEL_CURRENT);
Notification.Builder builder = new Notification.Builder(getApplicationContext())
.setContentTitle("Title")
.setContentText("Description")
.setContentIntent(pendingIntent)
.setSmallIcon(R.mipmap.ic_playlist_play_white_36dp)
.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.app_icon))
.setOngoing(true);
notificationManager.notify(8000, builder.build());
on the onDestroy
method I call
notificationManager.cancelAll();
super.onDestroy();
The notification never gets removed, I have to uninstall the app for it to be killed, is there a way to remove it when the app is not loaded?
Edit: I'm targeting API 19