最近个人小项目用到了一个位置定位的功能,本来需要去研究下百度定位SDK的,突然看到《第一行代码》里有关于位置定位服务的Demo,所以就借用了一下,结果敲完代码调试了一下,发现有一个Bug,可能是因为现阶段好多定制系统对Google的原生android系统进行了裁剪,出现了这种机型匹配的问题,且原谅我称其为Bug吧,希望大神们可以原谅小弟。
原书代码中作者通过获取可用位置提供器的集合判断了手机中有哪些可进行定位的方式,并优先选用了GPS的方式进行了定位,无论如何并没有采用网络的定位方式,而当获取Location对象的时候,选择采用了getLastKnownLocation()这个方法,首先解释下这个方法是获取最近一次定位的Location对象,当第一次启用的时候,我们还并没有这个对象,所以此处获取的Location必为null,刚开始碰到这个问题很是纠结,找了很多帖子,发现好像都没啥用,后来想到了一个好的方法,所以发帖给大家分享一下,虽然有点Low但效果很好,代码如下:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv_dis = (TextView) findViewById(R.id.display);
locationmanager = (LocationManager)
getSystemService(Context.LOCATION_SERVICE);//获取系统的定位服务
location = getBestLocation(locationmanager);
//获得更好的定位效果
Criteria criteria = new Criteria();
criteria.setAccuracy(Criteria.ACCURACY_FINE);
criteria.setAltitudeRequired(false);
criteria.setBearingRequired(false);
criteria.setCostAllowed(false);
//使用省电模式
criteria.setPowerRequirement(Criteria.POWER_LOW);
provider = locationmanager.getBestProvider(criteria, true);
Log.i("TAG", provider);
if(provider != null){
showLocation(location);
}
//添加位置监听器
locationListener = new LocationListener() {
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
// TODO Auto-generated method stub
}
@Override
public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub
}
@Override
public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub
}
@Override
public void onLocationChanged(Location location) {
// TODO Auto-generated method stub
//检测到改变立马显示
showLocation(location);
}
};
}
@Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
if(!TextUtils.isEmpty(provider)){
locationmanager.requestLocationUpdates(provider, 10000, 1,
locationListener);
}
}
protected void onDestroy() {
super.onDestroy();
if (locationmanager != null) {
// 关闭程序时将监听器移除
locationmanager.removeUpdates(locationListener);
}
}
private Handler handler = new Handler(){
@Override
public void handleMessage(Message msg){
switch (msg.what) {
case SHOW_lOCATION:
String currentPosition = (String) msg.obj;
tv_dis.setText(currentPosition);
break;
}
}
};
private void showLocation(final Location location) {
// TODO Auto-generated method stub
//Log.i("TAG",location.getLatitude()+"\n"+location.getLongitude()+"");
new Thread(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
JuheParseUtils jpu = new JuheParseUtils();
String result = jpu.resultResponse(location);
Log.i("TAG",result);
Message message = new Message();
message.what = SHOW_lOCATION;
message.obj = result;
handler.sendMessage(message);
}
}).start();
}
/*
*优先采用GPS_PROVIDER,如果结果为null,采用NETWORK_PROVIDER
*/
private Location getBestLocation(LocationManager locationManager) {
Location result = null;
if (locationManager != null) {
result = locationManager
.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if (result != null) {
return result;
} else {
result = locationManager
.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
return result;
}
}
return result;
}
}
效果图: