自定义通知按钮单击

时间:2015-08-16 19:41:21

标签: android android-notifications android-notification-bar

在我的申请表中,我有通知显示。

让我们说当通知显示时我想按“是”进行活动并隐藏通知,按“否”不做任何事情只是隐藏通知。

我尝试了这段代码,但是onclick不是onClckPendingIntent,而是我无法做任何我想做的事。

NotificationManager mNotificationManager = (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);
        RemoteViews remoteViews = new RemoteViews(getPackageName(), R.layout.custom_push_layout);
        NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this)
                .setSmallIcon(R.drawable.ic_launcher)
                .setContent(remoteViews)
                .setAutoCancel(true);

        Intent intent = new Intent(this,GPSTrackingActivity.class);
        final Intent yesIntent = new Intent(intent);
        final Intent noIntent = new Intent(this, GPSTrackingActivity.class);

        TaskStackBuilder yesStackBuilder = TaskStackBuilder.create(this);
        yesStackBuilder.addParentStack(MainActivity.class);
        yesStackBuilder.addNextIntent(yesIntent);

        TaskStackBuilder noStackBuilder = TaskStackBuilder.create(this);
        noStackBuilder.addParentStack(MainActivity.class);
        noStackBuilder.addNextIntent(noIntent);

        PendingIntent yesPendingIntent = yesStackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
        PendingIntent noPendingIntent = noStackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);


        remoteViews.setOnClickPendingIntent(R.id.btn_yes, yesPendingIntent);
        remoteViews.setOnClickPendingIntent(R.id.btn_no, noPendingIntent);
        mNotificationManager.notify(100, mBuilder.build());

我怎么能这样做?

1 个答案:

答案 0 :(得分:0)

我知道这有点晚了,但我设法通过接收器完成任务。 你应该:

  1. 创建接收器
  2. 为接收器创建两个意图 -

    String SHOULD_OPEN =“should_open_intent” Intent yes = new Intent(this,MyReceiver.class); yes.putBooleanExtra(SHOULD_OPEN,true); 在意图“不”

  3. 上也一样
  4. 用PendingIntent
  5. 包装它们
  6. 在您的接收器中,在函数“onReceive”上使用intent.getBooleanExtra(SHOUOLD_OPEN,DEFAULT_VALUE_DOESNT_MATTER)获取该数据 并据此处理
  7. 检查一下 - https://stackoverflow.com/a/26486425/3339597