gpt4 book ai didi

android - 通过点击通知判断app是否启动的方法

转载 作者:太空狗 更新时间:2023-10-29 15:53:57 24 4
gpt4 key购买 nike

有没有办法通过点击通知或正常启动来跟踪确定Android应用程序是否已启动?

最佳答案

你可以放一个extra在用于为通知创建 PendingIntent 的 Intent 中。

来自official guide :

// Instantiate a Builder object.
NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
// Creates an Intent for the Activity
Intent notifyIntent =
new Intent(new ComponentName(this, ResultActivity.class));
// Sets the Activity to start in a new, empty task
notifyIntent.setFlags(FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_CLEAR_TASK);
// Creates the PendingIntent
PendingIntent notifyPendingIntent =
PendingIntent.getActivity(
this,
0,
notifyIntent
PendingIntent.FLAG_UPDATE_CURRENT
);

// Puts the PendingIntent into the notification builder
builder.setContentIntent(notifyPendingIntent);
// Notifications are issued by sending them to the
// NotificationManager system service.
NotificationManager mNotificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
// Builds an anonymous Notification object from the builder, and
// passes it to the NotificationManager
mNotificationManager.notify(id, builder.build());

只需添加 notifyIntent.putExtra("fromNotification", true);

关于android - 通过点击通知判断app是否启动的方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18403482/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com