恢复通知的后台活动 [英] Resume background activity on notification

查看:94
本文介绍了恢复通知的后台活动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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).

这篇关于恢复通知的后台活动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆