2010-12-17 7 views
1

私はスレッドでサービスしています。アクティビティから開始サービス(Activity :: onDestroy()から)を呼び出し、サービス(activity :: onCreate())を停止しています。Android :: Service OnDestroyは非常に遅く呼び出されました

StopService()はすぐに返されましたが、Service :: OnDestroyは非常に多くの時間の後にかなり遅く呼び出されました。なぜですか? StopService()を呼び出すとすぐにService :: OnDestroyを呼び出す方法。今ここに

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");  
} 

Serviceクラス

@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"); 
      } 
     } 
    } 
} 

はOnDestroyと呼ばれる方法を後半たMyService ::示しlogcatの出力です。

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 

サービスコードを正しく実装しましたか?

データサービスの読み取りが停止したように、サービスが破棄された後に残りの処理を実行できるように、サービスが正常に終了するまで待つことができます。

おかげで、

答えて

2

GCがサービスを再利用する際onDestroyが呼び出されるように私には思えます。

onStopm_UIUpdater.interrupt();を試してください。

+0

stopService()を呼び出した後、手動でSystem.gc()を手動で試してみてください。 – mad

+0

ありがとうございます。http://developer.android.com/guide/topics/fundamentals.html サービスの有効期間は、onStart()の呼び出しで始まります。このメソッドは、startService()に渡されたIntentオブジェクトを渡します。音楽サービスはインテントを開き、再生する音楽を見つけて再生を開始します。 サービスが停止したときに対応するコールバックはありません - onStop()メソッドはありません。 – JRC

+0

@マーカス、ありがとう。私はSystem.gc()を試みましたが、MyActivity :: OnResumeが完了した後もMyService :: OnDestroyが呼び出されます。 stopserviceでどのように待つことができますか? – JRC

関連する問題