how to add view to notification?
I want to add one icon to the notification bar for mediaplayer. Now on dragging 开发者_JAVA百科the notification bar I want to add view like four buttons.
How can I do that?This functionality is in default player of android. When a user exits the screen, the player keeps playing in the background and when we drag the notification there is functionality to play, pause, next and previous.
This can be done by using RemoteView. This article on developer.android.com explains it quite good: http://developer.android.com/guide/topics/ui/notifiers/notifications.html
Basically you create your RemoteView by retrieving the layout you've defined in the layout folder of your project and set it as the ContentView of your notifiation. Here's an excerpt from the site:
RemoteViews contentView = new RemoteViews(getPackageName(), R.layout.custom_notification_layout);
contentView.setImageViewResource(R.id.image, R.drawable.notification_image);
contentView.setTextViewText(R.id.text, "Hello, this message is in a custom expanded view");
notification.contentView = contentView;
Note, the MediaController may come in quite handy in your situation. Maybe you can add it to this custom view directly. I've never tried it though.
Update
As you mentioned, the click could be a problem because the notification container rather than its button reacts on the user click. A possible solution could be one similar on how Google handles clicks on single items on their Contact list, where you can click on the phone symbol on the very right of the list item to directly start a phone call, rather than to open the contact detail.
Take a look at the according source code here:
http://www.google.com/codesearch/p?hl=en#J8HqCFe1rOo/src/com/android/contacts/ContactListItemView.java&q=Android%20ContactList&sa=N&cd=4&ct=rc
You see a DontPressWithParentImageView
element there. You could take a look at how that works and implement your buttons like this. That may work.
精彩评论