简体   繁体   English

单击通知时恢复活动

[英]Resume an activity when clicked on a notification

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:这是用于创建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());

You need to set flags in your PendingIntent's ...like FLAG_UPDATE_CURRENT.你需要在你的 PendingIntent 中设置标志......比如 FLAG_UPDATE_CURRENT。

Here is all on it.这就是它的全部。 http://developer.android.com/reference/android/app/PendingIntent.html http://developer.android.com/reference/android/app/PendingIntent.html

Edit 1: I misunderstood the question.编辑1:我误解了这个问题。

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 Android:从以前的位置恢复应用程序

Please read the above answers for a full solution and let me know if it works.请阅读上述答案以获得完整的解决方案,并让我知道它是否有效。

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

Add this line to the corresponding activity in manifest file of your app.将此行添加到应用程序清单文件中的相应活动。

android:launchMode="singleTask" android:launchMode="singleTask"

eg:例如:

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

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

If the aforementioned solution didn't work, try to change the activity launch mode in your androidManifest.xml file from standard to singleTask.如果上述解决方案不起作用,请尝试将 androidManifest.xml 文件中的活动启动模式从标准更改为 singleTask。

<activity>
...
android:launchMode="singleTask
...
</activity>

This will prevent the activity from having multiple instances.这将防止活动具有多个实例。

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

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