根据通知恢复后台 activity

Resume background activity on notification

我在这里阅读了一些答案,我想我已经有了实现我的结果所需的一切,但我需要一些帮助。

我的应用在特定条件下启动通知,我需要我的应用按以下方式运行:

我的问题是:如何让应用程序在后台搜索自身的任何距离 运行?因为我需要使用 Intent 标志重新排序的 activity 与启动器 activity.

不同

该通知由定期检查来自 Internet 的一些信息的服务处理。

感谢您的帮助。

您需要的只是一个简单的 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();
    }
}

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