3

当用户暂停我的应用程序时,我正在尝试发出通知。因此,为了更容易,用户可以使用通知快速进入应用程序。这是我正在使用的代码。它适用于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。

4

4 回答 4

3

我刚试过 PendingIntent.FLAG_CANCEL_CURRENT 似乎对我有用

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);
}
于 2014-08-22T11:03:36.490 回答
2

经过大量搜索后,唯一对我有用的解决方案是执行以下操作:

NotificationCompat.Builder builder = new NotificationCompat.Builder(this).set...(...).set...(..);

Intent resultIntent = new Intent(this, MainClass.class);
resultIntent.setAction("android.intent.action.MAIN");
resultIntent.addCategory("android.intent.category.LAUNCHER");

PendingIntent resultPendingIntent = PendingIntent.getActivity(this, 0, resultIntent, PendingIntent.FLAG_UPDATE_CURRENT);

builder.setContentIntent(resultPendingIntent);
NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(0, builder.build());

这将打开您当前的活动,而无需创建另一个活动!

于 2014-10-30T15:21:10.290 回答
2

android:launchMode="singleTop"在清单声明中使用MainActivity

于 2013-06-05T07:22:01.163 回答
0

@Patrick 提供的解决方案取自问题并添加为答案:

NotificationCompat.Builder mBuilder = 
                new NotificationCompat.Builder(this) 
                .setSmallIcon(R.drawable.logo)    
                .setContentTitle(getString(R.string.txt)) 
                .setContentText(getString(R.string.txt)); 

        mBuilder.setOngoing(true); 

        // Creates an explicit intent for an Activity in your app 
        Intent resultIntent = new Intent(this, Activity.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.from(this); 
        // Adds the back stack for the Intent (but not the Intent itself) 
        stackBuilder.addParentStack(Activity.class); 
        // Adds the Intent that starts the Activity to the top of the stack 
        stackBuilder.addNextIntent(resultIntent); 
        PendingIntent resultPendingIntent = PendingIntent.getActivity(this, 0, resultIntent, 0); 
        mBuilder.setContentIntent(resultPendingIntent); 
        NotificationManager mNotificationManager = 
            (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); 
        // mId allows you to update the notification later on. 
        mNotificationManager.notify(mId, mBuilder.getNotification());
于 2013-12-20T18:59:36.990 回答