Android应用程序关闭后如何结束服务?

时间:2014-05-12 15:43:37

标签: android android-service

我了解当您致电Intent启动服务时,服务会一直持续到明确结束。

我希望该服务能够持续多项活动。当应用程序的所有活动都被销毁时,我该如何结束它?

或者是否会自动销毁?

1 个答案:

答案 0 :(得分:2)

在主Activity中,覆盖onDestroy并致电stopService

@Override
public void onDestroy()
{
    super.onDestroy();
    Toast.makeText(<Context>, "Stopping service", Toast.LENGTH_SHORT).show();
    Intent intent = new Intent(<MainActivity>.this, <YourService>.class);
    stopService(intent);
}
相关问题