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

任何想法为什么服务没有启动?

4

2 回答 2

12

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

<service android:name="com.mkyong.android.SpeechActivationService"/>
于 2013-02-14T15:06:10.510 回答
9

makeStartService()只是为你创造了一个意图。您似乎实际上并没有启动该服务的意图。像这样试试

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

请注意,如果this.getApplicationContext()有效,您可能已经在Context对象内部,因此简单地使用也this 应该有效。

于 2013-02-14T14:54:27.960 回答