关于Android用户定位获取location值为null的解决方法
来源:互联网 发布:淘宝0.1元商品 编辑:程序博客网 时间:2024/04/28 06:47
在玩一个GPS的东西 获取Location一直为null 后来是从onLocationChanged里面拿location才搞定了 贴一下吧 代码太乱
- /**
- * 主要是用户定位操作
- *@author hope
- */
- package com.jeedroid.tools;
- import java.io.IOException;
- import java.util.List;
- import java.util.Locale;
- import android.content.Context;
- import android.location.Address;
- import android.location.Criteria;
- import android.location.Geocoder;
- import android.location.Location;
- import android.location.LocationListener;
- import android.location.LocationManager;
- import android.os.Bundle;
- import android.provider.Settings;
- import android.util.Log;
- public class LocationTools
- {
- private Location location;
- public static LocationManager getLocationManager(Context context)
- {
- return (LocationManager) context.getSystemService(context.LOCATION_SERVICE);
- }
- //获取位置信息
- public String getAddress(Context context)
- {
- LocationManager locationManager = this.getLocationManager(context);
- if(!locationManager.isProviderEnabled(android.location.LocationManager.GPS_PROVIDER))
- {
- //打开GPS 需Android2.2以上系统支持
- android.provider.Settings.Secure.setLocationProviderEnabled(context.getContentResolver(), LocationManager.GPS_PROVIDER, false);
- }
- return doWork(context);
- }
- private String doWork(Context context)
- {
- String addres="";
- LocationManager locationManager = this.getLocationManager(context);
- Criteria criteria = new Criteria();
- // 获得最好的定位效果
- criteria.setAccuracy(Criteria.ACCURACY_FINE);
- criteria.setAltitudeRequired(false);
- criteria.setBearingRequired(false);
- criteria.setCostAllowed(false);
- // 使用省电模式
- criteria.setPowerRequirement(Criteria.POWER_LOW);
- String provider =locationManager.getBestProvider(criteria, true);
- Log.i("provider>>>>>>", provider);
- //获得当前位置 location为空是一直取 从onLocationChanged里面取
- while(location==null)
- {
- location =locationManager.getLastKnownLocation(provider);
- }
- //locationListener
- LocationListener locationListener = new LocationListener()
- {
- @Override
- public void onLocationChanged(Location location)
- {
- LocationTools.this.location=location;
- }
- @Override
- public void onProviderDisabled(String provider)
- {
- }
- @Override
- public void onProviderEnabled(String provider)
- {
- }
- @Override
- public void onStatusChanged(String provider, int status, Bundle extras)
- {
- }
- };
- locationManager.requestLocationUpdates(provider, 1000, 10, locationListener);
- Geocoder geo = new Geocoder(context,Locale.getDefault());
- try
- {
- List<Address> address=geo.getFromLocation(location.getLatitude(), location.getLongitude(), 1);
- if(address.size()>0)
- {
- addres=address.get(0).getAddressLine(0);
- }
- } catch (IOException e)
- {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- return addres;
- }
- }
- 关于Android用户定位获取location值为null的解决方法
- Android GPS室内定位问题的解决方法(location为null)
- android 网络定位失败 location为null的真正原因
- 关于google的location定位的一些问题(location为null)
- android获取位置location为null的问题
- Android中获取location永为null
- GetModuleHandle()获取值为NULL的解决方法
- Android 解决GPS室内定位问题(location为null)
- Andriod 手机定位 解决location为null的问题
- Location 定位获取用户许可
- 安卓手机定位,location为null
- android getLastKnownLocation()获取当前位置为null解决方法
- Android:GPS定位 Location == null
- 作百度地图定位时,location.getAddrStr()获取到的值一直为空的问题
- Android使用百度定位API时获取的地址信息为null
- 【Android】getActionBar()为null的解决方法总结
- 【Android】getActionBar()为null的解决方法总结
- 【Android】getActionBar()为null的解决方法总结
- C#执行批处理命令
- C++ vector容器类型
- squid缓存批量更新
- Access不能读取记录;在'MSysObjects' 上没有读取数据权
- 维护CentOS6.2下的一些常用脚本之二
- 关于Android用户定位获取location值为null的解决方法
- 关于TCP封包、粘包、半包
- violate
- C语言struct和union结合使用,空间最小
- 杀死指定进程
- flex_停止事件传播说明示例;
- 向Android模拟器打电话发短信的简单方法
- 教你如何迅速秒杀99%的海量数据处理面试题
- android中引用项目工程中的sqlite文件