问题
I would like to make an android wear notification using MediaSession that has several buttons on one page and some other buttons on a different page. It would look like Google Play Now App notification on an android wear. I followed this github tutorial at https://github.com/PaulTR/AndroidDemoProjects/blob/master/MediaSessionwithMediaStyleNotification/app/src/main/java/com/ptrprograms/mediasessionwithmediastylenotification/MediaPlayerService.java
However, each action was added to a separate page on the android wear. I want to group some of them into one page. For instance, the play/pause, pre and next on one page and the rate button on a second page. I would like to know if it is possible to achieve that with a custom notification without using MediaSession in order to cover API less than 21.
Thanks!
private void buildNotification( Notification.Action action ) {
Notification.MediaStyle style = new Notification.MediaStyle();
Intent intent = new Intent( getApplicationContext(), MediaPlayerService.class );
intent.setAction( ACTION_STOP );
PendingIntent pendingIntent = PendingIntent.getService(getApplicationContext(), 1, intent, 0);
Notification.Builder builder = new Notification.Builder( this )
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle( "Media Title" )
.setContentText( "Media Artist" )
.setDeleteIntent( pendingIntent )
.setStyle(style);
builder.addAction( generateAction( android.R.drawable.ic_media_previous, "Previous", ACTION_PREVIOUS ) );
builder.addAction( generateAction( android.R.drawable.ic_media_rew, "Rewind", ACTION_REWIND ) );
builder.addAction( action );
builder.addAction( generateAction( android.R.drawable.ic_media_ff, "Fast Foward", ACTION_FAST_FORWARD ) );
builder.addAction( generateAction( android.R.drawable.ic_media_next, "Next", ACTION_NEXT ) );
style.setShowActionsInCompactView(0,1,2,3,4);
NotificationManager notificationManager = (NotificationManager) getSystemService( Context.NOTIFICATION_SERVICE );
notificationManager.notify( 1, builder.build() );
}
UPDATED CODE: The the setMediaSession is giving this compiling error when I pass it a token: The method setMediaSession(MediaSessionCompat.Token) in the type NotificationCompat.MediaStyle is not applicable for the arguments (MediaSession.Token). The 3 actions are still being displayed on 3 separate pages on the android wear.
private void buildNotification( Notification.Action action ) {
NotificationCompat.MediaStyle style = new NotificationCompat.MediaStyle();
//style.setMediaSession(mSession.getSessionToken());
style.setMediaSession(null);
style.setShowActionsInCompactView(1,2);
Bitmap icon = BitmapFactory.decodeResource(this.getResources(), R.drawable.pinkfloyd);
NotificationCompat.Builder builder = new NotificationCompat.Builder( this );
builder.setSmallIcon(R.drawable.ic_launcher);
builder.setLargeIcon(icon);
builder.setContentTitle( "Media Title" );
builder.setContentText( "Media Artist" );
builder.setColor(Color.argb(0, 60, 13, 77));
builder.setStyle(style);
builder.addAction(R.drawable.ic_launcher,
"Test1 ", null);
builder.addAction(R.drawable.ic_launcher,
"Test2 ", null);
builder.addAction(R.drawable.ic_launcher,
"Test3 ", null);
NotificationManager notificationManager = (NotificationManager) getSystemService( Context.NOTIFICATION_SERVICE );
notificationManager.notify( 1, builder.build() );
}
回答1:
One feature added in AppCompat v22.2.0 is NotificationCompat.MediaStyle - this gives you a backward compatible way to build media notifications and pass media information to Android 5.0+ devices, working alongside MediaSessionCompat to provide information to Android Wear on older devices (as well as adding lockscreen controls on API14-19 devices).
When you build a MediaStyle notification, it is critical to call setMediaSession() which tells Android Wear that your notification is attached to media playback, giving you the 4 action on a single card style notification.
来源:https://stackoverflow.com/questions/30988523/android-wear-notification-with-mediasession