2012-11-27 37 views
4

後,我已經通過調用綁定服務:如何強制onServiceDisconnected()被調用?

bindService(new Intent(IBumpAPI.class.getName()), connection, Context.BIND_AUTO_CREATE); 

我需要調試的目的,使onServiceDisconnected()被調用。

我知道Android系統在與服務的連接意外丟失時會調用此服務,例如服務崩潰或已被殺死,並且在客戶端解除綁定時不會調用此服務。

所以我的問題是如何強制onServiceDisconnected()得到調用,只要我想,所以我可以完成一個測試?

回答

6

您需要啓動服務,然後使用Context.BIND_NOT_FOREGROUND標誌綁定,然後停止它。這將導致onServiceDisconnected被調用。下面是代碼(假設你已經定義了TestService的服務)MainActivity與其中兩個鏈接調用doBind和doUnbind方法按鈕:

package com.example.servicetest; 

import android.app.Activity; 
import android.content.ComponentName; 
import android.content.Context; 
import android.content.Intent; 
import android.content.ServiceConnection; 
import android.os.Bundle; 
import android.os.IBinder; 
import android.util.Log; 
import android.view.Menu; 
import android.view.View; 

public class MainActivity extends Activity { 
    private static final String TAG = "MainActivity"; 

    private ServiceConnection connection = new ServiceConnection() { 

     @Override 
     public void onServiceDisconnected(ComponentName name) { 
      Log.d(TAG, "Service disconnected: " + name); 
     } 

     @Override 
     public void onServiceConnected(ComponentName name, IBinder service) { 
      Log.d(TAG, "Service connected: " + name); 
     } 
    }; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

    } 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     // Inflate the menu; this adds items to the action bar if it is present. 
     getMenuInflater().inflate(R.menu.activity_main, menu); 
     return true; 
    } 

    public void doBind(View v) { 
     Intent i = new Intent(this, TestService.class); 
     startService(i); 
     bindService(i, connection, Context.BIND_NOT_FOREGROUND); 
    } 

    public void doUnbind(View v) { 
     Intent i = new Intent(this, TestService.class); 
     stopService(i); 
    } 

} 

此代碼提供當你點擊按鈕以下日誌:

11-27 09:21:57.326: D/MainActivity(10724): Service connected: ComponentInfo{com.example.servicetest/com.example.servicetest.TestService} 
11-27 09:21:58.099: D/MainActivity(10724): Service disconnected: ComponentInfo{com.example.servicetest/com.example.servicetest.TestService} 
+1

感謝,但unbindService()不會調用onServiceDisconnected()。 (來自Android Doc) –

+0

你確定嗎?當我在我的活動中綁定/取消綁定服務時,發生了什麼事情。此外,沒有關於android文檔中未調用onServiceDisconnected的信息。 – kenota

+1

我覺得它只是在意外發生時才被調用,而不是正常的unbindService()調用的結果。 (你是對的,它不是直接替代文件)任何想法?謝謝 –

1

你可能只是取消綁定服務

public void openService { 
mConnection = new ServiceConnection() { 
      @Override 
      public void onServiceConnected(ComponentName name, IBinder service) { 
       mService = IService.Stub.asInterface(service); 
      } 
      @Override 
      public void onServiceDisconnected(ComponentName cn) { 
       mService = null; 
      } 
     }; 
     bindService(service, mConnection, Context.BIND_AUTO_CREATE); 
    } 
} 

public void closeService() { 
    unbindService(mConnection); 
    stopService(new Intent(this, Service.class)); 
} 
+1

謝謝 - 我試過了,但沒有被調用。 –

+0

你的意思是unbingService和stopService什麼都不做? – rocknow

+0

要調用onServiceDisconnected,你只需要調用closeService – rocknow

相關問題