2013-02-14 57 views
3

我想以靜態方式啓動服務。所以從我的活動我打電話onCreate android服務不叫

SpeechActivationService.makeStartServiceIntent(
       this.getApplicationContext(), 
       "WordActivator"); 

下面是實際的類,從服務類http://dpaste.com/hold/928115/擴展正如你可以看到有幾個日誌點,例如在onCreate方法中。

這不是記錄。只有當我在makeStartServiceIntent方法中放置日誌文本時,它纔會出現,但不在onCreate方法中。

這裏的makeStartServiceIntent方法:

public static Intent makeStartServiceIntent(Context context, 
     String activationType) {   
    Intent i = new Intent(context, SpeechActivationService.class); 
    i.putExtra(ACTIVATION_TYPE_INTENT_KEY, activationType); 
    return i; 
} 

清單文件我有

<service android:name="root.gast.speech.activation.SpeechActivationService"/> 

爲什麼服務沒有啓動任何想法?

回答

8

您的makeStartService()只是爲您創建一個意向。你似乎並沒有真正啓動該服務的意圖。試着這樣

Intent i = SpeechActivationService.makeStartServiceIntent(this,"WordActivator"); 
startService(i); 

注意,如果this.getApplicationContext()運作的,你很可能已經是Context對象的內部,從而簡單地使用this應該工作也。

+0

是的,我要問他是否真的用Intent做了什麼...... – dmon 2013-02-14 14:55:45

10

除了你不張貼出startService()代碼,它看起來像你的Service在清單的包名不符合您的SpeechActivationService類(假設你發佈的是實際SpeechActivationService類項目中的代碼鏈接,不只是從複製的課程)。

<service android:name="com.mkyong.android.SpeechActivationService"/>