根据通知恢复后台活动



我在这里读到了一些答案,我认为我有实现目标所需的东西,但我需要一些帮助。

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

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

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

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

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

谢谢你的帮助。

您只需要一个简单的活动来决定要做什么

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,则为)。

最新更新