Android 如何在新的通知生成器中收听PendingEvent?
我可以创建一个包含3个操作的通知,但我希望在单击每个操作时调用一个方法Android 如何在新的通知生成器中收听PendingEvent?,android,Android,我可以创建一个包含3个操作的通知,但我希望在单击每个操作时调用一个方法 Intent intent = new Intent( this, MainActivity.class ); PendingIntent pIntent = PendingIntent.getActivity( this, (int) System.currentTimeMillis(), intent, 0 ); // Build notification // Actions are ju
Intent intent = new Intent( this, MainActivity.class );
PendingIntent pIntent = PendingIntent.getActivity( this, (int) System.currentTimeMillis(), intent, 0 );
// Build notification
// Actions are just fake
Notification noti = new Notification.Builder( this )
.setContentTitle( "New mail from " + "test@gmail.com" )
.setContentText( "Subject" ).setSmallIcon( R.drawable.icon )
.setContentIntent( pIntent )
.addAction( R.drawable.icon, "Call", pIntent )
.addAction( R.drawable.icon, "More", pIntent )
.addAction( R.drawable.icon, "And more", pIntent ).build();
NotificationManager notificationManager = (NotificationManager) getSystemService( NOTIFICATION_SERVICE );
// hide the notification after its selected
noti.flags |= Notification.FLAG_AUTO_CANCEL;
notificationManager.notify( 0, noti );
如何做到这一点?步骤1:创建三个独立的Intent
对象,每个对象都有一个额外的或一些东西,以区别于其他两个对象
步骤2:创建三个独立的pendingent
对象,这将要求您对ID使用与System.currentTimeMillis()
不同的解决方案(因为创建三个背对背的对象可能会产生相同的毫秒时间值)
步骤#3:让每个动作使用这三个pendingent
对象中自己的一个
第四步:在
onCreate()
和/或onNewIntent()
中,检查意图的额外部分,查看单击了三个操作中的哪一个,并调用所需的方法@user8780097:关于“代码在哪里?别浪费我的时间”
——当然,你的评论会激励许多人帮助你。