我想使用MediaSession製作Android Wear通知,該通知在一個頁面上具有多個按鈕,並在其他頁面上具有其他按鈕。它看起來像是Android Wear上的Google Play Now App通知。我跟着這個github教程在https://github.com/PaulTR/AndroidDemoProjects/blob/master/MediaSessionwithMediaStyleNotification/app/src/main/java/com/ptrprograms/mediasessionwithmediastylenotification/MediaPlayerService.java使用MediaSession的Android Wear通知
但是,每個動作都被添加到android磨損的單獨頁面上。我想將其中一些分組到一個頁面中。例如,播放/暫停,前一頁和下一頁以及第二頁上的速率按鈕。我想知道是否有可能通過自定義通知而不使用MediaSession來覆蓋API小於21.
謝謝!
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());
}
更新的代碼:將所述setMediaSession是給當我通過它的令牌此編譯錯誤:在類型NotificationCompat.MediaStyle方法setMediaSession(MediaSessionCompat.Token)是不適用的參數(MediaSession.Token) 。這3個動作仍然在Android Wear的3個獨立頁面上顯示。在程序兼容性v22.2.0添加
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());
}
是否有你沒有使用[MediaSessionCompat](https://developer.android.com/reference/android/support/v4/media/session/MediaSessionCompat.html)取代[MediaSession](https: //developer.android.com/reference/android/media/session/MediaSession.html)?相同的API,但向後兼容。 – ianhanniballake