我在模拟器和Studio中使用Android Studio 2.2.3,API 23(Android 6.0 / M)。我理解,从API 23开始,您必须对调用危险权限的方法使用运行时权限(例如,在这种情况下为ACCESS_FINE_LOCATION)。在下面的代码中,我的getLastKnownLocation()方法返回null(但Provider和LocationManager不为null)。不知道我做错了什么,但我觉得它与我如何编写运行时权限有关。任何见解将不胜感激。还应注意,在我的“模拟器位置”设置中,该应用程序未显示在“最近的位置请求”下。
public class MapsActivity extends FragmentActivity implements OnMapReadyCallback, LocationListener {
private GoogleMap mMap;
LocationManager locationManager;
String provider;
Location location;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maps);
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
provider = locationManager.getBestProvider(new Criteria(), false);
Log.i("Location Manager", locationManager.toString()); //not returning null
Log.i("Provider", provider); //not returning null
//Runtime permission request
if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
if (ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.ACCESS_FINE_LOCATION)) {
Toast.makeText(getApplicationContext(), "<Explanation of permission request goes here>", Toast.LENGTH_SHORT).show();
} else {
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, 1);
}
}
else {
location = locationManager.getLastKnownLocation(provider);
if (location != null) {
Log.i("Location info:", "not null");
} else {
Log.i("Location info:", "null"); //always returns null, which means the if(ActivityCompat... statement returns false, ie. permission is granted
}
}
}
/**
* Manipulates the map once available.
* This callback is triggered when the map is ready to be used.
* This is where we can add markers or lines, add listeners or move the camera. In this case,
* we just add a marker near Sydney, Australia.
* If Google Play services is not installed on the device, the user will be prompted to install
* it inside the SupportMapFragment. This method will only be triggered once the user has
* installed Google Play services and returned to the app.
*/
@Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
// Add a marker in Sydney and move the camera
LatLng sydney = new LatLng(-34, 151);
mMap.addMarker(new MarkerOptions().position(sydney).title("Marker in Sydney"));
mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney));
}
@Override
public void onLocationChanged(Location location) {
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
Log.i("Permission", "Permission granted");
} else {
Log.i("Permission", "Permission denied");
}
}
}
在清单中我有:
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
另外,我试图在API 21中编写类似的代码,因为它应该不使用运行时权限,但它仍然提示我包含运行时权限。任何人都知道如何在API 21中绕过这个?我是否必须使用早期版本的android studio?
答案 0 :(得分:0)
getLastKnowLocation()
返回上一个已知位置。它可用于快速修复,直到提供更准确的位置。要注册位置更新,您必须致电requestLocationUpdates()