简体   繁体   English

如何从android库回调到各种活动

[英]how to callback into various activities from a android library

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. 我已经为Android编写了一个库,初始化该库后,我会注册该回调以进行位置更新,订阅状态更改,库状态更改以及其他各种更新...最初,我希望所有这些更新都可以回调给controller我的Android UI应用程序,然后让控制器启动活动A,将消息从控制器传递给活动A,依此类推。 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? 还是启动活动B来注册订阅并将消息转发给该活动等等,但是似乎没有办法实现这一目标-因为每个活动都是孤立的? 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? 听起来好像我要么必须为整个应用程序使用一个活动,然后交换UI,但在研究完它之后,这似乎不是一种方法?

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. 我确实尝试对Application进行子类化,该类可以正常工作,并且使我可以通过活动访问我的库-但我希望反之亦然。 Is this possible? 这可能吗? would wrapping the library in a service achieve what I want to do? 将库包装到服务中会实现我想要的功能吗?

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. 您的每个活动都必须在其onResume()方法中注册一个BroadcastReceiver ,并在其onPause()方法中注销它。 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. 然后,您可以将命令从库发送到当前处于活动状态的任何活动。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM