简体   繁体   English

onCreate没有调用android服务

[英]onCreate of android service not called

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, eg in the onCreate method. 以下是从服务类http://dpaste.com/hold/928115/扩展的实际类。正如您所看到的,有几个日志点,例如在onCreate方法中。

This is not logged. 这不会被记录。 Only if I put log text in makeStartServiceIntent method it appears, however not in the onCreate method. 只有当我在makeStartServiceIntent方法中放入日志文本时,它才出现,但不在onCreate方法中。

Here's the makeStartServiceIntent method: 这是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;
}

In manifest file I have 在清单文件中我有

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

Any ideas why the service is not started? 有没有为什么服务没有开始的想法?

除了您没有发布显示startService()代码之外,看起来清单中Service的包名称与您的SpeechActivationService类不匹配( 假设您发布的代码链接是项目中的实际SpeechActivationService类,而不仅仅是你复制的课程 )。

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

Your makeStartService() just creates an Intent for you. 你的makeStartService()只是为你创建一个Intent。 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. 请注意,如果this.getApplicationContext()有效,则您可能已经在Context对象中,因此使用this应该可以正常工作。

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

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