Android 中如何通过通知检查 Activity 是否已启动?

问题描述 投票:0回答:2

我正在制作一个Android应用程序。我有一个问题,就是我的MainActivity可以通过3种方式创建:

  1. 标准应用程序启动
  2. 来自服务
  3. 并从通知中单击。

如何检查通知点击何时开始?

通知代码:

private void createNotification()
{
    Log.d("service createNotification",MainActivity.TAG);
    Context context = getApplicationContext();
    Intent notificationIntent = new Intent(this,MainActivity.class);
    intent.putExtra(AppNames.IS_NOTIFICATION_INTENT,true);
    PendingIntent pendingIntent = PendingIntent.getActivity(context, 0,
            notificationIntent, Intent.FLAG_ACTIVITY_NEW_TASK);

    NotificationCompat.Builder builder = new NotificationCompat.Builder(context)
            .setContentTitle(this.getString(R.string.notification_title))
            .setContentText(this.getString(R.string.notification_text))             
            .setContentIntent(pendingIntent)             
            .setSmallIcon(R.drawable.ic_launcher);

    getSystemService(Context.NOTIFICATION_SERVICE);
    notificationManager.notify(AppNames.APP_NOTIFICATION, builder.getNotification());
}
android android-activity notifications
2个回答
8
投票

添加

intent.putExtra("started_from","notification");

对于从通知启动意图的代码,对于其他 startActivity 调用也是如此,只需更改值,然后在您的活动中

String startedFrom = getIntent().getStringExtra("started_from");

有关更多信息,请参阅此问题:如何从 Android 上的 Intent 获取额外数据?


0
投票

要确定哪个服务或广播接收器在您的应用程序中充当通知蹦床,请查看以下终端命令的输出:

adb shell dumpsys activity service \com.android.systemui/.dump.SystemUIAuxiliaryDumpService

输出包含文本“NotifInteractionLog”。此部分包含识别因通知点击而启动的组件所需的信息。

© www.soinside.com 2019 - 2024. All rights reserved.