从通知发送的 PendingIntent
我想要完成的是通过通知管理器发送通知 一旦单击,仅当应用程序当前正在运行时才会在应用程序中执行某些操作。 我尝试使用:
notification.contentIntent = PendingIntent.getActivity(this, nNotificationCounter, Someintent, PendingIntent.FLAG_NO_CREATE)
一旦尝试使用通知,总会导致异常。 我切换到:
Notification notification = new Notification(icon, tickerText, when);
RemoteViews contentView = new RemoteViews(getPackageName(), R.layout.some_notification);
contentView.setTextViewText(R.id.title, sTitle);
contentView.setTextViewText(R.id.text, sText);
notification.contentView = contentView;
notification.defaults |= Notification.DEFAULT_SOUND;
notification.number = nNotificationCounter;
Intent notificationIntent = new Intent(this, MainWindow.class).setAction(ACTION_RESET_MESSGE_COUNTER);
notification.contentIntent = PendingIntent.getBroadcast(this, nNotificationCounter, notificationIntent, 0);
尽管这段代码不会导致异常。它不会调用我的 BroadcastReceiver,其定义如下:
public class IncomingReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(ACTION_RESET_MESSGE_COUNTER)) {
System.out.println("GOT THE INTENT");
return;
}
}
}
并在 onCreate 中设置:
IntentFilter filter = new IntentFilter(ACTION_RESET_MESSGE_COUNTER);
IncomingReceiver receiver = new IncomingReceiver();
context.registerReceiver(receiver, filter);
有人看到代码有问题吗? 或者,当单击通知时,我将如何获取消息,但如果尚未创建,则不创建任何活动。
编辑:添加了意图创建和通知创建。
谢谢, 汤姆
What im trying to accomplish is to send a notification through the notification manager
that once clicked will do something in the application only if its currently running.
i have tried to use:
notification.contentIntent = PendingIntent.getActivity(this, nNotificationCounter, Someintent, PendingIntent.FLAG_NO_CREATE)
Which allways caused an exception once trying to use the notify.
I switched to:
Notification notification = new Notification(icon, tickerText, when);
RemoteViews contentView = new RemoteViews(getPackageName(), R.layout.some_notification);
contentView.setTextViewText(R.id.title, sTitle);
contentView.setTextViewText(R.id.text, sText);
notification.contentView = contentView;
notification.defaults |= Notification.DEFAULT_SOUND;
notification.number = nNotificationCounter;
Intent notificationIntent = new Intent(this, MainWindow.class).setAction(ACTION_RESET_MESSGE_COUNTER);
notification.contentIntent = PendingIntent.getBroadcast(this, nNotificationCounter, notificationIntent, 0);
and although this code doesn't cause an exception. it doesnt call my BroadcastReceiver which is defined as follows:
public class IncomingReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(ACTION_RESET_MESSGE_COUNTER)) {
System.out.println("GOT THE INTENT");
return;
}
}
}
and set in the onCreate:
IntentFilter filter = new IntentFilter(ACTION_RESET_MESSGE_COUNTER);
IncomingReceiver receiver = new IncomingReceiver();
context.registerReceiver(receiver, filter);
Does anyone see something wrong with the code?
Or how would i go about to get messages when the notification is clicked, but not create any activity if it isn't already created.
edit: added the intent creation and notification creation.
Thanks,
Tom
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您很可能无法收到广播,因为您错误地创建了
Intent
。Intent(Context, Class)
告诉系统创建一个意图来“创建”该类。换句话说,该意图仅直接针对硬编码类。只需使用new Intent(String)
或在您的具体情况下new Intent(ACTION_RESET_MESSGE_COUNTER)
,您将创建我所说的“广播”意图或文档所说的“行动”意图。这样,意图将被定向到正在收听该特定广播/操作的任何人。You're most likely not getting the broadcast because you are incorrectly creating your
Intent
.Intent(Context, Class<?>)
tells the system to create an intent that will 'create' that class. In other words, that intent is directly solely to the hard coded class. Just usenew Intent(String)
or in your specific casenew Intent(ACTION_RESET_MESSGE_COUNTER)
and you'll create what I call a 'broadcast' intent or what the docs call an 'action' intent. That way the intent will be directed to anyone who is listening for that specific broadcast/action.