2015-06-22 83 views
2

我想使用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()); 
} 
+0

是否有你沒有使用[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

回答

4

一個特點是NotificationCompat.MediaStyle - 這可讓您建立媒體的通知,並通過媒體信息Android 5.0以上版本的設備向後兼容的方式,沿着MediaSessionCompat工作提供信息到Android穿在老設備(以及在API14-19設備上添加鎖屏控制)。

當您構建MediaStyle通知時,請致電setMediaSession(),告知Android Wear您的通知已附加到媒體播放,併爲您提供針對單一卡片式通知的4動作。

+0

請檢查我更新的問題。我嘗試了你的建議,然後3個動作顯示在Android Wear的3個不同頁面上。 –

+0

Android Wear會從您設置的[PlaybackStateCompat](https://developer.android.com/reference/android/support/v4/media/session/PlaybackStateCompat.html)中拉取播放/暫停/上一個/下一個按鈕你的'MediaSessionCompat'。 – ianhanniballake

+0

@ianhanniballake - 你能幫助我如何隱藏穿戴通知。我不希望Android穿着從MediaSessionCompat中拉取播放狀態。我有一個由穿戴應用程序觸發的自定義通知。我不使用'MediaStyle'通知 – Libin