2012-02-22 84 views
0

我收到了自定義狀態欄通知。它上面有一個「播放」和「下一個」圖像。 我想在按下「播放」或「下一張」圖像時將廣播意圖發送到服務。自定義狀態欄通知

notification = new Notification(R.drawable.ic_launcher, title + "\n" 
     + text, System.currentTimeMillis()); 
contentIntent = PendingIntent.getActivity(context, 0, actionOnClick, 0); 
notification.contentIntent = contentIntent; 

contentView = new RemoteViews(context.getPackageName(), 
     R.layout.custom_player_notification); 
contentView.setTextViewText(R.id.custom_player_notification_title, 
     title); 
contentView.setTextViewText(R.id.custom_player_notification_text, text); 
notification.contentView = contentView; 

這是代碼。 ContentView提供了設置「setOnClickPendingIntent」和「setOnClickFillInIntent」的可能性。我如何使用它們發送此廣播? 我想發送廣播,因爲我不想發起活動。

回答

0

我要廣播意圖發送到服務時,「播放」或「下一個」像被

我建議你要通過startService()發送命令,而不是廣播。

如何使用它們發送此廣播?

你可以嘗試調用setOnClickPendingIntent(),通過靜態getBroadcast()方法上PendingIntent提供創建PendingIntent(用於廣播)或getService()(對於通過startService()發送一個命令)。

請記住,目前大多數Android設備都無法使用此功能。我認爲按鈕在Notifications工程開始的Android 4.0,也許3.0,但絕對不是在那之前。

+0

先生,你能不能把重點放在這個問題上? http://stackoverflow.com/questions/27169367/android-update-specific-notification-from-multiple-notifications-which-have-rem 我會提供更多的信息,如果你想。 – 2014-11-28 07:43:18

0

您可以在通知的onClick()方法中加入與此相似的內容,將其指向廣播接收器。

 Intent intent = new Intent(this, receiver.class); 
     intent.putExtra("SomeInfoMaybe", info); 
     PendingIntent pendingIntent = PendingIntent.getBroadcast(
       this.getApplicationContext(), 234324243, intent, PendingIntent.FLAG_CANCEL_CURRENT); 

然後處理下一階段的代碼可以去這裏:

公共類接收器擴展了BroadcastReceiver {

@Override 
public void onReceive(Context context, Intent intent) { 
      Bundle bundle = intent.getExtras(); 
    System.out.println("SomeInfoMaybe = " + bundle.getString("info")); 
      //etc... 
    }                   

}

調用.notify();在你的通知應該幫助鏈接到廣播接收器