繁体   English   中英

奇怪的Android服务无法启动

[英]Weird Android Service not starting

我的服务没有启动有一个奇怪的问题。 我将清单文件与服务一起使用,并对其进行了调用。 但是仍然没有打开。

<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 

我想知道我在这里想念的是什么。

更改

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

android:name="com.taxeeta.ForHire"

或服务是否在根软件包上

android:name=".ForHire"

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

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

<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

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

一切尽在您的清单上。

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

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

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

(请注意,已删除. [点])。 还要确保service是您的application元素的子元素,这是Android OS识别该服务所必需的。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM