Android 应用程序即服务,无需活动

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24307412/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 08:00:25  来源:igfitidea点击:

Android application as a service without activity

androidandroid-servicegoogle-play-servicesbackground-service

提问by iLyas

I am making a set of apps and I have pretty much the same background service for all of them.

我正在制作一组应用程序,并且为所有应用程序提供几乎相同的后台服务。

I'm trying to make an app that has only this Service. so I don't repeat it in all of them, but the thing is don't need any Activity. because there is no UI needed for it, and so the user can't close it except if they stop the Service.

我正在尝试制作一个只有 this 的应用程序Service。所以我不会在所有这些中重复它,但事情是不需要任何Activity. 因为它不需要 UI,所以用户不能关闭它,除非他们停止Service.

I tried to remove the Activity, but then the app doesn't run or start. My question is: can I make an app exactly like Google Play Servicesso other apps can use its Service.

我试图删除Activity,但该应用程序无法运行或启动。 我的问题是:我可以制作一个完全一样的应用程序,Google Play Services以便其他应用程序可以使用它的Service.

If yes than a snippet or a sample would be very welcome.

如果是,那么非常欢迎使用片段或示例。

回答by G. Blake Meike

Sure! No reason you cannot have an application with only a service. ...and no need to get into AIDL unless you want to.

当然!没有理由您不能拥有仅包含服务的应用程序。...除非您愿意,否则无需进入 AIDL。

The problem is, how to make the application run. When you create an application with an Activity, you add an Intent filter, in the manifest, that makes the activity startable from the Launcher. If there's no activity, you'll have to find another way to start it.

问题是,如何使应用程序运行。当您使用 Activity 创建应用程序时,您会在清单中添加一个 Intent 过滤器,使 Activity 可从启动器启动。如果没有活动,您将不得不寻找另一种方式来启动它。

It is easy to do, though. Just fire an intent from one of your other programs, like this:

不过,这很容易做到。只需从您的其他程序之一中触发一个意图,如下所示:

startService(new Intent("my.service.intent"));

... where the service is registered your manifest, like this:

...服务在您的清单中注册的地方,如下所示:

        <service android:name=".SomeService" >
          <intent-filter>
            <action android:name="my.service.intent"/>
          </intent-filter>

You could use that intent to pass Parcelable parameters to the service, and the service can reply by broadcasting intents back.

您可以使用该意图将 Parcelable 参数传递给服务,服务可以通过广播意图回复。

Of course startService and broadcastIntent are a bit clunky if you really need a complex API between applications and your service. If you need something richer, you willwant to look into AIDL and a Bound Service.

当然 startService 和 broadcastIntent 有点笨拙,如果您真的需要应用程序和服务之间的复杂 API。如果您需要更丰富的东西,您需要研究 AIDL 和绑定服务。

Edited to add Intent Filter

编辑以添加意图过滤器

回答by Mattias Backman

Yes, I think you're looking for AIDL. Have a look at this thread?

是的,我认为您正在寻找 AIDL。看看这个线程?

https://groups.google.com/forum/#!topic/android-developers/LuWPZjPZ0sk

https://groups.google.com/forum/#!topic/android-developers/LuWPZjPZ0sk