Android应用程序是不活动的服务应用程序、Android

2023-09-06 09:29:41 作者:4.北徊.

我提出了一套应用程序,我有pretty的多为所有这些相同的后台服务。

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.

我试图删除活动,但随后的应用程序不运行或启动。 我的问题是:我可以让一个应用程序酷似谷歌播放服务,以便其他应用程序可以使用它的服务

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 Services so other apps can use its Service.

如果是不是一个代码段或样品将是非常欢迎的。

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

推荐答案

当然!没有原因,你不能有一个应用程序,只有一个服务。 ...没必要进入AIDL,除非你想。

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

现在的问题是,如何让应用程序运行。当你创建与活动的应用程序,添加一个意图过滤器,在清单中,这使得该活动从启动启动的。如果没有活动,你将不得不寻找另一种方式来启动它。

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.

这是很容易做到,但。刚刚火的意图,从您的其他程序中的一个,像这样的:

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>

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

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

当然startService和broadcastIntent是有点笨重,如果你确实需要的应用程序和服务之间的复杂的API。如果你需要的东西更丰富,你的将会的想看看AIDL和绑定服务。

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 will want to look into AIDL and a Bound Service.

编辑补充意图过滤器