恢复通知的后台活动

编程入门 行业动态 更新时间:2024-10-12 01:22:46
本文介绍了恢复通知的后台活动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我已经在这里阅读了一些答案,我认为我有实现我的结果所需要的东西,但是我需要一些帮助.

I've read some answers here, I think I have what I need in order to achieve my result, but I need some help.

我的应用程序会在特定条件下启动通知,我需要我的应用程序具有以下行为:

My app launches an notification on specific conditions, and I need my app to behave as follow:

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

  • if there is an instance of my main activity running in background I need to make it to the foreground (I found this on the site: intent.setFlags(FLAG_ACTIVITY_REORDER_TO_FRONT);, so I think this point is solved;

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

if there isn't any activity of the app running in background I need to start the app from the beginning (and this can be achieved starting the launcher activity of the app.);

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

My question is: how can I make the app search for any istance of itself running in background? Because the activity that I need to reorder to front with the Intent flag is different from the launcher activity.

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

The notification is handled by a service that check periodically some infos from the internet.

感谢您的帮助.

推荐答案

您需要的只是一个简单的Activity,它决定要做什么.这是一个示例:

What you need is just a simple Activity that decides what to do. Here is an example:

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

Set up your notification to start this activity. Make sure that in the manifest the task affinity of this activity is the same as the task affinity of the other activities in your application (by default it is, if you haven't explicitly set android:taskAffinity).

更多推荐

恢复通知的后台活动

本文发布于:2023-11-27 10:54:02,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1637779.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:后台   通知

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!