7

我試圖讓我的通知有一個按鈕,點擊它時應該調用我的服務,它控制音頻播放。Android Notification操作未調用待定服務意圖

這是我的意圖

 Intent intent = new Intent(context, AudioStreamService.class); 


    Random generator = new Random(); 


    PendingIntent i = PendingIntent.getActivity(context, 579, intent,PendingIntent.FLAG_UPDATE_CURRENT); 


    final NotificationCompat.Builder builder = new NotificationCompat.Builder(context) 
      .setSmallIcon(R.drawable.ic_launcher) 
      .setContentTitle(title) 
      .setContentText(text) 
      .setPriority(NotificationCompat.PRIORITY_DEFAULT) 
      .setLargeIcon(picture) 
      .setTicker(ticker) 
      .setNumber(number) 
      .setOngoing(true) 
      .addAction(
       R.drawable.ic_action_stat_reply, 
       res.getString(R.string.action_reply), 
       i); 

    notify(context, builder.build()); 

通知下面是上開始爲我服務

public int onStartCommand(Intent intent, int flags, int startId) { 
    Log.e("APP ID", "APP ID - service called "); 

    if(isPlaying){ 
     stop(); 
    } 
    else { 
     play(); 
    } 
    return Service.START_STICKY; 
} 

從通知中的動作調用時日誌永遠不會觸發。但該服務被應用程序中的按鈕使用並正常工作。日誌被調用,就像下面的命令一樣。

回答

13

用途:代替

PendingIntent pendingIntent = PendingIntent.getService(context, 579, intent,PendingIntent.FLAG_UPDATE_CURRENT); 

PendingIntent pendingIntent = PendingIntent.getActivity(context, 579, intent,PendingIntent.FLAG_UPDATE_CURRENT); 

^^這是用來啓動從通知的活動。

+0

太棒了,謝謝! – Dandrews