我無法使onServiceConnected()方法運行,這意味着它不會將我的活動綁定到服務。無法將活動綁定到服務
這可能很簡單,我錯過了 - 但我已經嘗試了很多次 - 從頭開始。
在這裏,我們去...
我的服務類
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
public class QuickService extends Service {
private final IBinder mBinder = new QuickBinder(this);
@Override
public IBinder onBind(Intent intent) {
return mBinder;
}
}
我的文件夾類
import android.os.Binder;
public class QuickBinder extends Binder {
private final QuickService service;
public QuickBinder(QuickService service){
this.service = service;
}
public QuickService getService(){
return service;
}
}
和...試圖綁定到的活動服務。
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;
public class QuickActivity extends Activity {
QuickService mService;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_connecting);
}
@Override
protected void onStart() {
super.onStart();
Intent intent = new Intent(this, QuickService.class);
bindService(intent, mConnection, Context.BIND_AUTO_CREATE);
}
@Override
protected void onStop() {
super.onStop();
// Unbind from the service
unbindService(mConnection);
}
/** Defines callbacks for service binding, passed to bindService() */
private ServiceConnection mConnection = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName className,
IBinder service) {
Logger.d("Connected!!! :D");
// We've bound to LocalService, cast the IBinder and get LocalService instance
QuickBinder binder = (QuickBinder) service;
mService = binder.getService();
}
@Override
public void onServiceDisconnected(ComponentName arg0) {
}
};
}
而且,在清單文件中定義的服務 - 如果你認爲這是問題。
<service android:name=".QuickService"></service>
那麼,我在這裏做錯了什麼?爲什麼不調用onServiceConnected()方法?
謝謝,它現在都在工作。我必須去看看爲什麼你現在需要添加。 – shadrx 2012-07-07 05:29:21
你能解釋爲什麼我需要補充嗎? – shadrx 2012-07-07 05:31:16