gpt4 book ai didi

android - 根据通知恢复后台 Activity

转载 作者:行者123 更新时间:2023-11-29 20:14:07 25 4
gpt4 key购买 nike

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

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

  • 如果我的主要 Activity 有一个实例在后台运行,我需要将其置于前台(我在网站上找到了这个:intent.setFlags(FLAG_ACTIVITY_REORDER_TO_FRONT);,所以我觉得这点已经解决了;

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

    <

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

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

感谢您的帮助。

最佳答案

您需要的只是一个决定要做什么的简单 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 。确保 list 中此 Activity 的任务亲和性与应用程序中其他 Activity 的任务亲和性相同(默认情况下,如果您没有显式设置 android:taskAffinity) .

关于android - 根据通知恢复后台 Activity ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34288301/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com