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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/kubernetes/5.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 - Fatal编程技术网

Android:在不存在新活动时启动新活动

Android:在不存在新活动时启动新活动,android,Android,我有一个在前台工作的服务,当主活动启动它时,将使用以下代码启动通知: private void showNotification() { Log.i(TAG, LocalService.class.getName() + "showNotification()"); Intent notificationIntent = new Intent(getApplicationContext(), NotificationActivity.class); //notificat

我有一个在前台工作的服务,当主活动启动它时,将使用以下代码启动通知:

private void showNotification() {
    Log.i(TAG, LocalService.class.getName() + "showNotification()");
    Intent notificationIntent = new Intent(getApplicationContext(), NotificationActivity.class);
    //notificationIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
    notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
    PendingIntent contentIntent = PendingIntent.getActivity(getApplicationContext(), 0, notificationIntent, 0);

    nm = (NotificationManager) getApplicationContext().getSystemService(Context.NOTIFICATION_SERVICE);
    // Set the icon, scrolling text and timestamp

    int icontouse = R.drawable.icon;
    if (app.prefs.getBoolean("prefs_useprivacyicon", false)) {
        icontouse = R.drawable.icon_privacy;
    }

    n = new Notification(icontouse, getString(R.string.voice_pro_ready), System.currentTimeMillis());

    n.contentView = getRemoteView();
    n.contentIntent = contentIntent;

    startForeground(myID, n);
    nm.notify(myID, n);
}
我的问题是PendingEvent,如果主活动处于暂停状态,那么当单击状态栏中的notify时,它会出现在前面,但是如果主活动是通过finish()关闭的,则不会显示

我需要让活动在内存中变得可见,如果在内存中不可用,则启动一个新实例

notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
更改为
notificationIntent.setFlags(Intent.FLAG\u ACTIVITY\u SINGLE\u TOP)


然后再试一次

您的主要活动的启动模式是什么Yao,我正在使用标准模式尝试删除“notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);”并将活动的启动模式设置为SingleTop