简体   繁体   English

我应如何使我的服务可被我的Android应用程序中的其他活动访问?

[英]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.

相关问题 我如何在后台关闭我的 android 应用程序的所有活动 - How i can close all activities of my android app in background 如何让我的应用程序可从任何其他应用程序(如 Google 翻译应用程序)访问? - How to make my app accessible from any other app like Google Translate app? 如何将我的应用程序数据保存在我的android应用程序中? - How should i save my app data in my android app? 如何使我的 SpringBoot 应用程序可以通过我的公共 IP 地址访问? - How can I make my SpringBoot app accessible with my public IP address? 如何让我的所有活动都能够处理来自服务的广播? - How can I make all of my activities able to handle a broadcast from a service? 如何在Android应用程序中的任何位置使实例可用或可访问 - How do I make an instance available or accessible from anywhere in my android application 如何将我现有的 Android Studio 的两个活动转换为两个片段,以便我可以制作导航栏? - How do I convert my existing two Activities of Android Studio into two fragments so that I can make the Navigation Bar? 如何使我的Android应用程序的按钮时尚 - How can I make the button stylish for my android app 如何让我的Android应用生成随机数? - How do I make my Android app generate a random number? 我应该让我的服务器程序在Windows上作为Windows服务运行吗 - Should I make my server program run as a Windows Service on Windows
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM