2016-04-25 136 views
-1

你能解释为什么getLastKnownLocation总是返回null吗?getLastKnownLocation总是返回null。请解释?

这是onCreate方法:

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); 

    provider = locationManager.getBestProvider(new Criteria(), false); 

    if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) { 
     // TODO: Consider calling 
     // ActivityCompat#requestPermissions 
     // here to request the missing permissions, and then overriding 
     // public void onRequestPermissionsResult(int requestCode, String[] permissions, 
     //           int[] grantResults) 
     // to handle the case where the user grants the permission. See the documentation 
     // for ActivityCompat#requestPermissions for more details. 
     return; 
    } 

    locationManager.requestLocationUpdates(provider, 400, 1, this); 

    Location location = locationManager.getLastKnownLocation(provider); 

    if (location != null) { 
     Log.i("Location info", "location achieved"); 
    } else 
     Log.i("Location info", "location failed"); 


} 

日志显示此:

04-25 19:13:03.437 11755-11755/com.example.saurabh.locationdemo E/MultiWindowProxy: getServiceInstance failed! 

04-25 19:13:03.462 11755-11755/com.example.saurabh.locationdemo I/Location info: location failed 

我测试我的设备上的应用程序,并在设备上启用谷歌的位置。

+0

问题是与getSystemService行(我有同样的问题),但不知道如何解决它。 – ollie299792458

回答

-2

好的,请参阅下面的代码,我在一些应用程序中使用(并且很好地运行),这应该使您在正确的方向。今天是我第一次在StackOverflow上发布一个答案,所以如果我没有足够的解释,请不要激怒我,希望代码能够不言自明?

public boolean getLocation(Context context, LocationResult result) 
{ 

    locationResult=result; 
    if(lm==null) 
     lm = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE); 

try{gps_enabled=lm.isProviderEnabled(LocationManager.GPS_PROVIDER);}catch(Exception ex){} 
    try{network_enabled=lm.isProviderEnabled(LocationManager.NETWORK_PROVIDER);}catch(Exception ex){} 

    if(!gps_enabled && !network_enabled) 
     return false; 

    if(gps_enabled) 
     lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListenerGps); 
    if(network_enabled) 
     lm.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListenerNetwork); 
    timer1=new Timer(); 


    timer1.schedule(new GetLastLocation(), 10000); 
    return true; 
} 

LocationListener locationListenerGps = new LocationListener() { 
    public void onLocationChanged(Location location) { 
     timer1.cancel(); 
     locationResult.gotLocation(location); 
     lm.removeUpdates(this); 
     lm.removeUpdates(locationListenerNetwork); 
    } 
    public void onProviderDisabled(String provider) {} 
    public void onProviderEnabled(String provider) {} 
    public void onStatusChanged(String provider, int status, Bundle extras) {} 
}; 

LocationListener locationListenerNetwork = new LocationListener() { 
    public void onLocationChanged(Location location) { 
     timer1.cancel(); 
     locationResult.gotLocation(location); 
     lm.removeUpdates(this); 
     lm.removeUpdates(locationListenerGps); 
    } 
    public void onProviderDisabled(String provider) {} 
    public void onProviderEnabled(String provider) {} 
    public void onStatusChanged(String provider, int status, Bundle extras) {} 
}; 

class GetLastLocation extends TimerTask { 
    @Override 

    public void run() { 


     Location net_loc=null, gps_loc=null; 
     if(gps_enabled) 
      gps_loc=lm.getLastKnownLocation(LocationManager.GPS_PROVIDER); 
     if(network_enabled) 
      net_loc=lm.getLastKnownLocation(LocationManager.NETWORK_PROVIDER); 

     if(gps_loc!=null && net_loc!=null){ 
      if(gps_loc.getTime()>net_loc.getTime()) 
       locationResult.gotLocation(gps_loc); 
      else 
       locationResult.gotLocation(net_loc); 
      return; 
     } 

     if(gps_loc!=null){ 
      locationResult.gotLocation(gps_loc); 
      return; 
     } 
     if(net_loc!=null){ 
      locationResult.gotLocation(net_loc); 
      return; 
     } 
     //locationResult.gotLocation(null); 
    } 
} 
+0

是的,我已经添加了这些权限。 – Justice

+0

你可以扩展为什么这个建议的解决方案解决OP吗? –