[英]Notification in JellyBean - add action isn't work
Need some help with building an Jelly Bean notification with custom button (using addAction). 在使用自定义按钮(使用addAction)构建Jelly Bean通知时需要一些帮助。 The problem is, that i cannot just make it working.
问题是,我不能让它正常工作。 Basically, i want to build notification with following parameters: on notification click it must bring one of my activities up, on button click - play-pause player, which is running, when notification is shown.
基本上,我想使用以下参数构建通知:单击通知时,它必须调出我的一项活动,单击按钮时-显示通知时正在运行的播放暂停播放器。 My code is: Receiver:
我的代码是:接收方:
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();
}
}
};
In onCreate method of activity i've added: 在活动的onCreate方法中,我添加了:
IntentFilter notif_iff = new IntentFilter(
MainService.NOTIF_BROADCAST);
LocalBroadcastManager.getInstance(getActivity()).registerReceiver(
onNotification, notif_iff);
Code in MainService is following: 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);
}
So, when i calling showNotification() method, notification is shown, and click in it's body is working well. 因此,当我调用showNotification()方法时,将显示通知,然后单击它的主体即可正常工作。 But when i'm clicking a button, setted with addAction, nothing changes... I mean, even that line in onReceive() isn't work:
但是,当我单击设置有addAction的按钮时,没有任何变化...我的意思是,即使onReceive()中的那行也不起作用:
Log.d("NOTIFICATION", "Received broadcast");
What can be wrong here? 这里有什么问题? I read manuals in huge amount, but can't get it.
我大量阅读了手册,但听不懂。 Can somebody explain, how to get it working?
有人可以解释一下,如何使其工作吗? Thank you!
谢谢!
PS When i changing addAction PendingIntent to getActivity, it works fine, btw... PS当我将addAction PendingIntent更改为getActivity时,它工作正常,顺便说一句...
I think what you're stuck with here is that the BroadcastReceiver isn't setup correctly. 我认为您在这里遇到的问题是BroadcastReceiver的设置不正确。 I'd actually suggest using a Service instead of a BroadcastReceiver, just due to latency of the BroadcastReceiver (at the same time, it should happen soon enough that it's likely not a big deal).
实际上,我建议使用Service而不是BroadcastReceiver,这仅仅是由于BroadcastReceiver的延迟(同时,应该尽快发生,以至于没什么大不了的)。
Do you have the BroadcastReceiver setup correctly in your Manifest? 您的清单中是否正确设置了BroadcastReceiver?
Are you able to create an activity that just invokes the BroadcastReceiver (to verify that it's working correctly)? 您是否能够创建一个仅调用BroadcastReceiver的活动(以验证其是否正常工作)?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.