如何将回调到各种活动,从机器人库 [英] how to callback into various activities from a android library
问题描述
我写了一个库为Android,在初始化我注册回调的位置更新,订阅状态的变化,库状态变化和其他各种更新库...
本来,我想所有这些更新回调到控制器
在我的Android用户界面的应用程序,然后再用于控制器推出活动A,传递消息从控制器到活动A等。或启动活动B中注册一个订阅和转发消息给本等
然而,似乎没有办法做到这一点 - 因为每个活动是孤立?除非我弄错了?
I’ve written a library for Android, upon initialising the library I register for call backs for location updates, subscription status changes, library status changes and various other updates...
Originally, I wanted all these updates to call back to a controller
in my android UI app, and then for the controller to launch activity A, pass on the messages from the controller to activity A and so on. Or launch activity B for signing up for a subscription and forward messages to this etc
However, it appears that there isn’t a way to achieve this - Because each activity is in isolation? Unless I’m mistaken?
那么,什么是我选择这里?这听起来像我要么必须使用一个活动的整个应用程序和交换用户界面的其中寻找到它后不会出现是要走的路?
So what are my options here? It sounds like I either have to use one activity for the whole app and swap the UI’s which after looking into it doesn’t appear to be the way to go?
我曾尝试子类应用程序,它的工作,给了我从活动中获得我的图书馆 - 但我想它周围的其他方法。这可能吗?将包裹图书馆的服务达到我想要做什么?
I did try to subclass Application, which worked and gave me access to my library from an activity – but I want it the other way around. Is this possible? would wrapping the library in a service achieve what I want to do?
推荐答案
您可以使用广播,并将其从媒体库发送。您的每一个活动都必须注册一个的BroadcastReceiver
在他们 onResume()
方法和注销在他们的的onPause()
方法。当你使用一个公共基类的活动,这将是比较容易的。然后,您可以从您的图书馆的任何活动是当前活动发送命令。
You could use broadcasts and send them from your library. Each of your activities would have to register a BroadcastReceiver
in their onResume()
method and unregister it in their onPause()
method. This would be comparatively easy when you use a common base class for your activities. You could then send commands from your library to whatever activity is currently active.
这篇关于如何将回调到各种活动,从机器人库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!