通过单击通知来确定是否已启动应用程序的方法

时间:2013-08-23 12:57:01

标签: android notifications

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

2 个答案:

答案 0 :(得分:4)

在定义通知时,在通知中添加一些布尔标志作为额外。然后在主要活动开始检查意图是否包含额外的。

答案 1 :(得分:4)

您可以在用于为通知创建PendingIntent的Intent中放置extra

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

即可