【问题标题】:Android API 25 doesn's ask for location permissionAndroid API 25 不要求位置权限
【发布时间】:2017-08-02 11:58:38
【问题描述】:

在 android manifest 我添加了:

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

我正在开始地理定位:

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

当我在 IOS 上运行应用程序时,它运行良好 - 它要求获得位置许可。
我正在运行 android 7.1 模拟器 API 25,它不要求权限?
我错过了什么或做错了什么?

请注意:android 在我的代码上获取坐标,因此它具有权限,但它应该首先询问用户该权限。

【问题讨论】:

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


    【解决方案1】:

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

    【讨论】:

    • android/app/build.gradle!
    【解决方案2】:

    从 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

    【讨论】:

    • 是的,我明白这一点。但是 api 25 上的 android 不会请求许可。当我在模拟器上启动应用程序时,它已经很棒了。
    猜你喜欢
    • 1970-01-01
    • 2014-08-21
    • 1970-01-01
    • 2018-12-21
    • 2023-02-06
    • 2014-02-16
    • 1970-01-01
    • 2017-07-06
    相关资源
    最近更新 更多