【问题标题】:Android :: Service OnDestroy called very lateAndroid :: Service OnDestroy 调用很晚
【发布时间】:2010-12-17 11:59:15
【问题描述】:

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

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

活动类

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

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

如何等待服务正常销毁,以便在服务销毁后执行剩余进程,例如读取数据服务已停止。

谢谢,

【问题讨论】:

    标签: android


    【解决方案1】:

    在我看来,onDestroy 在 GC 回收服务时被调用。

    在您的onStop 中尝试m_UIUpdater.interrupt();

    【讨论】:

    • 感谢 developer.android.com/guide/topics/fundamentals.html 服务的活动生命周期从调用 onStart() 开始。这个方法被传递给 startService() 的 Intent 对象。音乐服务将打开 Intent 以发现要播放的音乐并开始播放。服务停止时没有等效的回调 - 没有 onStop() 方法。
    • @Markus,谢谢。我尝试了 System.gc() 但在 MyActivity::OnResume 完成后仍然调用了 MyService::OnDestroy。如何等待 stopservice ?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-03-31
    • 1970-01-01
    • 2016-03-13
    • 2014-06-24
    相关资源
    最近更新 更多