服务和启动/重新启动活动 [英] Service and launching/re-launching Activity

查看:175
本文介绍了服务和启动/重新启动活动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个运行的应用程序,该应用程序具有基本的UI和可以接收紧急情况的服务.如果用户在用户界面中并按home或Back,则会显示一条通知,并且该服务将继续运行.如果服务收到紧急事件,我想在UI中打开活动".为此,我必须在服务中使用带有Intent标志FLAG_ACTIVITY_NEW_TASK的startActivity.

I am developing an app which runs has a basic UI and service which can receive emergencies'. If the user is in the UI and presses home or Back a notification appears and the service continues to run. If an emergency is received by the service I want to open the Activity into the UI. To do this I have to use startActivity in the service with the Intent flag FLAG_ACTIVITY_NEW_TASK.

但这意味着,如果UI仍在后台运行,则不会重新打开它.从那时起,我的许多UI交互似乎发生在旧的,隐藏的UI上,而不是在打开的新UI上.另外,如果我关闭当前的UI,有时会在后面隐藏旧的UI.

But this means that, if the UI is still running in the background, it doesn't get re-opened. Many of my UI interactions from that point onward seem to happen on the old, hidden UI not the new UI which is open. Also If i close the current UI I sometimes have the old UI behind it.

我希望从服务中可以将旧的ui放到最前面或打开一个新的ui,具体取决于旧的UI是否仍在运行.

I would like that from a service I can either bring the old ui to the front OR open a new one, depending whether or not the old UI is still running.

我曾尝试同时使用FLAG_ACTIVITY_CLEAR_TASK和FLAG_ACTIVITY_NEW_TASK来清除UI的任何其他实例,然后在新任务中打开UI,但是CLEAR_TASK是API 11或更高版本;我的应用程序支持10.

I have tried using FLAG_ACTIVITY_CLEAR_TASK and FLAG_ACTIVITY_NEW_TASK together to both clear any other instances of the UI then open the UI in a new task but CLEAR_TASK is API 11 and over; my app supports 10.

在上述方面的任何帮助都将受到赞赏.

Any help on the above is appreciated.

推荐答案

我要做的是在Activity的onResume中执行bindService(这样您将获得Service实例),然后添加一个Service实例的方法,该方法从Activity接收Handler实例.在onPause中,将Service上的Handler设置为null.这样,Service就会知道(通过检查其内部Handler实例)是否已经有可用的UI.如果存在,它将向Handler发送Message发生了事情.如果没有Handler,它将启动新的UI.

What I've done with this is do a bindService in the Activity's onResume (so you get the Service instance) and then add a method to the Service instance that receives a Handler instance from the Activity. In onPause you set the Handler on the Service to null. This way the Service knows (by checking its internal Handler instance) if there is already a UI available. If there is, it sends the Handler a Message that things happened. If there is no Handler, it launches a new UI.

这篇关于服务和启动/重新启动活动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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