Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/328.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
Java 通知恢复应用程序而不是重新启动_Java_Android_Notifications_Resume - Fatal编程技术网

Java 通知恢复应用程序而不是重新启动

Java 通知恢复应用程序而不是重新启动,java,android,notifications,resume,Java,Android,Notifications,Resume,我已经阅读了关于这个问题的多个问题和答案,但是我无法让它们中的任何一个为我工作 我收到一个通知,单击后,我希望将应用程序置于前端并继续,而不是关闭并重新启动 这是我的通知代码 NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this) .setSmallIcon(R.drawable.ic_launcher) .setConte

我已经阅读了关于这个问题的多个问题和答案,但是我无法让它们中的任何一个为我工作

我收到一个通知,单击后,我希望将应用程序置于前端并继续,而不是关闭并重新启动

这是我的通知代码

    NotificationCompat.Builder mBuilder =
            new NotificationCompat.Builder(this)
            .setSmallIcon(R.drawable.ic_launcher)
            .setContentTitle("example")
            .setContentText("example");
    // Creates an explicit intent for an Activity in your app
    Intent resultIntent = new Intent(this, MainActivity.class);
    resultIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
    // The stack builder object will contain an artificial back stack for the
    // started Activity.
    // This ensures that navigating backward from the Activity leads out of
    // your application to the Home screen.
    TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
    // Adds the back stack for the Intent (but not the Intent itself)
    stackBuilder.addParentStack(MainActivity.class);
    // Adds the Intent that starts the Activity to the top of the stack
    stackBuilder.addNextIntent(resultIntent);
    PendingIntent resultPendingIntent =
            stackBuilder.getPendingIntent(
                0,
                PendingIntent.FLAG_UPDATE_CURRENT
            );
    mBuilder.setContentIntent(resultPendingIntent);
    NotificationManager mNotificationManager =
        (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    int mId = 0;
    // mId allows you to update the notification later on.
    mNotificationManager.notify(mId, mBuilder.build());
在我的清单文件中我有

        android:launchMode="singleTop"

有人能看出哪里出了问题吗?虽然通知拒绝恢复应用程序,而是重新启动应用程序,但我没有收到任何错误。

我最近遇到了一些类似问题,使用以下标志更改您的意图标志:

resultIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_SINGLE_TOP);
编辑:并从清单中删除该启动模式

希望它有帮助

通过使用以下内容进行修复:

    NotificationCompat.Builder mBuilder =
            new NotificationCompat.Builder(this)
            .setSmallIcon(R.drawable.ic_launcher)
            .setContentTitle("Sound Asleep")
            .setContentText("Click to play and stop sounds");
    // Creates an explicit intent for an Activity in your app
    final Intent notificationIntent = new Intent(this, MainActivity.class);
    notificationIntent.setAction(Intent.ACTION_MAIN);
    notificationIntent.addCategory(Intent.CATEGORY_LAUNCHER);
    // The stack builder object will contain an artificial back stack for the
    // started Activity.
    // This ensures that navigating backward from the Activity leads out of
    // your application to the Home screen.

    // Adds the back stack for the Intent (but not the Intent itself)
    PendingIntent resultPendingIntent = PendingIntent.getActivity(this,0,notificationIntent,PendingIntent.FLAG_UPDATE_CURRENT);

    mBuilder.setContentIntent(resultPendingIntent);
    NotificationManager mNotificationManager =
        (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    int mId = 0;
    // mId allows you to update the notification later on.
    mNotificationManager.notify(mId, mBuilder.build());
}
明显地

android:launchMode="singleTop"

使用下面的代码,可能会有所帮助

NotificationCompat.Builder mBuilder =
        new NotificationCompat.Builder(this)
        .setSmallIcon(R.drawable.ic_launcher)
        .setContentTitle("Sound Asleep")
        .setContentText("Click to play and stop sounds");
// Creates an explicit intent for an Activity in your app
final Intent notificationIntent = new Intent(this, MainActivity.class);
notificationIntent.setAction(Intent.ACTION_MAIN);
notificationIntent.addCategory(Intent.CATEGORY_LAUNCHER);
// The stack builder object will contain an artificial back stack for the
// started Activity.
// This ensures that navigating backward from the Activity leads out of
// your application to the Home screen.

// Adds the back stack for the Intent (but not the Intent itself)
PendingIntent resultPendingIntent = PendingIntent.getActivity(this,0,notificationIntent,PendingIntent.FLAG_ACTIVITY_REORDER_TO_FRONT);

mBuilder.setContentIntent(resultPendingIntent);
NotificationManager mNotificationManager =
    (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
int mId = 0;
// mId allows you to update the notification later on.
mNotificationManager.notify(mId, mBuilder.build());

resultIntent.setFlags(意图.标志\u活动\u重新排序\u到\u前端);仍然导致应用程序重新启动。也许,您试图带到前台的活动已被破坏?应用程序会在按下按钮时播放声音。在测试这些方法时,我一直在播放声音,最小化应用程序,然后下拉抽签并选择通知。选择通知后,音乐停止,应用程序重新启动。请尝试
singleTask
singleInstance
,可能会有所帮助。我认为用声音测试可能是不正确的。当我观看“活动生命周期”时,我使用日志和生命周期回调来打印消息。我尝试过,但应用程序仍在重新加载,这让我很困惑,无法找到原因。只需创建PendingEvent ResultPendingEvent=PendingEvent.getActivities(上下文,0,resultIntent,PendingEvent.FLAG\u UPDATE\u CURRENT)而不使用堆栈生成器即可由于您在resultIntent上设置了标志,因此我认为没有必要删除这些标志并使用-PendingEvent ResultPendingEvent=PendingEvent.getActivity(这个,0,resultIntent,PendingEvent.FLAG\u UPDATE\u CURRENT);仍在重新启动应用程序。似乎没有什么能解决我的问题。这对我来说很有效,但在manifest android:launchMode=“singleInstance”中更改mId=1。谢谢