从另一个服务启动服务

时间:2015-03-22 09:25:06

标签: android service

我试图从其他服务启动服务。但想知道出了什么问题。代码就像

class Service1 extends GCMBaseIntentService {

    @Override
    protected void onMessage(Context arg0, Intent intent) {
        Toast.makeText(getApplicationContext(), "Hello", Toast.LENGTH_LONG).show();
        Intent service = new Intent(getApplicationContext(), Service2.class);
        startService(service);
    }
}

Service2是

class Service2 extends Service {

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        // TODO Auto-generated method stub
        Toast.makeText(getApplicationContext(), "Service Started", Toast.LENGTH_LONG).show();
    }
}

我得到了Toast"你好"在Service1但未获得Toast" Service Started"来自Service2

1 个答案:

答案 0 :(得分:12)

您应该使用getApplicationContext()Service1.this,而不是getBaseContext()。您是否已在AndroidManifest中声明了Service2?