0

我的服务没有启动有一个奇怪的问题。我有我的清单文件和服务,并调用了它。但它仍然没有打开。

<service
android:name=".com.taxeeta.ForHire"
android:enabled="true" />

调用意图

Intent serviceIntent = new Intent();
serviceIntent.setAction("com.taxeeta.ForHire");
startService(serviceIntent);

服务

public class ForHire extends Service 

我想知道我在这里缺少什么。

4

4 回答 4

3

改变

android:name=".com.taxeeta.ForHire"

android:name="com.taxeeta.ForHire"

或者如果服务在根包上

android:name=".ForHire"

此外,您应该使用Intent.setClass( )而不是 setAction,因为您没有为您的服务声明 IntentFilter 并且您很可能尝试使用显式意图。

于 2013-01-21T17:18:20.980 回答
2

当您在 Manifest 文件中声明服务时,请像这样使用。

<service android:name=".ForHire">
<intent-filter>
     <action android:name="com.taxeeta.ForHire" />
</intent-filter>
</service> 

& 呼叫服务 像这样。

Intent serviceIntent = new Intent();
serviceIntent.setAction("com.taxeeta.ForHire");
startService(serviceIntent);

有关服务的更多信息,请参阅此文档 http://developer.android.com/guide/components/services.html

于 2013-01-21T17:24:31.017 回答
2

只需调用startService(new Intent(getApplicationContext(),ForHire.class));

你的清单中的每件事都很好。

无需根据您的清单设置操作。

于 2013-01-21T17:51:11.433 回答
1

您的清单中的服务声明有问题。将其更改为:

<service android:name="com.taxeeta.ForHire" />

(注意.[dot] 已移除)。还要确保service是您元素的子元素application,这是 Android 操作系统识别服务所必需的。

于 2013-01-21T17:24:26.863 回答