I want to use three actions buttons in a notification to control a media service and am having difficultly finding a working example of the code: Vogella has a tutorial but his uses a single button and opens an activity not a service. I have found several other tutorials and examples of this kind but none with three working action buttons that have similar tutorials that demonstrate the construction of intents for services and multiple actions, as well most current examples of action button usage on stackoverflow are either applicable to lower versions of android, use single intent buttons, ...
here is the code i am thinking of using to set up the notification:
//individual intents for each action???
Intent Back = new Intent(this, MusicService.class );
Back.putExtras("back", 1);
Back.putExtras("play", 0);
Back.putExtras("next", 0);
Intent Play = new Intent (this, MusicService.class);
Back.putExtras("back", 0);
Back.putExtras("play", 1);
Back.putExtras("next", 0);
Intent Next = new Intent (this, MusicService.class);
Next.putExtras("back", 0);
Next.putExtras("play", 0);
Next.putExtras("next", 1);
//do I need to make three pending intents????
PendingIntent pIntentback = PendingIntent.getService(this, 0, Back, 0);
PendingIntent pIntentplay = PendingIntent.getService(this, 0, Play, 0)
PendingIntent pIntentnext = PendingIntent.getService(this, 0, Next, 0)
Notification noti = new Notification.Builder(this)
.setContentTitle("Juke Box Hero")
.setContentText("Playing ")
.setSmallIcon(R.drawable.ic_launcher)
.addAction(R.drawable.back, "", pIntentback)
.addAction(R.drawable.play, "", pIntentplay)
.addAction(R.drawable.next, "", pIntentnext)
.build();
NotificationManager notificationManager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
noti.flags = Notification.FLAG_AUTO_CANCEL;
notificationManager.notify(0, noti);
I have several questions here...
1) is the above code how i should go about setting up the action buttons, intents, and pending intents?
2) since I want to recieve the intents wrapped by the pending intents in a service, am I correct in calling getService, instead of getActivity?
3) if I am able to recieve intents directly in the MusicService class, how can I receive the intents in the service without restarting the service (thus losing data stored in memory already)?
4) or do I need to build a receiver class to extend the BroadcastReceiver class and register that in the manifest and then send the corresponding intent to the MusicService class?
please understand that i am more than willing to research and write the code--and have been at that for several days before asking, but i am in need of straightforward answers to these questions...
help and direction are much appreciated