安卓服务的onCreate未被调用

5

我希望以静态方式启动一个服务。因此,从我的活动中调用以下代码:

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"/>

有什么想法,为什么服务没有启动?
2个回答

12

除了你没有发布展示startService()的代码外,看起来你在清单文件中的Service的包名称与你的SpeechActivationService类不匹配(假设你发布的代码链接是你项目中真正的SpeechActivationService类,而不仅仅是你复制的类)。

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

9

你的makeStartService()只是为你创建一个Intent。你似乎并没有实际上将该意图发射出去以启动服务。请尝试像这样进行操作:

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

请注意,如果 this.getApplicationContext() 能够正常工作,那么你很可能已经在一个 Context 对象内部,因此只使用 this 应该也可以。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接