从另一个服务启动服务

Vis*_*fre 7 service android

我正在尝试从其他服务启动服务.但想知道出了什么问题.代码就像

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);
    }
}
Run Code Online (Sandbox Code Playgroud)

而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();
    }
}
Run Code Online (Sandbox Code Playgroud)

我在Service1中获得了Toast"Hello",但没有从Service2获得Toast"Service Started"

Fre*_*ger 14

而不是使用getApplicationContext()你应该使用Service1.thisgetBaseContext().您是否已在AndroidManifest中声明了Service2?

  • 哦......清单......我应该为此付出沉重的代价...无论如何,非常感谢:) (14认同)