1. 程式人生 > >android service bindService onServiceConnected沒有呼叫

android service bindService onServiceConnected沒有呼叫

http://blog.sina.com.cn/s/blog_749e806f0100yikk.html

開始程式碼如下:、

public void onCreate(Bundle b){
  super.onCreate(b);
  setContentView(R.layout.mapactivity);
  //繫結服務
  Intent i=new Intent();
  i.setClass(MapTabActivity.this,GetNowPosService.class);
  booleanisret=this.getApplicationContext().bindService(i, conn,Context.BIND_AUTO_CREATE);
  if(isret){
  Log.i(TAG, "BIND ...");
  //設定地圖檢視
    mapView=(MapView)findViewById(R.id.mapview);
  //設定有懸浮的地圖控制元件
  mapView.setBuiltInZoomControls(false);
  //設定交通圖
  mapView.setTraffic(true);
  //地圖控制控制元件
  mapControl=mapView.getController();
  //得到經緯度
  //this.getApplicationContext().startService(i);
  if(myService==null){
   Toast.makeText(this,"myServiceis null ",Toast.LENGTH_LONG);
  }else {
   Toast.makeText(this,"myServiceis not null  ",Toast.LENGTH_LONG);
  }
  MapTabActivity.this.myService=((GetNowPosService.myBinder)arg1).getPosService();
  StringtestStr=myService.test();
  Log.i(TAG,"testStr:"+testStr);
  location=myService.getUserPos();
  Doublelongitute=location.getLongitude()*1E6;
  Doublelatitute=location.getLatitude()*1E6;
  //得到地理位置
    gp=newGeoPoint(Integer.parseInt(latitute.toString()),Integer.parseInt(longitute.toString()));
  mapControl.setCenter(gp);
  mapControl.setZoom(8);
  }else {
   Log.i(TAG,"UNbind  ...");
  }
 }

,最後找了好久,在一個外國網站上找到如下:

This is the designed behavior of all of these methods. For example,in the bindService(Intentservice, ServiceConnection conn, intflags) method accordingto thedocumentation, the service will only run as long as the callingcontext exists:

The service will be considered required by the system only for aslong as the calling context exists. For example, if this Context isan Activity that is stopped, the service will not be required tocontinue running until the Activity is resumed.

Disconnect from an application service. You will no longer receivecalls as the service is restarted, and the service is now allowedto stop at any time.

Using startService() overridesthe default service lifetime that is managed bybindService(Intent,ServiceConnection, int): it requires the service to remainrunning until stopService(Intent)

 iscalled, regardless of whether any clients are connected to it. Notethat calls to startService() arenot nesting: no matter how many times you callstartService(),a single call to stopService(Intent) willstop it.

也就是說:在繫結的的時候Context 不可以為空,在OnCreate中繫結,當然contexe沒有完成啊!

所以就把程式碼轉移到和服務連結器連線成功裡面呼叫!

private ServiceConnection conn=new ServiceConnection(){
  @Override
  public voidonServiceConnected(ComponentName arg0, IBinder arg1) {
   Log.i(TAG,"onServiceConnected : myServie is set to  objectfrom getPosService");
   MapTabActivity.this.myService=((GetNowPosService.myBinder)arg1).getPosService();
   StringtestStr=myService.test();
   Log.i(TAG,"testStr:"+testStr);
   location=myService.getUserPos();
   Doublelongitute=location.getLongitude()*1E6;
   Doublelatitute=location.getLatitude()*1E6;
   //得到地理位置
     gp=newGeoPoint(Integer.parseInt(latitute.toString()),Integer.parseInt(longitute.toString()));
   mapControl.setCenter(gp);
   mapControl.setZoom(8);
  }