Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/192.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/jquery-ui/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_Android Activity_Notifications - Fatal编程技术网

在Android中使用通知,将活动带到前台

在Android中使用通知,将活动带到前台,android,android-activity,notifications,Android,Android Activity,Notifications,我提出了一个通知。我想要的是,当用户单击通知时,我的活动将显示在最前面,而不创建它的新实例。 为此,我添加了标志REORDER_TO_FRONT,但当我单击通知时,仍然调用oncreate而不是onNewIntent 这是我的密码- int icon = R.drawable.android; long when = System.currentTimeMillis(); CharSequence text = "new message";

我提出了一个通知。我想要的是,当用户单击通知时,我的活动将显示在最前面,而不创建它的新实例。 为此,我添加了标志REORDER_TO_FRONT,但当我单击通知时,仍然调用oncreate而不是onNewIntent

这是我的密码-

        int icon = R.drawable.android;
        long when = System.currentTimeMillis();
        CharSequence text = "new message";
        CharSequence contentTitle = stanza.getFrom();  // message title
        CharSequence contentText = stanza.getBody();      // message text

        Intent notificationIntent = new Intent(context, ChatBox.class);
        notificationIntent.putExtra("buddyid",stanza.getFrom());
        notificationIntent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
        PendingIntent contentIntent = PendingIntent.getActivity(context, 0,notificationIntent, 0);


// the next two lines initialize the Notification, using the configurations above
        Notification notification = new Notification(icon, text, when);
        notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);
        mNotificationManager.notify(1,notification);
你有没有试过:

Intent.FLAG_ACTIVITY_CLEAR_TOP
使用您的notificationIntent.addFlag()

你试过:

Intent.FLAG_ACTIVITY_CLEAR_TOP

使用您的notificationIntent.addFlag()

我的解决方案是制作一个广播接收器,监听通知触发的广播动作。所以基本上:

  • 通知会触发一个广播操作,其中包含要启动的活动的名称

  • 广播接收器在单击通知时捕捉到这一点,然后使用标志\u activity\u REORDER\u to\u FRONT标志创建启动该活动的意图

  • 活动被带到活动堆栈的顶部,没有重复项


  • 对此,我的解决方案是制作一个广播接收器,用于侦听通知触发的广播操作。所以基本上:

  • 通知会触发一个广播操作,其中包含要启动的活动的名称

  • 广播接收器在单击通知时捕捉到这一点,然后使用标志\u activity\u REORDER\u to\u FRONT标志创建启动该活动的意图

  • 活动被带到活动堆栈的顶部,没有重复项


  • 我试过了,现在,同样的结果,oncreate被调用了。现在试试这个。意图=新意图(上下文,ChatBox.class);intent.setFlags(intent.FLAG_ACTIVITY_NEW_TASK|intent.FLAG_ACTIVITY_SINGLE_TOP);在manifest中,添加到您的活动android:launchMode=“singleTop”我想这会对您有所帮助。我尝试过,现在,相同的结果是,oncreate被调用。好的。现在试试这个。意图=新意图(上下文,ChatBox.class);intent.setFlags(intent.FLAG_ACTIVITY_NEW_TASK|intent.FLAG_ACTIVITY_SINGLE_TOP);在清单中添加到您的活动android:launchMode=“singleTop”我想这会对您有所帮助。访问对我来说很好!访问对我来说很好的网站!