Resume an activity when clicked on a notification

2019-01-19 00:34发布

问题:

I've made an app which manage sms, I've created the notifications but when I click on them it starts another activity, I would like to know how to check if an activity has been stopped and resume it.

Here is the code used to create the pendingintent:

private void createNotification(SmsMessage sms, Context context){

    final NotificationManager nm = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);

    String contentTitle = "";


    // construct the Notification object.
        final NotificationCompat.Builder  builder = new NotificationCompat.Builder(context)
        .setContentTitle(contentTitle)
         .setContentText(sms.getMessageBody())
         .setSmallIcon(R.drawable.ic_launcher)
         .setLargeIcon(getIconBitmap())
         .setNumber(nmessages);

        builder.setAutoCancel(true);

        //(R.drawable.stat_sample, tickerText,
          //      System.currentTimeMillis());

        // Set the info for the views that show in the notification panel.
        //notif.setLatestEventInfo(this, from, message, contentIntent);
        /*
        // On tablets, the ticker shows the sender, the first line of the message,
        // the photo of the person and the app icon.  For our sample, we just show
        // the same icon twice.  If there is no sender, just pass an array of 1 Bitmap.
        notif.tickerTitle = from;
        notif.tickerSubtitle = message;
        notif.tickerIcons = new Bitmap[2];
        notif.tickerIcons[0] = getIconBitmap();;
        notif.tickerIcons[1] = getIconBitmap();;
        */

     // Creates an explicit intent for an Activity in your app
        Intent resultIntent = new Intent(context, BasicActivity.class);

        resultIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
        // Because clicking the notification opens a new ("special") activity, there's
        // no need to create an artificial back stack.
        PendingIntent resultPendingIntent =
            PendingIntent.getActivity(
            context,
            0,
            resultIntent,
            PendingIntent.FLAG_UPDATE_CURRENT
        );


       // Ritardo in millisecondi



     builder.setContentIntent(resultPendingIntent);

     nm.notify(R.drawable.ic_drawer, builder.build());

回答1:

You need to set flags in your PendingIntent's ...like FLAG_UPDATE_CURRENT.

Here is all on it. http://developer.android.com/reference/android/app/PendingIntent.html

Edit 1: I misunderstood the question.

Here are links to topics that had the same issue but are resolved:

resuming an activity from a notification

Notification Resume Activity

Intent to resume a previously paused activity (Called from a Notification)

Android: resume app from previous position

Please read the above answers for a full solution and let me know if it works.



回答2:

Try with this.

NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(
                    mContext).setSmallIcon(R.drawable.ic_launcher)
                    .setContentTitle(mContext.getString(R.string.notif_title))
                    .setContentText(mContext.getString(R.string.notif_msg));
            mBuilder.setAutoCancel(true);

        // Set notification sound
        Uri alarmSound = RingtoneManager
                .getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
        mBuilder.setSound(alarmSound);

        Intent resultIntent = mActivity.getIntent();
        resultIntent.addCategory(Intent.CATEGORY_LAUNCHER);
        resultIntent.setAction(Intent.ACTION_MAIN);

        PendingIntent pendingIntent = PendingIntent.getActivity(mContext, 0, resultIntent, PendingIntent.FLAG_UPDATE_CURRENT);
        mBuilder.setContentIntent(pendingIntent);
        NotificationManager mNotificationManager = (NotificationManager) mContext
                .getSystemService(Context.NOTIFICATION_SERVICE);
        // mId allows you to update the notification later on.
        mNotificationManager.notify(mId, mBuilder.build());


回答3:

The only solution that actually worked for me after doing a lot of search is to do the following :

here you are simply launching of the application keeping the current stack:

//here you specify the notification properties
NotificationCompat.Builder builder = new NotificationCompat.Builder(this).set...(...).set...(..);

//specifying an action and its category to be triggered once clicked on the notification
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);

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


回答4:

Add this line to the corresponding activity in manifest file of your app.

android:launchMode="singleTask"

eg:

<activity
android:name=".Main_Activity"
android:label="@string/title_main_activity"
android:theme="@style/AppTheme.NoActionBar"
android:launchMode="singleTask" />