Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sockets/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 通知栏的单击事件_Android_Notifications - Fatal编程技术网

Android 通知栏的单击事件

Android 通知栏的单击事件,android,notifications,Android,Notifications,单击通知栏时,它将跳过MainActivity.class界面: Intent notificationIntent = new Intent(context,MainActivity.class); //After click the notification, it will skip to the activity PendingIntent contentIntent = PendingIntent.getActivity(context,0,notificationIntent,0)

单击通知栏时,它将跳过
MainActivity.class
界面:

Intent notificationIntent = new Intent(context,MainActivity.class); //After click the notification, it will skip to the activity 
PendingIntent contentIntent = PendingIntent.getActivity(context,0,notificationIntent,0);
notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);
如果
MainActivity.class
本身已经打开,我再次单击通知栏,它会显示两个界面。谁知道如何解决这个问题

更改您的线路:

Intent notificationIntent = new Intent(context,MainActivity.class);
致:

从Android文档中可以看出:


如果已设置,则如果活动已在历史堆栈顶部运行,则不会启动该活动

或者,如果这不是预期的行为,请检查此标志:


如果已设置,并且正在启动的活动已在当前任务中运行,则不会启动该活动的新实例,而是关闭其上的所有其他活动,并且此意图将作为新意图传递给(现在位于顶部的)旧活动

当您试图了解这些标志将导致什么时,您可能会对以下文档感兴趣:

  • (特别是第节)
Intent notificationIntent = 
    new Intent(context,MainActivity.class).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);