Android API 25不会要求获得位置许可

时间:2017-03-12 12:10:28

标签: android react-native react-native-android

在android清单中我添加了:

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

我正在开始地理位置,如:

geolocation.getCurrentPosition(function(position) {
        console.log(position);
    }, (error) ...

当我在IOS上运行app时,它运行正常 - 它要求获得位置许可 我正在运行android 7.1模拟器API 25并且它没有要求权限?
我错过了什么或做错了什么?

请注意:android获取我的代码上的坐标,因此它有权限,但它应首先要求用户提供该权限。

2 个答案:

答案 0 :(得分:1)

尝试将 AndroidManifest.xml 中的targetSdkVersion更改为23(Android M)或更高版本。这对我有用。

答案 1 :(得分:0)

从Android 6.0(API级别23)开始,用户在应用运行时向应用授予权限,而不是在安装应用时授予权限。

首先实现 ActivityCompat.OnRequestPermissionsResultCallback

@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {

if (requestCode == REQUEST_LOCATION) {
     if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
           if ( ActivityCompat.checkSelfPermission( this, ACCESS_FINE_LOCATION ) == PackageManager.PERMISSION_GRANTED ) {

           }
     }
}
}

然后检查您是否需要运行时权限

if ( Build.VERSION.SDK_INT >= Build.VERSION_CODES.M ) {
    if ( ActivityCompat.checkSelfPermission( this, ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED ) {

        //Location Permission already granted
        map.setMyLocationEnabled( true );
    } else {
        //Request Location Permission
        requestLocationPermission();
    }
}

private void requestLocationPermission() {
        if ( ContextCompat.checkSelfPermission( this, ACCESS_FINE_LOCATION ) != PackageManager.PERMISSION_GRANTED ) {

            // Should we show an explanation?
            if ( ActivityCompat.shouldShowRequestPermissionRationale( this, ACCESS_FINE_LOCATION ) ) {

                // Show an explanation to the user *asynchronously* -- don't block
                // this thread waiting for the user's response! After the user
                // sees the explanation, try again to request the permission.
                new AlertDialog.Builder(this)
                        .setTitle("Location Permission Needed")
                        .setMessage("This app needs the Location permission, please accept to use location functionality")
                        .setPositiveButton("OK", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialogInterface, int i) {
                                //Prompt the user once explanation has been shown
                                ActivityCompat.requestPermissions( MainActivity.this, new String[]{ACCESS_FINE_LOCATION}, REQUEST_LOCATION );
                            }
                        })
                        .create()
                        .show();


            } else {
                // No explanation needed, we can request the permission.
                ActivityCompat.requestPermissions( this, new String[]{ACCESS_FINE_LOCATION}, REQUEST_LOCATION );
            }
        }
    }

有关运行时权限的更多信息:Requesting Permissions at Run Time