当点击通知时恢复一个活动

15

我制作了一个管理短信的应用程序,我创建了通知,但是当我点击它们时,它会启动另一个活动。我想知道如何检查活动是否已停止并恢复它。

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

展示你的Intent / PendingIntent代码。 - Steve Benett
1
请更详细地解释正在发生的事情以及您希望发生的事情。您的问题不够清晰明了。 - David Wasser
我用这个解决了:https://dev59.com/J3A75IYBdhLWcg3wboYz#39482464 - TharakaNirmana
5个回答

13
您需要在PendingIntent中设置标志,例如FLAG_UPDATE_CURRENT。
以下是有关此问题的所有内容。 http://developer.android.com/reference/android/app/PendingIntent.html 编辑1:我误解了问题。
以下是具有相同问题但已解决的主题链接: 从通知中恢复活动 Notification Resume Activity 意图以恢复先前暂停的活动(从通知调用) Android:从之前的位置恢复应用程序 请阅读上面的答案以获取完整的解决方案,并告诉我是否有效。

6

在你的应用程序的清单文件中,将此行添加到相应的活动中。

android:launchMode="singleTask"

例如:

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

谢谢!!!终于解决了我的“音乐播放器”从通知栏启动时播放同一首歌的问题。 - Alvindrakes

4

试试这个。

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

这也可以正常工作 Intent resultIntent = new Intent(mContext, ActivityWebView.class); resultIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP); - Thushara

3

在进行了大量搜索后,我发现唯一实际可行的解决方案是执行以下操作:

在保留当前堆栈的情况下,启动应用程序:

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

请简要解释一下您的解决方案如何工作,以及OP方法的问题所在。 - KKK

0
如果上述解决方案无效,请尝试在androidManifest.xml文件中将活动启动模式从standard更改为singleTask。
<activity>
...
android:launchMode="singleTask
...
</activity>

这将防止该活动具有多个实例。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接