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