调用gps信息在android2.2下正常,但是2.3以上gps设备显示出来,就是无法读出经纬度
代码如下
private void openGPSSettings() {
LocationManager alm = (LocationManager) this
.getSystemService(Context.LOCATION_SERVICE);
if (alm.isProviderEnabled(android.location.LocationManager.GPS_PROVIDER)) {
Toast.makeText(this, "GPS模块正常", Toast.LENGTH_SHORT).show();
getLocation();
return;
}
Toast.makeText(this, "请开启GPS!", Toast.LENGTH_SHORT).show();
Intent intent = new Intent(Settings.ACTION_SECURITY_SETTINGS);
startActivityForResult(intent, 0); // 此为设置完成后返回到获取界面 }
} private void getLocation() {
// 获取位置管理服务
LocationManager locationManager;
String serviceName = Context.LOCATION_SERVICE;
locationManager = (LocationManager) this.getSystemService(serviceName);
// 查找到服务信息
Criteria criteria = new Criteria();
criteria.setAccuracy(Criteria.ACCURACY_FINE);
// 高精度
criteria.setAltitudeRequired(false);
criteria.setBearingRequired(false);
criteria.setCostAllowed(true);
criteria.setPowerRequirement(Criteria.POWER_LOW);
// 低功耗
String provider = locationManager.getBestProvider(criteria, true);
// 获取GPS信息
Location location = locationManager.getLastKnownLocation(provider);
//Location location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
// 通过GPS获取位置
updateToNewLocation(location);
// 设置监听器,自动更新的最小时间为间隔N秒(1秒为1*1000,这样写主要为了方便)或最小位移变化超过N米
locationManager.requestLocationUpdates(provider, 2 * 1000, 500,
locationListener);
}
private void updateToNewLocation(Location location) {
TextView tv1;
tv1 = (TextView) this.findViewById(R.id.tv1);
Intent intent=getIntent();
String name=intent.getStringExtra("name");
String mima=intent.getStringExtra("mima");
if (location != null) {
double latitude = location.getLatitude();//经度
double longitude = location.getLongitude();//纬度
double altitude = location.getAltitude(); //海拔
tv1.setText("维度:" + latitude + "\n经度" + longitude+"\n海拔:"+altitude);}}
代码如下
private void openGPSSettings() {
LocationManager alm = (LocationManager) this
.getSystemService(Context.LOCATION_SERVICE);
if (alm.isProviderEnabled(android.location.LocationManager.GPS_PROVIDER)) {
Toast.makeText(this, "GPS模块正常", Toast.LENGTH_SHORT).show();
getLocation();
return;
}
Toast.makeText(this, "请开启GPS!", Toast.LENGTH_SHORT).show();
Intent intent = new Intent(Settings.ACTION_SECURITY_SETTINGS);
startActivityForResult(intent, 0); // 此为设置完成后返回到获取界面 }
} private void getLocation() {
// 获取位置管理服务
LocationManager locationManager;
String serviceName = Context.LOCATION_SERVICE;
locationManager = (LocationManager) this.getSystemService(serviceName);
// 查找到服务信息
Criteria criteria = new Criteria();
criteria.setAccuracy(Criteria.ACCURACY_FINE);
// 高精度
criteria.setAltitudeRequired(false);
criteria.setBearingRequired(false);
criteria.setCostAllowed(true);
criteria.setPowerRequirement(Criteria.POWER_LOW);
// 低功耗
String provider = locationManager.getBestProvider(criteria, true);
// 获取GPS信息
Location location = locationManager.getLastKnownLocation(provider);
//Location location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
// 通过GPS获取位置
updateToNewLocation(location);
// 设置监听器,自动更新的最小时间为间隔N秒(1秒为1*1000,这样写主要为了方便)或最小位移变化超过N米
locationManager.requestLocationUpdates(provider, 2 * 1000, 500,
locationListener);
}
private void updateToNewLocation(Location location) {
TextView tv1;
tv1 = (TextView) this.findViewById(R.id.tv1);
Intent intent=getIntent();
String name=intent.getStringExtra("name");
String mima=intent.getStringExtra("mima");
if (location != null) {
double latitude = location.getLatitude();//经度
double longitude = location.getLongitude();//纬度
double altitude = location.getAltitude(); //海拔
tv1.setText("维度:" + latitude + "\n经度" + longitude+"\n海拔:"+altitude);}}
解决方案 »
- android 驱动调试 真没意识
- 设置 ListActivity的setEmptyView()
- 设置suid及文件所有者root,apk通过exec执行此程序时为何euid不是0还是apk的uid呀?急急急~
- 在代码中怎么可以控制一个xml文件include标签里的内容的显示和消失呢?
- android 布局
- 错误提示
- android 视频渲染基础问题。。
- 在service中 如何获取调用startService的 Context
- 请高手帮我看下如下Android代码,问题在哪里
- 请问,以下代码有什么问题?(不能输出 Log.v("AAA", ".............test...1..");)
- 关于android系统运行纯java的讨论
- 关于getSharedPreferences 的空指针异常
请问楼主解决这个问题没?怎么解决的?
Intent callGPSSettingIntent = new Intent(android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS);startActivity(callGPSSettingIntent);替换你的:Intent intent = new Intent(Settings.ACTION_SECURITY_SETTINGS);//这个是启动gps?