我正在尝试在用户暂停我的应用程序时发出通知。因此,为了方便起见,用户可以使用NotificationAction快速访问应用程序。这是我正在使用的代码。它适用于android 4之前的所有版本,我不知道哪一个是问题所在

 NotificationCompat.Builder mBuilder =
                new NotificationCompat.Builder(this)
                .setSmallIcon(R.drawable.ic_launcher)
                .setContentTitle("Titulo")
                .setContentText("Titulo");

        mBuilder.setOngoing(true);

        // Creates an explicit intent for an Activity this
        Intent resultIntent = new Intent(getApplicationContext(), MainActivity.class);
        // put the flags
        resultIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);

        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);
        // mId allows you to update the notification later on.
        mNotificationManager.notify(mId, mBuilder.build());

因此,当我在android 4.0及更高版本中按下通知时,将再次创建活动,而不是继续。请帮忙,我做不到。
编辑(忘记清单单顶)
android:launchMode="singleTop"相同的结果,不起作用…
我的活动包含一张地图。我正在使用新版谷歌地图。V2。

最佳答案

我刚试过PendingIntent.Flag_Cancel_当前,似乎对我有用

public void showNotification(String header,String message){

            // define sound URI, the sound to be played when there's a notification
            Uri soundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);

            Intent intent = new Intent(this, MainActivity.class);

            //PendingIntent.FLAG_CANCEL_CURRENT will bring the app back up again
            PendingIntent pIntent = PendingIntent.getActivity(MainActivity.this,PendingIntent.FLAG_CANCEL_CURRENT, intent, 0);

            Notification mNotification = new Notification.Builder(this)
                .setContentTitle(header)
                .setContentText(message)
                .setSmallIcon(R.drawable.ic_launcher)
                .setContentIntent(pIntent)
                .setSound(soundUri)

                .addAction(R.drawable.ic_launcher, "View", pIntent)
                .addAction(0, "Remind", pIntent)
                .setOngoing(true)//optional
                .build();

            NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

            notificationManager.notify(0, mNotification);
}

10-07 19:31
查看更多