簡體   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