2015-02-06 99 views
3

我有播放收音機的服務&創建通知。通知必須包含標題,文字,時間& a按鈕(暫停/播放)。我已經添加了其他的東西&通知正在顯示。但我不確定如何將按鈕添加到通知。在搜索時,我發現我可以使用RemoteView,RemoteControlClient & MediaStyles。但RemoteControlClient & MediaStyles用於有媒體文件使用所有按鈕播放(上一個,暫停,播放,返回)。所以我很困惑。任何人都可以建議我,哪一個用於在Lollypop的通知中添加按鈕。如何添加暫停/播放按鈕來通知棒棒糖?

+1

使用遠程視圖,並按照鏈接http://www.androidbegin.com/tutorial/android-custom-notification-tutorial/ – Ramesh 2015-02-06 05:03:43

+0

它將爲棒糖工作? – 2015-02-06 05:13:11

+0

最近我們在一個具有遠程視圖的應用程序中實現了通知。它在棒棒糖中工作也很好 – Ramesh 2015-02-06 05:17:44

回答

0

首先,你需要知道addAction在23 API已被棄用,讀this到涉及

呼叫showNotification()方法來顯示通知

private void showActionButtonsNotification() { 


     Notification.Builder notif; 
     NotificationManager nm; 
     notif = new Notification.Builder(getApplicationContext()); 
     notif.setSmallIcon(R.mipmap.ic_launcher); 
     notif.setContentTitle("Hi there!"); 
     notif.setContentText("This is even more text."); 
     Uri path = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION); 
     notif.setSound(path); 
     nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); 

     Intent yesReceive = new Intent(); 
     yesReceive.setAction(MyNotificationReceiver.RESUME_ACTION); 
     PendingIntent pendingIntentYes = PendingIntent.getBroadcast(this, MyNotificationReceiver.REQUEST_CODE_NOTIFICATION, yesReceive, PendingIntent.FLAG_UPDATE_CURRENT); 
     notif.addAction(R.drawable.resume, "resume", pendingIntentYes); 


     Intent yesReceive2 = new Intent(); 
     yesReceive2.setAction(MyNotificationReceiver.STOP_ACTION); 
     PendingIntent pendingIntentYes2 = PendingIntent.getBroadcast(this, MyNotificationReceiver.REQUEST_CODE_NOTIFICATION, yesReceive2, PendingIntent.FLAG_UPDATE_CURRENT); 
     notif.addAction(R.drawable.stop, "stop", pendingIntentYes2); 




     Intent maybeReceive2 = new Intent(); 
     maybeReceive2.setAction(MyNotificationReceiver.CANCEL_ACTION); 
     PendingIntent pendingIntentMaybe2 = PendingIntent.getBroadcast(this, MyNotificationReceiver.REQUEST_CODE_NOTIFICATION, maybeReceive2, PendingIntent.FLAG_UPDATE_CURRENT); 
     notif.addAction(R.drawable.cancel, "cancel", pendingIntentMaybe2); 


     assert nm != null; 
     nm.notify(MyNotificationReceiver.REQUEST_CODE, notif.getNotification()); 


    } 

創建MyNotificationReceiver類recive的點擊

public class MyNotificationReceiver extends BroadcastReceiver { 
     public static int REQUEST_CODE_NOTIFICATION = 1212; 
     public static int REQUEST_CODE = 10; 
     public static final String RESUME_ACTION = "RESUME_ACTION"; 
     public static final String STOP_ACTION = "STOP_ACTION"; 
     public static final String CANCEL_ACTION = "CANCEL_ACTION"; 

     @Override 
     public void onReceive(Context context, Intent intent) { 
      String action = intent.getAction(); 

      Log.e("action", action); 

      if (intent.getAction() != null) { 
       switch (intent.getAction()) { 
        case RESUME_ACTION : 
         Toast.makeText(context, "resume", Toast.LENGTH_SHORT).show(); 
// you resume action 
         break; 
        case STOP_ACTION : 
         Toast.makeText(context, "stop", Toast.LENGTH_SHORT).show(); 
// you stop action 
         break; 
        case CANCEL_ACTION: 
         Toast.makeText(context, "cancel", Toast.LENGTH_SHORT).show(); 
// you cancel action 
         break; 
       } 
      } 
     } 



    } 

最後,將receiver加入您的manifests

<receiver android:name=".MyNotificationReceiver"> 
      <intent-filter> 
       <action android:name="RESUME_ACTION"/> 
       <action android:name="STOP_ACTION"/> 
       <action android:name="CANCEL_ACTION"/> 

      </intent-filter> 
     </receiver>