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
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 use new Intent(String)
or in your specific case new 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.
精彩评论