【问题标题】:Resume an activity when clicked on a notification单击通知时恢复活动
【发布时间】:2013-10-03 15:39:22
【问题描述】:

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

这是用于创建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 的代码。
  • 请更详细地解释正在发生的事情以及您希望发生的事情。你的问题不清楚。
  • 我用这个解决了它:stackoverflow.com/questions/3305088/…

标签: android android-activity android-notifications


【解决方案1】:

你需要在你的 PendingIntent 中设置标志...比如 FLAG_UPDATE_CURRENT。

这就是全部。 http://developer.android.com/reference/android/app/PendingIntent.html

编辑1:我误解了这个问题。

以下是存在相同问题但已解决的主题的链接:

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

请阅读以上答案以获得完整的解决方案,如果可行,请告诉我。

【讨论】:

    【解决方案2】:

    试试这个。

    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);
    【解决方案3】:

    将此行添加到应用清单文件中的相应活动中。

    android:launchMode="singleTask"

    例如:

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

    【讨论】:

    • 谢谢!!!!终于解决了我的“音乐播放器”从通知启动时播放同一首歌曲的问题。
    【解决方案4】:

    在进行大量搜索后对我真正有用的唯一解决方案是执行以下操作:

    在这里,您只是启动了保持当前堆栈的应用程序:

    //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 方法有什么问题。
    【解决方案5】:

    如果上述解决方案不起作用,请尝试将 androidManifest.xml 文件中的活动启动模式从标准更改为单任务。

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

    这将防止 Activity 有多个实例。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-01-24
      相关资源
      最近更新 更多