简体   繁体   English

Android通知恢复活动

[英]Android notification Resume Activity

I'm trying to make a notification when users pause my app. 当用户暂停我的应用程序时,我正在尝试发出通知。 So to make it easier, users can go quickly to the application using notificaction. 因此,为了简化操作,用户可以使用通知功能快速进入应用程序。 This is the code i'm using. 这是我正在使用的代码。 It works for all versions before android 4 and i don't know which is the problem 它适用于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());

So when i press the notification in android 4.0 and higher, the activity is created again instead of resume. 因此,当我在android 4.0及更高版本中按通知时,将再次创建活动,而不是恢复。 any help please, i can't make it work. 请任何帮助,我不能使其工作。

EDIT ( forget about manifest singletop ) 编辑(忘记清单清单)

android:launchMode="singleTop" same result, not working... android:launchMode="singleTop"结果相同,无法正常使用...

My activity contains a map. 我的活动包含一张地图。 I'm using the new version of google maps. 我正在使用新版的Google地图。 v2. V2。

I just tried PendingIntent.FLAG_CANCEL_CURRENT and seems to work for me 我刚刚尝试了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);
}

The only solution that actually worked for me after doing a lot of search is to do the following : 经过大量搜索之后,对我真正有效的唯一解决方案是执行以下操作:

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());

this opens your current activiy without creating another one ! 这将打开您当前的活动,而无需创建另一个活动!

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

SOLUTION provided by @Patrick taken from the question and added as answer: @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());

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM