2016-04-25 6 views
-1

getLastKnownLocationが常にnullを返す理由を説明してください。getLastKnownLocationは常にnullを返します。説明してください?

これはのonCreateメソッドである:私は私のデバイス上でアプリケーションをテストしていし、デバイス上のGoogleの場所を有効にしている

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 

@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"); 


} 

ログはこれを示します。

+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を解決する理由を広げることができますか? –

関連する問題