我在android 6.0中有位置的另一个问题。
我阅读了Android 6.0权限的官方指南,现在正常工作,但我的位置为空,代码似乎被阻止了!
这是OnCreate方法:
if ( Build.VERSION.SDK_INT >= 23 ) { //CONTROLLO PER ANDROID 6.0 O SUPERIORE
int permissionCheck = ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION);
if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
if (ActivityCompat.shouldShowRequestPermissionRationale(this,
Manifest.permission.ACCESS_FINE_LOCATION)) {
} else {
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION},
MY_PERMISSIONS_ACCESS_FINE_LOCATION);
}
}
}else{
Log.d("ANDROID","ANDROID SENZA FRONZOLI");
//dA QUI IN POI MAIN STANDARD
}
这是检查权限类:
@Override
public void onRequestPermissionsResult(int requestCode,
String permissions[], int[] grantResults) {
switch (requestCode) {
case MY_PERMISSIONS_ACCESS_FINE_LOCATION: {
// If request is cancelled, the result arrays are empty.
if (grantResults.length > 0
&& grantResults[0] == PackageManager.PERMISSION_GRANTED) {
Log.d("PERMESSI-fine", "PERMESSI OK");
LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
if ( ContextCompat.checkSelfPermission( this, android.Manifest.permission.ACCESS_FINE_LOCATION ) != PackageManager.PERMISSION_GRANTED ) {
ActivityCompat.requestPermissions( this, new String[] { android.Manifest.permission.ACCESS_FINE_LOCATION },
MY_PERMISSIONS_ACCESS_FINE_LOCATION );
}
Location location = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if (location != null) {
double longitude = location.getLongitude();
double latitude = location.getLatitude();
Log.d("GPS-PROVIDER", String.valueOf(longitude));
Log.d("GPS-PROVIDER", String.valueOf(latitude));
// Toast.makeText(MainActivity.this, "GPS OK", Toast.LENGTH_SHORT).show();
}else{
Log.d("GPS","NIENTE ZONA VUOTA");
Log.d("GPS", String.valueOf(location));
}
} else {
Log.d("PERMESSI-fine","PERMESSE NO");
}
return;
}
// other 'case' lines to check for other
// permissions this app might request
}
}
代码不会出错,简单无误! null对象。现在我正在使用android 6.0模拟器。我没有这个操作系统版本的真实设备。
请帮帮我。
答案 0 :(得分:1)
模拟器没有gps,因此位置提供程序根本不会获取任何位置或任何最后已知的位置。您需要添加gps数据。 follow the link或android.developer source