我是靠谱客的博主 幽默鞋垫,最近开发中收集的这篇文章主要介绍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  ...");
  }
 }

 

 

,最后找了好久,在一个外国网站上找到如下:

http://stackoverflow.com/questions/3792344/why-isnt-onserviceconnected-called

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.

For unbindService(ServiceConnection conn) thedocumentation says:

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.

In the startService(Intentservice) documentation itsays:

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

最后

以上就是幽默鞋垫为你收集整理的android service bindService onServiceConnected没有调用的全部内容,希望文章能够帮你解决android service bindService onServiceConnected没有调用所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(42)

评论列表共有 0 条评论

立即
投稿
返回
顶部