0

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?

Community
  • 1
  • 1
progNewbie
  • 4,362
  • 9
  • 48
  • 107

1 Answers1

2

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.

Jerry101
  • 12,157
  • 5
  • 44
  • 63
  • i tried to cancel the notification when the activity is in onResume(). But my App always crashes, or if i do it in "try" it doesn't cancel the notification. Any ideas? – progNewbie Oct 22 '14 at 17:03
  • @prognewfag If cancel() throws an exception, that tells you why it couldn't cancel the notification. Pay attention to that exception and its stack trace. Read it carefully. If the stack trace says the top exception was caused by another exception, look at that one. If you're still puzzled, post the stack trace here and give other key excerpts from your code. – Jerry101 Oct 22 '14 at 20:02
  • @progNewFag Getting beyond the scope of the SO question, but in case you don't know, watch the logcat output to see what's going on while your program runs, including exception stack trace backs and debug logging info. Also, an exception indicates why a call failed. A try-catch block allows your program to keep running, perhaps totally ignoring the exception, but doesn't cause the failure. If any of this is news, seek out a book or class in Android + Java programming. – Jerry101 Oct 22 '14 at 20:52
  • Thank you! See this: http://stackoverflow.com/questions/26531773/app-crashes-onresume-when-canceling-notification – progNewbie Oct 23 '14 at 15:33