【问题标题】:custom notification button click自定义通知按钮点击
【发布时间】:2016-06-08 15:32:40
【问题描述】:

在我的应用程序中,我有一个要显示的通知。

假设显示通知时我想按“是”进入活动并隐藏通知,按“否”什么都不做,只是隐藏通知。

我尝试了此代码,但 onclick 不是 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());

我该怎么做?

【问题讨论】:

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


【解决方案1】:

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

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

    字符串 SHOULD_OPEN = "should_open_intent" Intent yes = new Intent(this, MyReceiver.class); yes.putBooleanExtra(SHOULD_OPEN, true); 和 Intent "no" 相同

  3. 用 PendingIntent 包装它们
  4. 在您的接收器中,在“onReceive”函数中使用intent.getBooleanExtra(SHOUOLD_OPEN, DEFAULT_VALUE_DOESNT_MATTER) 获取该数据 并进行相应处理

看看这个 - https://stackoverflow.com/a/26486425/3339597

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多