Java 如何区分通知单击和通知按钮单击
当我单击“通知”按钮时,它自己和“操作”按钮也会这样做,我想区分它们,我想让按钮做些什么,当我单击“通知”按钮时,它自己做些什么,这是我的代码-Java 如何区分通知单击和通知按钮单击,java,android,android-notifications,Java,Android,Android Notifications,当我单击“通知”按钮时,它自己和“操作”按钮也会这样做,我想区分它们,我想让按钮做些什么,当我单击“通知”按钮时,它自己做些什么,这是我的代码- Intent intent = new Intent(this, NotificationView.class); PendingIntent pIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURREN
Intent intent = new Intent(this, NotificationView.class);
PendingIntent pIntent = PendingIntent.getActivity(this, 0, intent,
PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder builder = (NotificationCompat.Builder) new NotificationCompat.Builder(this)
.setSmallIcon(R.mipmap.ic_launcher)
.setTicker(getString(R.string.notificationticker))
.setContentTitle(getString(R.string.notificationtitle))
.setContentText(yeah)
.addAction(R.drawable.ic_favorite, getString(R.string.azor), pIntent)
.setContentIntent(pIntent)
.setAutoCancel(true);
NotificationManager notificationmanager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
notificationmanager.notify(0, builder.build());
将不同的挂起内容传递给
addAction
和setContentIntent
目前,它们都使用相同的pIntent实例
如果您希望他们启动相同的活动,但执行不同的操作,您可以在用于创建挂起的意图的意图中添加额外的。我必须做两件事
Intent hangup_intent = new Intent(getApplicationContext(), DialerScren.class)
.putExtra("intent_action", "hangup");
hangup_intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent hangup_pending = PendingIntent.getActivity(getApplicationContext(),1, hangup_intent, PendingIntent.FLAG_UPDATE_CURRENT);
Intent click_intent = new Intent(getApplicationContext(), DialerScren.class)
.putExtra("intent_action", "click");
click_intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent click_pending = PendingIntent.getActivity(getApplicationContext(), 2, click_intent, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Action hangupAction = new NotificationCompat.Action(R.drawable.ic_call_end_24,
"Hangup", hangup_pending);
NotificationCompat.Builder builder=new NotificationCompat.Builder(getApplicationContext(),channelId);
builder.setSmallIcon(R.mipmap.ic_launcher);
builder.setContentTitle("Title");
builder.setDefaults(NotificationCompat.DEFAULT_ALL);
builder.setContentText("sample_text");
builder.setContentIntent(click_pending);
builder.setDeleteIntent(click_pending);
builder.addAction(hangupAction);
builder.setAutoCancel(true);
builder.setPriority(NotificationCompat.PRIORITY_MIN);
我就是这样做的。要在通知时执行,请单击,然后单击通知操作按钮
Intent hangup_intent = new Intent(getApplicationContext(), DialerScren.class)
.putExtra("intent_action", "hangup");
hangup_intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent hangup_pending = PendingIntent.getActivity(getApplicationContext(),1, hangup_intent, PendingIntent.FLAG_UPDATE_CURRENT);
Intent click_intent = new Intent(getApplicationContext(), DialerScren.class)
.putExtra("intent_action", "click");
click_intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent click_pending = PendingIntent.getActivity(getApplicationContext(), 2, click_intent, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Action hangupAction = new NotificationCompat.Action(R.drawable.ic_call_end_24,
"Hangup", hangup_pending);
NotificationCompat.Builder builder=new NotificationCompat.Builder(getApplicationContext(),channelId);
builder.setSmallIcon(R.mipmap.ic_launcher);
builder.setContentTitle("Title");
builder.setDefaults(NotificationCompat.DEFAULT_ALL);
builder.setContentText("sample_text");
builder.setContentIntent(click_pending);
builder.setDeleteIntent(click_pending);
builder.addAction(hangupAction);
builder.setAutoCancel(true);
builder.setPriority(NotificationCompat.PRIORITY_MIN);
另一方面,在activity onNewIntent()方法中,检查从挂起的意图传递的值是单击还是挂起
String action = null;
try {
if (intent != null) {
action = intent.getExtras().getString("intent_action");
} else {
action = "UnSpecified";
}
}catch (Exception e){e.printStackTrace();}
try{
if(action.equals("hangup")){
//code for hangup notification action button clicked
}else if(action.equals("click")){
//code for notification clicked
}
}catch (Exception e){e.printStackTrace();}