需要一些幫助,以使用定製按鈕構建Jelly Bean通知(使用addAction)。 問題是,我不能讓它工作。 基本上,我想用以下參數建立通知:在通知點擊它時,必須帶上我的一個活動,在顯示通知時按鈕點擊 - 播放暫停播放器,它正在運行。 我的代碼是: 接收器:JellyBean中的通知 - 添加動作不起作用
private BroadcastReceiver onNotification = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
Log.d("NOTIFICATION", "Received broadcast");
Bundle extras = intent.getExtras();
int state = extras.getInt("state");
if (state == 1) {
playPauseMethod();
}
}
};
在活動的onCreate方法我已經添加:
IntentFilter notif_iff = new IntentFilter(
MainService.NOTIF_BROADCAST);
LocalBroadcastManager.getInstance(getActivity()).registerReceiver(
onNotification, notif_iff);
守則MainService是以下幾點:
public static final String ACTION = "com.formatbce.mdrive.action.UPDATE_UI";
public static final String NOTIF_BROADCAST = "com.formatbce.mdrive.action.NOTIF_BRD";
Intent in = new Intent(ACTION);
Intent notifInt = new Intent(NOTIF_BROADCAST);
private NotificationManager mNM = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
private void showNotification(final int statusBarIconID,
final int bigIconID, final int ppIconID, final String contentTitle,
final String contentText, final boolean showIconOnly) {
PendingIntent layoutIntent = PendingIntent.getActivity(this, 0,
new Intent(this, MediaFragmentActivity.class), 0);
int state = 1;
notifInt.setAction("blabla");
notifInt.putExtra("state", state);
PendingIntent mIntent = PendingIntent.getBroadcast(
getApplicationContext(), 0, notifInt,
PendingIntent.FLAG_UPDATE_CURRENT);
notification = new Notification.Builder(this)
.setContentTitle(contentTitle).setContentText(contentText)
.setSmallIcon(bigIconID).setContentIntent(layoutIntent)
.addAction(ppIconID, null, mIntent)
.build();
notification.flags |= Notification.FLAG_ONGOING_EVENT;
mNM.notify(NOTIFICATION, notification);
}
所以,當我打電話showNotification()方法,顯示通知,並點擊它的正文運行良好。但是,當我點擊一個按鈕,用的addAction設置好的,沒有什麼變化......我的意思是,在的onReceive()甚至行不行:
Log.d("NOTIFICATION", "Received broadcast");
什麼可以錯在這裏?我大量閱讀手冊,但無法得到它。有人可以解釋,如何讓它工作? 謝謝!
P.S.當我更改addAction PendingIntent getActivity,它工作正常,順便說一句...
有人嗎?請至少告訴我,我對該功能的理解有問題,或者沒有人真正知道答案? :( – formatBCE 2013-03-05 09:23:58