关于Android用户定位获取location值为null的解决方法

来源:互联网 发布:淘宝0.1元商品 编辑:程序博客网 时间:2024/04/28 06:47

在玩一个GPS的东西 获取Location一直为null  后来是从onLocationChanged里面拿location才搞定了   贴一下吧  代码太乱

  1. /**  
  2.  * 主要是用户定位操作  
  3.  *@author hope
  4.  */  
  5. package com.jeedroid.tools;   
  6.   
  7. import java.io.IOException;   
  8. import java.util.List;   
  9. import java.util.Locale;   
  10.   
  11. import android.content.Context;   
  12. import android.location.Address;   
  13. import android.location.Criteria;   
  14. import android.location.Geocoder;   
  15. import android.location.Location;   
  16. import android.location.LocationListener;   
  17. import android.location.LocationManager;   
  18. import android.os.Bundle;   
  19. import android.provider.Settings;   
  20. import android.util.Log;   
  21.   
  22. public class LocationTools   
  23. {   
  24.     private Location location;   
  25. public static LocationManager getLocationManager(Context context)   
  26. {   
  27.     return (LocationManager) context.getSystemService(context.LOCATION_SERVICE);   
  28. }   
  29. //获取位置信息   
  30. public String getAddress(Context context)   
  31. {   
  32.     LocationManager locationManager = this.getLocationManager(context);   
  33.     if(!locationManager.isProviderEnabled(android.location.LocationManager.GPS_PROVIDER))   
  34.     {   
  35.         //打开GPS 需Android2.2以上系统支持   
  36.     android.provider.Settings.Secure.setLocationProviderEnabled(context.getContentResolver(), LocationManager.GPS_PROVIDER, false);   
  37.     }   
  38.     return doWork(context);   
  39. }   
  40.   
  41. private String doWork(Context context)   
  42. {   
  43.     String addres="";   
  44.     LocationManager locationManager = this.getLocationManager(context);   
  45.     Criteria criteria = new Criteria();   
  46.     // 获得最好的定位效果     
  47.     criteria.setAccuracy(Criteria.ACCURACY_FINE);     
  48.     criteria.setAltitudeRequired(false);     
  49.     criteria.setBearingRequired(false);     
  50.     criteria.setCostAllowed(false);     
  51.     // 使用省电模式     
  52.     criteria.setPowerRequirement(Criteria.POWER_LOW);     
  53.     String provider =locationManager.getBestProvider(criteria, true);   
  54.     Log.i("provider>>>>>>", provider);   
  55.     //获得当前位置  location为空是一直取  从onLocationChanged里面取   
  56.     while(location==null)   
  57.     {   
  58.     location =locationManager.getLastKnownLocation(provider);   
  59.     }   
  60.     //locationListener   
  61.     LocationListener locationListener = new LocationListener()   
  62.     {   
  63.   
  64.         @Override  
  65.         public void onLocationChanged(Location location)   
  66.         {   
  67.             LocationTools.this.location=location;   
  68.         }   
  69.   
  70.         @Override  
  71.         public void onProviderDisabled(String provider)   
  72.         {   
  73.                
  74.         }   
  75.   
  76.         @Override  
  77.         public void onProviderEnabled(String provider)   
  78.         {   
  79.                
  80.         }   
  81.   
  82.         @Override  
  83.         public void onStatusChanged(String provider, int status, Bundle extras)   
  84.         {   
  85.                
  86.         }   
  87.            
  88.     };   
  89.     locationManager.requestLocationUpdates(provider, 100010, locationListener);   
  90.      
  91.     Geocoder geo = new Geocoder(context,Locale.getDefault());   
  92.     try  
  93.     {   
  94.         List<Address> address=geo.getFromLocation(location.getLatitude(), location.getLongitude(), 1);   
  95.         if(address.size()>0)   
  96.         {   
  97.             addres=address.get(0).getAddressLine(0);   
  98.         }   
  99.     } catch (IOException e)   
  100.     {   
  101.         // TODO Auto-generated catch block   
  102.         e.printStackTrace();   
  103.     }   
  104.     return addres;   
  105. }   
  106. }  

原创粉丝点击