I tried the code form the answer of this question: How to put media controller button on notification bar?
By calling the
showNotification()
method my app gets closed. How to prevent this? And how can i handle that this method is just called if the mobile phone api is >= 16. Because i think it is just available since api 16.
I've read, that there is a solution for lower API:
import android.support.v4.app.NotificationCompat;
But i didn't got it working, so i just wanted to prevent calling it.
And can i delete the notification from the bar, by onDestroy() of my app?
The code on that page is for the most difficult case using RemoteViews
, and it looks dubious anyway. (E.g. it creates a subclass of Notification
with a constructor that creates another Notification
.)
The normal approach is to use a NotificationCompat.Builder
to build your notification, and NotificationManager
or NotificationManagerCompat
to show and cancel it. See the Notifications API Guide for details and example code.
Also see the Notifying the User documentation and the Notifications design guide.
Generally, your app should only show a notification when its activity is not visible. When the user taps on the notification, it should usually open the activity which should in turn cancel the notification.