Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/macos/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 如何在新的通知生成器中收听PendingEvent?_Android - Fatal编程技术网

Android 如何在新的通知生成器中收听PendingEvent?

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

我可以创建一个包含3个操作的通知,但我希望在单击每个操作时调用一个方法

  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:关于
“代码在哪里?别浪费我的时间”
——当然,你的评论会激励许多人帮助你。