2016-04-25 139 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嗎? –