在使用自定义按钮(使用 addAction)构建 Jelly Bean 通知方面需要一些帮助。问题是,我不能让它工作。基本上,我想使用以下参数构建通知:在通知单击时,它必须启动我的一项活动,在按钮单击时 - 正在运行的播放暂停播放器,当显示通知时。我的代码是: 接收者:
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");
这里有什么问题?我大量阅读手册,但无法得到它。有人可以解释一下,如何让它工作?谢谢!
PS当我将addAction PendingIntent更改为getActivity时,它工作正常,顺便说一句......