LocationManager locationManager22 ;
String serviceName = Context.LOCATION_SERVICE;
locationManager22 = (LocationManager) BaiduActivity.this.getSystemService(serviceName);// 查找到服务信息
String provider=LocationManager.GPS_PROVIDER;
Location location1 = locationManager22.getLastKnownLocation(provider); // 通过GPS获取位置
try{
String ss=location1.toString();
Toast.makeText(this, ss, Toast.LENGTH_SHORT).show();}
catch(Exception es)
{
Toast.makeText(this, "获取不到", Toast.LENGTH_SHORT).show();
}
每次运行都显示获取不到,请问是什么原因啊 !请大神指导!
Android GPS 百度地图
------解决方案--------------------
前提:如果你的GPS开了的话 那么可能是如下原因
因为getLastKnownLocation方法获取的是上一次已知的位置
但是你的apk运行的时候 从来没有获取位置数据 所以每次运行都获取不到数据 每次location都是为Null
解决办法:使用new LocationListener()接口
locationManager.requestLocationUpdates(locationprovider, 2000, 2, listener); //注册监听位置改变
然后再 location=locationManager.getLastKnownLocation(locationprovider);
就可以获取数据了
------解决方案--------------------
你的设备有没有GPS模块
------解决方案--------------------
模拟器么?
------解决方案--------------------