I want to set a onClickListner for a status bar Notification. How it is possible ? Please help. Now i can load a Activity by using the pending intent. I like to set a onClickListner for the notificatioin.
Regards
Parvathi
It is not possible to set an OnClickListener for a notification. Because of the way notifications are handled/displayed there is no way to guarantee that the process that created the notification will be running at the time the notification is clicked. This means that any code you wrote to provide click handling may not be running.
If you need click listener style behavior you will have to do it using the PendingIntent: set it to start a Service that runs the logic or to use an Intent that is received by a BroadcastReceiver. This will let you perform activity without requiring a UI.
Related
I have a music service playing in background. Whenever some action happens on notification i want to listen(Pending event) those actions in service for further processing. Please provide code samples if possible.
You can start service on notification click by two ways :
1) First Way : You can call broadcast receiver when user clicks on notification.
For this you need to use PendingIntent.getBroadcast instead of
getActivity().
Reference : How to Start one Activity and two service on notification click
2) Second Way : You can directly start service when user clicks on notification.
For this you need to use PendingIntent.getService instead of
getActivity().
Reference : Start Service from Notification
Basicly I want to do something after the user clear my notification from the top bar, How do I do that in andorid programmatically?
Basic example or complete code would be nice thanks.
When you create a Notification,setDeleteIntent ,Here is the api:
setDeleteIntent(PendingIntent intent)
Supply a PendingIntent to send when the notification is cleared explicitly by the user.
The google play music app has a little 'X' in the top right corner, instead of displaying the time and a miniature notification icon in that location. Tapping the button kills the player and closes the notification. Great!
I'd like to do the same with my app - Is this only possible by inflating remote views and associating a custom button with an intent to destroy the service, or is there some notification builder methods that I'm overlooking which do that job?
There are no notification builder methods which achieve this. The only solution is to create a custom button with a pending intent to perform the action required.
I would like to animate the icon of the app without having to cancel the notification and create a new one (because in this way the icon doesn't stay in the same position of the notification bar but could move to first place if there are other notification running).
I'm able to get this with normal notification, but I would like to get the same behaviour when I use startForeground in my service. This method launches a new notification which can't be removed unless you remove service from foreground using stopForeground.
Is this possible to do? How?
Use the same notification ID for the startForeground method and the Notification object.
I've been searching for this for a while and keep coming up short. However I have setup notifications before where selecting it launches an activity, now I'm trying to mod that code.
This time I'm looking for a solution to run just a single line of code when the notification is selected. I want it to set a button from invisible to visible. So when my notification is cleared I want the code below to take place, not launch an activity or intent:
butNX.setVisibility(0);
Any input on the matter? thanks in advance.
I wasn't try this, but i think it will resolve your problem.
1. Set up a Receiver where you need it.
2. Create an Intent and add the extras to it something like this: intent.putExtra("MODE","VIS");.
3. Craete a PendingIntent which will broadcast your intent to your receiver.
PendingIntent pIntent=PendingIntent.getBroadcast(yourContext,yourReqCode,
yourIntent,yourFlags);
4.Set notification
So while receiver will receive your broadcast it should to check what to do with this intent by data which you send...