有没有办法通过点击通知或正常启动来跟踪确定一个 android 应用程序是否已启动?
问问题
1511 次
2 回答
4
在定义意图时,您在通知中添加一些布尔标志作为额外的。然后在主要活动中开始检查意图是否包含该额外内容。
于 2013-08-23T12:59:15.787 回答
4
您可以在 Intent 中添加一个额外的内容,用于为通知创建 PendingIntent。
来自官方指南:
// 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);
于 2013-08-23T13:12:31.147 回答