Android::Service OnDestroy 调用得很晚

标签 android

我有线程服务,从 Activity 中我调用启动服务(从 Activity::onDestroy())并停止服务(activity::onCreate())。

StopService() 立即返回,但我的 Service::OnDestroy 调用了很多次,很晚了,为什么?如何让 Service::OnDestroy 在调用 StopService() 时立即调用。

Activity 类别

MyActivity::OnDestroy()
{
  System.out.println("MyActivity-OnDestroy Start");
  // we are leaving activity so start service
  Intent svcIntent = new Intent(this,MyService.class);
  svcIntent.putExtra("SomeData", m_iData);
  startService(svcIntent);
  System.out.println("MyActivity-OnDestroy, startservice called");
  ..
  ..
  System.out.println("MyActivity-OnDestroy, End");
}
MyActivity::OnCreate()
{
  System.out.println("MyActivity-onCreate Start");
  // if service running stop it.
  if (m_bCalledFromNotification)
  {
   System.out.println("Stopping Service");
   boolean b = stopService(new Intent(this,AudioService.class));
   System.out.println("Stopping Service, Status="+b);
  }
  System.out.println("MyActivity-onCreate End");      
}

服务等级

@Override
public IBinder onBind(Intent intent) 
{
    return null;
}

// When service is created
@Override
public void onCreate() 
{
    // initialize service data
    System.out.println("MyService Created");    
}

@Override
public void onDestroy() 
{
    System.out.println("In MyService::onDestroy");
    m_UIUpdater.interrupt();
    System.out.println("In MyService::onDestroy, updater thread interrupted");
    ..
    System.out.println("My Service Stopped");
}

// This is the old onStart method that will be called on the pre-2.0
// platform.  On 2.0 or later we override onStartCommand() so this
// method will not be called.
@Override
public void onStart(Intent intent, int startId) 
{
    StartupHandler(intent,startId);
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) 
{
    StartupHandler(intent,startId);
    // We want this service to continue running until it is explicitly
    // stopped, so return sticky.
    return START_STICKY;
}

public void StartupHandler(Intent intent, int startid) 
{
    // send notification 
    System.out.println("In MyService::StartupHandler, Sending notification");

    // start thread 
    m_UIUpdater = new Thread(new Runnable() 
        {
        public void run() 
        {
            System.out.println("In MyService::Thread::Run");
            while (!Thread.interrupted())
            {
                // check update
                System.out.println("In MyService::Thread::Run::got update and loop for next update");
            }
        }
    }
}

现在这是 logcat 的输出,显示了 MyService::OnDestroy 调用的时间。

12-17 09:25:44.462: INFO/System.out(314): MyActivity-OnDestroy Start
..
12-17 09:25:44.493: INFO/System.out(314): MyActivity-OnDestroy, startservice called
..
12-17 09:25:51.102: INFO/System.out(314): MyActivity-OnDestroy, End 
12-17 09:25:51.153: INFO/System.out(314): MyService Created
12-17 09:25:51.172: INFO/System.out(314): In MyService::StartupHandler, Sending notification
12-17 09:25:51.273: INFO/System.out(314): In MyService::Thread::Run
12-17 09:25:51.302: INFO/System.out(314): In MyService::Thread::Run::got update and loop for next update
..
..
..
12-17 09:25:59.903: INFO/System.out(314): MyActivity-onCreate Start
..
12-17 09:26:00.461: INFO/System.out(314): Stopping Service
12-17 09:26:00.471: INFO/System.out(314): Stopping Service, Status=true
12-17 09:26:01.363: INFO/System.out(314): MyActivity-onCreate End
12-17 09:26:01.421: INFO/System.out(314): In MyService::OnDestroy
12-17 09:26:01.421: INFO/System.out(314): In MyService::OnDestroy, updater interrupted
12-17 09:26:01.591: INFO/System.out(314): My Service Stopped

我是否正确实现了服务代码?

我如何等待服务优雅地销毁,以便我可以在服务销毁后执行剩余的进程,例如读取数据服务已停止。

谢谢

最佳答案

在我看来,当GC回收服务时,会调用onDestroy

onStop 中尝试 m_UIUpdater.interrupt();

关于Android::Service OnDestroy 调用得很晚,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4470222/

相关文章:

android - 将外部 jar 添加到 Android UIautomator 项目

基于多个实时数据值的 Android 逻辑

android - 如何在 Android 中将 View 转换为位图?

Android - 需要有关 SDK 20 中的 Tabs+Swipe 模板的帮助

android - 通知自定义声音在前台播放但在后台应用程序时不播放

android - "Content has view with id attribute ' android.r.id.list ' that is not a ListView Class."在 fragment 中创建 ListView 时

android - 单击通知操作时如何取消改造 2 请求?

java - 如何更改为我可以删除的 ValueEventListener?

Android 无法访问本地主机?

android - 为什么应用程序在 android studio 2.0 中速度变慢并使用超过 40 MB 缓存