onCreate of android service not called

橙三吉。 提交于 2019-12-07 10:28:12

问题


I want to start a service in a static way. So from my activity I call

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

Here is the actual class that extends from service class http://dpaste.com/hold/928115/ As you can see there are several log points, e.g. in the onCreate method.

This is not logged. Only if I put log text in makeStartServiceIntent method it appears, however not in the onCreate method.

Here's the makeStartServiceIntent method:

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

In manifest file I have

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

Any ideas why the service is not started?


回答1:


Your makeStartService() just creates an Intent for you. You don't seem to actually be firing that intent off to start the service. Try like this

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

Note that if this.getApplicationContext() works you are likely already inside of a Context object so simply using this should work also.




回答2:


Aside from you not posting code showing startService(), it looks like the package name of your Service in the manifest doesn't match your SpeechActivationService class (assuming the code link you posted is the actual SpeechActivationService class in your project, and not just a class you copied from).

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


来源:https://stackoverflow.com/questions/14877442/oncreate-of-android-service-not-called

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!