简体   繁体   中英

Resuming Activity from notification created by service

I have an application which starts a service running on the background. This service periodically issues a constant notification. I would like to be able to press that notification and resume the last activity of the application.

I am creating my notifications as follows:

public static  void startNotification(Service service,  String message) {
    SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(service);
    if(prefs.getBoolean("pref_NotificationDisplayed", true)){
        // Creates an Ongoing Notification
        NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(service.getApplicationContext()).setSmallIcon(R.drawable.ic_launcher).setContentTitle("Title").setContentText(message);

        Intent toLaunch = new Intent(service.getApplicationContext(),MainActivity.class);
        toLaunch.setAction("android.intent.action.MAIN");
        toLaunch.addCategory("android.intent.category.LAUNCHER");
        PendingIntent intentBack = PendingIntent.getActivity(service.getApplicationContext(),   0,toLaunch, PendingIntent.FLAG_UPDATE_CURRENT);

        //PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
        mBuilder.setContentIntent(intentBack);
        NotificationManager mNotificationManager = (NotificationManager) service.getSystemService(Context.NOTIFICATION_SERVICE);

        // Send Notification
        Notification primaryNotification = mBuilder.build();
        primaryNotification.flags = Notification.FLAG_ONGOING_EVENT;
        mNotificationManager.notify(10001,primaryNotification);
    }

}

I have tried the solutions from here and here with no luck.

Every time I press the notification a new activity is started up rather than resuming the old activity. Is this because I am issuing the activity from a service ? or am I making an obvious error above ?

Thanks for the help.

are you try

toLaunch.setFlags(Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);

inyour intent. check above flag i dont have tried...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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