【问题标题】:Resume background activity on notification在通知上恢复后台活动
【发布时间】:2015-12-15 11:40:59
【问题描述】:

我在这里阅读了一些答案,我想我有我需要的东西来实现我的结果,但我需要一些帮助。

我的应用在特定条件下启动通知,我需要我的应用表现如下:

  • 如果我的主要活动的一个实例在后台运行,我需要将它放到前台(我在网站上找到了这个:intent.setFlags(FLAG_ACTIVITY_REORDER_TO_FRONT);,所以我认为这一点已经解决了;

  • 如果应用程序没有任何活动在后台运行,我需要从头启动应用程序(这可以通过启动应用程序的启动器活动来实现。);

    李>

我的问题是:如何让应用程序搜索在后台运行的任何自身的 istance?因为我需要用 Intent 标志重新排序到前面的活动与启动器活动不同。

通知由定期检查来自互联网的一些信息的服务处理。

感谢您的帮助。

【问题讨论】:

    标签: android android-intent background push-notification android-service


    【解决方案1】:

    您需要的只是一个决定要做什么的简单 Activity。这是一个例子:

    public class NotificationActivity extends Activity {
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            // Check if the app was already running
            if (isTaskRoot()) {
                // App wasn't running, so start the app from the beginning
                Intent startIntent = new Intent(this, MyStartingActivity.class);
                startActivity(startIntent);
            } else {
                // App was already running, bring MainActivity to the top
                Intent reorderIntent = new Intent(this, MainActivity.class);
                reorderIntent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
                startActivity(reorderIntent);
            }
            // We are done now so just finish
            finish();
        }
    }
    

    设置您的通知以开始此活动。确保在清单中此活动的任务亲和性与您应用程序中其他活动的任务亲和性相同(默认情况下,如果您没有明确设置 android:taskAffinity) .

    【讨论】:

    • 哇,这是一个不错的解决方案。我不知道isTaskRoot 方法。非常有用。非常感谢,伙计。
    • 我的荣幸。很高兴能帮上忙。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-11-30
    • 1970-01-01
    • 2011-01-24
    • 1970-01-01
    相关资源
    最近更新 更多