[英]How should I make my service accessible to other activities in my Android app?
I just started learning Java/Android development and have used Retrofit to create a service that retrieves data from an API. 我刚刚开始学习Java / Android开发,并使用Retrofit创建了一个从API检索数据的服务。 I create this service in my MainActivity, but I want other activities to use this service. 我在MainActivity中创建了此服务,但我希望其他活动使用此服务。 For example, if the MainActivity is a search that calls service.search(query) and displays the results, I would want to pass that service to an InfoActivity when the user selects a result so that it can make API calls itself and so on. 例如,如果MainActivity是一个调用service.search(query)并显示结果的搜索,那么我希望在用户选择结果时将该服务传递给InfoActivity,以便它可以自行调用API等。
However, I'm confused as to how I should do this. 但是,我对应该如何做感到困惑。 Intents seem mainly suitable for primitive objects, I can't seem to serialize Retrofit services either (java.io.NotSerializableException: retrofit2.Retrofit) to put it into an intent. 意图似乎主要适用于原始对象,我似乎也无法序列化Retrofit服务(java.io.NotSerializableException:retrofit2.Retrofit)以将其放入意图中。 I'm not sure if making the service global is the right thing to do either because I've read that Android will sometimes reset them without warning. 我不确定将服务全局化是否是正确的选择,因为我已阅读到Android有时会在没有警告的情况下将其重置。
Could someone point me into the right direction of how I should do this? 有人能指出我正确的方向吗?
<service android:name=".MyService" android:exported="true"/>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.