【问题标题】:PendingIntent completionPendingIntent 完成
【发布时间】:2021-06-19 09:44:11
【问题描述】:

我在服务中记录了这个:

    intent = new Intent(this,MainActivity_Large.class);

    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP |
            Intent.FLAG_ACTIVITY_SINGLE_TOP); 

    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent,  0);


   
            NotificationCompat.Builder builder =
            new NotificationCompat.Builder(this,CHANNEL_ID)
                    .setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
                    .setAutoCancel(true)
                    .setChannelId(CHANNEL_ID)
                    .setDefaults(Notification.DEFAULT_ALL)
                    .setSmallIcon(R.drawable.ant_intro)
                    .setContentTitle("Anttack")
                    .setContentText("Continues")
                    .setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION))
                    //     .setPriority(2)
                    .setContentIntent(pendingIntent);

    if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        builder.setCategory(Notification.CATEGORY_SERVICE);
    }

    notification = builder.build();
    startForeground(1, notification);

设备进入睡眠状态并唤醒后,屏幕上会显示一条通知。如果你点击它,最小化的活动将被恢复。

如何跟踪 PendingIntent 的完成情况?

也许有人知道如何使用 PendingIntent.onFinished?

【问题讨论】:

  • 您希望在点击通知时启动一个全新的活动,而不是最小化的活动?你能详细说明一下吗?
  • 相同的折叠活动被恢复。
  • 看起来将被带到前台的Activity属于您自己的应用程序。如果我理解正确,您需要知道用户从通知中触发了 PendingIntent 而不是显示的 Activity 因为用户点击了启动器图标。这是正确的吗?
  • 是的。没错。

标签: java android android-pendingintent


【解决方案1】:

有一些Activity 方法可以帮助您确定Activity 是否因为用户点击了通知而出现在前台:

如果在必须(重新)创建Activity 时点击通知,getIntent() 将为您提供触发Activity 创建的Intent

覆盖onNewIntent() 将使您能够访问负责重新启动现有ActivityIntent。请注意,您还有机会为此 Activity 设置新的 Intent 为“该”Intent

@Override
protected void onNewIntent(Intent newIntent){
    setIntent(newIntent);
}

因此,在为您的PendingIntent 创建Intent 时,您可以将boolean 额外添加为Intent ...

intent = new Intent(this,MainActivity_Large.class);
intent.putExtra("FROM_NOTIFICATION", true);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP |
        Intent.FLAG_ACTIVITY_SINGLE_TOP); 

... 并在您的代码中对此进行评估,例如在Activity.onResume()

@override
protected void onResume(){
    if(getIntent().hasExtra("FROM_NOTIFICATION")){
        // Activity was (re-)launched because user tapped notification
    }
}

我知道,当Activity 已经启动并运行时,您将无法判断用户第二次点击通知的时间。由于我不知道您的用例,我无法判断这是否会发生或这是否会成为问题。但是由于您也可以将其他数据作为Intent extra 传递,因此如果需要,应该可以识别每个事件。

【讨论】:

  • 看起来不错。我明天试试。重复通知的可能性应该是。我认为如果可以在 onResume() 中将‘hasExtra ("FROM_NOTIFICATION") 清零,那么它应该可以工作。
  • 感谢您的回复。就我而言,发生onNewIntent() 调用就足够了。
猜你喜欢
  • 1970-01-01
  • 2015-12-13
  • 2012-04-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多