【问题标题】:Get back to previous Activity when Ongoing notification is clicked单击正在进行的通知时返回上一个活动
【发布时间】:2016-06-29 08:15:52
【问题描述】:

我正在进行一项包含一些 EditText 和 正在进行的通知的活动。

填写 EditTexts 后,我按 Home 按钮返回主屏幕(我的应用程序在后台运行)。当我点击正在进行的通知时,我只想用填充的 EditTexts 返回我的活动(而不是创建新的活动)。

我试过了

How should i do from notification back to activity without new intent

还有这个

Notification click: activity already open

它们根本不起作用!!!

下面是我的代码sn-p

ProtocolMonitorActivity.java

    Intent resultIntent = new Intent(this, ProtocolMonitorActivity.class);

    TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
    stackBuilder.addParentStack(ProtocolMonitorActivity.class);
    stackBuilder.addNextIntent(resultIntent);

    PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0,
            PendingIntent.FLAG_UPDATE_CURRENT);

    NotificationCompat.Builder notiBuilder = new NotificationCompat.Builder(
            this).setSmallIcon(R.drawable.noti_icon)
            .setContentTitle("Protocol Monitor App")
            .setContentText("Service is running")
            .setOngoing(true);
    notiBuilder.setContentIntent(resultPendingIntent);

    notiManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    notiManager.notify(notiId, notiBuilder.build());

清单

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >

    <activity
        android:name=".ProtocolMonitorActivity"
        android:label="@string/app_name"
        android:process="com.android.phone"
        android:launchMode="singleTop"
        android:screenOrientation="portrait" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
            <category android:name="android.intent.category.DEVELOPMENT_PREFERENCE" />
        </intent-filter>

        <meta-data
            android:name="android.support.PARENT_ACTIVITY"
            android:value=".ProtocolMonitorActivity" />

    </activity>
</application>

有人对此有任何想法吗?

非常感谢!!!

【问题讨论】:

    标签: android android-activity notifications builder


    【解决方案1】:

    您无法使用TaskStackBuilder 执行此操作。 TaskStackBuilder 的行为是它总是清除任务(重新创建任何活动)。

    您只需要一个“启动意图”即可将您的任务以它恰好处于的任何状态带到前台。有两种方法可以做到这一点:

    变体 1:

    final Intent notificationIntent = new Intent(this, ProtocolMonitorActivity.class);
    notificationIntent.setAction(Intent.ACTION_MAIN);
    notificationIntent.addCategory(Intent.CATEGORY_LAUNCHER);
    notificationIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    

    变体 2:

    final Intent notificationIntent = 
            PackageManager.getLaunchIntentForPackage(getPackageName());
    

    然后做:

    PendingIntent resultPendingIntent = PendingIntent.getActivity(this, 0,
            notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
    
    NotificationCompat.Builder notiBuilder = new NotificationCompat.Builder(
            this).setSmallIcon(R.drawable.noti_icon)
            .setContentTitle("Protocol Monitor App")
            .setContentText("Service is running")
            .setOngoing(true);
    notiBuilder.setContentIntent(resultPendingIntent);
    
    notiManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    notiManager.notify(notiId, notiBuilder.build());
    

    【讨论】:

    • @Viet-AnhDinh 嗨,同样的问题困扰着我。这种方式仅在您关闭应用程序时有效。但是当活动已经打开时,它将重新开始。没有 SingleTask 模式如何解决?
    • @AnswerZhao 这不是真的。该解决方案只是将现有任务以它所处的任何状态带回前台。它不会再次启动任何东西。如果您有特定问题,您应该发布一个新问题,而不是对现有答案发表评论。这会让你得到更多的关注。
    【解决方案2】:

    只需插入这一行

    resultIntent.setAction(Intent.ACTION_MAIN);
    

    【讨论】:

    • 谢谢!我需要先删除 TaskStackBuilder。
    【解决方案3】:

    在所需活动内的清单中的活动标签中设置 android:launchMode="singleTop"。

    【讨论】:

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