I'm trying to make a notification with 2 buttons on it:
- one takes me back to the activity
- the other closes it
Has anyone got an idea on how to catch the button click event (remember that the Activity is paused)?
I'm trying to make a notification with 2 buttons on it:
Has anyone got an idea on how to catch the button click event (remember that the Activity is paused)?
I am glad to post it! After working all night I found something. So, here we go!
1. Create an xml layout file for your notification.
2. Create the notification using the Notification.Builder. After adding everything you want (icons, sounds, etc) do this:
3. Create a method setListeners. Inside this method you have to write this:
4. For my requirements I used a HelperActivity which responds to the intents. But for you I don't think it is necessary.
If you want the full source code you can browse it or download it from my git repo. The code is for personal use, so don't expect to read a gorgeous code with a lot of comments. https://github.com/BILLyTheLiTTle/AndroidProject_Shortcuts
ALL THE ABOVE, ANSWERS THE QUESTION OF CATCHING EVENT FROM DIFFERENT BUTTONS.
About canceling the notification I redirect you here
How to clear a notification in Android
Just remember to use the id you parsed at the notify method when you called the notification for fist time
As for ICS, the question is simple to reply because the required behaviour reflects default notification: you can close a notification swiping it to the right, and you can define which activity to send the user to when he presses it simply using
PendingIntent
:code taken from http://developer.android.com/guide/topics/ui/notifiers/notifications.html
If you want to assign specific intent to a button:
I suppose that you need only one intent to be sent when the button is clicked, so you have to AVOID setting the main notification intent
Otherwise (if you set "notification.contentIntent = pendingIntent;" as usual) both intents will be called which might not be what you want/user expects.
If you still want pressing other parts of the notification invoke that general intent (or any other) you can use the same method of intent-per-view assignment as above. Don't forget to set
to any view you'd like to track onClick() for.
You can track these intents by their extras in the activity they are calling. If you are calling you main/launcher activity than you'll track them here (as it comes from javadoc for this method):
You can simply add action Buttons in your
Notification
by setting action to yourNotification.Builder
and definingPendingIntent
for each actionbelow is the sample code:
There is a full example for you here
and then you can define listener method :
and here is the notification_view layout to costumize your notification .
For me, this is working greatly.. I will write down the whole example.. This is for Notification create
In Android Menifest
This is service class