2013-02-14 59 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"/>