PendingIntent 完成 [英] PendingIntent completion

查看:30
本文介绍了PendingIntent 完成的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在服务中记录:

    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);

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

After the device goes to sleep and wakes up, a notification appears on the screen. If you click on it, the minimized activity will be restored.

如何跟踪 PendingIntent 的完成情况?

How can i track completion of PendingIntent?

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

Maybe someone knows how to use PendingIntent.onFinished?

推荐答案

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

There are some Activity methods which will help you to find out whether the Activity comes to the foreground because the user tapped the notification:

getIntent() 会给你触发 Activity 创建的 Intent 如果在 Activity 必须(重新)创建时点击通知发生.

getIntent() will give you the Intent which triggered Activity creation if tapping the notification happens when the Activity has to be (re)created.

覆盖 onNewIntent() 将使您能够访问负责重新启动现有 ActivityIntent.请注意,您还可以将新的 Intent 设置为the".此ActivityIntent:

Overriding onNewIntent() will give you access to the Intent responsible for re-launching an existing Activity. Note that you also have the opportunity to set the new Intent as "the" Intent for this Activity:

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

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

So you can put a boolean as Intent extra when creating the Intent for your PendingIntent ...

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()

... and evaluate this in your code, e.g. in Activity.onResume()

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

我知道,当 Activity 已经启动并运行时,通过这种方式您将无法判断用户何时第二次点击通知.由于我不知道您的用例,因此我无法判断这是否会发生,或者这是否会成为问题.但由于您还可以将其他数据作为 Intent 额外传递,因此应该可以根据需要识别每个事件.

I'm aware that this way you won't be able to tell when the user taps the notification a second time when the Activity is already up and running. Since I don't know your use case I can't tell whether this can happen or whether this would be a problem. But since you can also pass other data as Intent extra, it should be possible to identify each event if required.

这篇关于PendingIntent 完成的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆