2015-12-24 61 views
-1

我正在嘗試使用與用戶的位置一起工作的應用程序。 首先,我做了一個小測試程序在getLastKnownLocation返回用戶的位置分配通過API請求的權限控制23getLastKnownLocation總是返回空 - API 23

位置的控制是由條件

if (location != null){ 
     Log.i("LogX","Got Location!"); 
    } else{ Log.i("LogX","It has location");} 
做出

的問題是,在所有次,返回「它有位置」。 我認爲這應該是與使用getLastKnownLocation有關的東西。

我的MainActivity

public class MainActivity extends AppCompatActivity implements LocationListener{ 

LocationManager locationManager; 
String provider; 

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

    permissaoMapa(); 

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

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

    Location location = locationManager.getLastKnownLocation(provider); 

    if (location != null){ 
     Log.i("LogX","Got Location!"); 
    } else{ Log.i("LogX","It has location");} 
} 


private void permissaoMapa(){ 
    if(ActivityCompat.checkSelfPermission(MainActivity.this, Manifest.permission.ACCESS_COARSE_LOCATION)!= PackageManager.PERMISSION_GRANTED){ 
     if(ActivityCompat.shouldShowRequestPermissionRationale(MainActivity.this, android.Manifest.permission.ACCESS_COARSE_LOCATION)){ 

     }else{ 
      ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.ACCESS_COARSE_LOCATION},0); 
     } 
    }else { 

    } 
} 


@Override 
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) { 
    super.onRequestPermissionsResult(requestCode, permissions, grantResults); 
    for (int result : grantResults) { 
     if (result == PackageManager.PERMISSION_DENIED) { 

      return; 
     } else { 
      if (grantResults.length ==1) { 

      } 
     } 
    } 
} 

@Override 
public void onLocationChanged(Location location) { 

    Double lat = location.getLatitude(); 
    Double lon = location.getLatitude(); 

    Log.i("LogX",lat.toString()); 
    Log.i("LogX", lon.toString()); 


} 

@Override 
public void onStatusChanged(String provider, int status, Bundle extras) { 

} 

@Override 
public void onProviderEnabled(String provider) { 

} 

@Override 
public void onProviderDisabled(String provider) { 

} 

@Override 
protected void onResume() { 
    super.onResume(); 
    locationManager.requestLocationUpdates(provider, 400, 1, this); 
} 

@Override 
protected void onPause() { 
    super.onPause(); 
    locationManager.removeUpdates(this); 
} 

} 
+0

如果沒有應用程序都抱着位置請求調用然後lastknown位置可能會無效。嘗試使用[FusedLocation API's](https://developers.google.com/android/reference/com/google/android/gms/location/FusedLocationProviderApi)。 [這是一個例子](http://developer.android.com/intl/pt-br/training/location/retrieve-current.html) –

回答

1

對於getLastKnownLocation工作,我做了以下步驟:

  • 我打開終端並鍵入命令 「telnet localhost 5554」,其中5554是指AVD端口
  • 打開安裝在AVD上的地圖應用程序
  • 輸入終端中的命令行「geo fix 12 45」。在這個例子中,你可以鍵入任何經度和緯度信息。

在那裏,它設法得到的位置。

enter image description here