Android M权限,返回不正确的结果

时间:2016-01-27 22:20:19

标签: android android-permissions

我在Andorid M设备的应用程序中拥有以下权限。我已经在6.0.0和6.0.1上对此进行了测试,我在Nexus 5X和Nexus 6P上获得了相同的结果。基本上每个权限都会返回错误的结果,当我启用/禁用它们时,结果不会改变。

我将在下面粘贴一些示例测试代码,以显示我是如何测试它的。

Manifest Permissions:

    <uses-permission android:name="android.permission.CALL_PHONE" />
    <uses-permission android:name="android.permission.VIBRATE" />
    <uses-permission android:name="android.permission.READ_PHONE_STATE" />
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.NFC" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.BIND_NFC_SERVICE" />
    <uses-permission android:name="com.samsung.android.providers.context.permission.WRITE_USE_APP_FEATURE_SURVEY" />
    <uses-permission android:name="android.permission.RECEIVE" />
    <uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
    <uses-permission android:name="android.permission.WAKE_LOCK" />
    <uses-permission android:name="com.simplytapp.virtualcard.permission.C2D_MESSAGE" />
    <uses-permission android:name="android.permission.READ_CONTACTS" />
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

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

MainActivity:

    private static final String[] CHECK_PERMISSIONS = {
                Manifest.permission.READ_PHONE_STATE,
                Manifest.permission.CALL_PHONE,
                Manifest.permission.READ_CALL_LOG,
                Manifest.permission.WRITE_CALL_LOG,
                Manifest.permission.USE_SIP,
                Manifest.permission.PROCESS_OUTGOING_CALLS,
                Manifest.permission.CALL_PRIVILEGED,
                Manifest.permission.MODIFY_PHONE_STATE,
                Manifest.permission.VIBRATE,
                Manifest.permission.INTERNET,
                Manifest.permission.NFC,
                Manifest.permission.ACCESS_NETWORK_STATE,
                Manifest.permission.WRITE_EXTERNAL_STORAGE,
                Manifest.permission.BIND_NFC_SERVICE,
                Manifest.permission.WAKE_LOCK,
                Manifest.permission.READ_CONTACTS,
                Manifest.permission.READ_EXTERNAL_STORAGE,
                Manifest.permission.ACCESS_FINE_LOCATION
        };

        for (int i = 0; i < CHECK_PERMISSIONS.length; i++) {   
            permissionsController.checkAccessPermissions(0, CHECK_PERMISSIONS[i]);       
        }

        public boolean checkAccessPermissions(int statusCode, String permission) {
            boolean granted = true;
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                if (ActivityCompat.checkSelfPermission(mContext, permission) != PackageManager.PERMISSION_GRANTED) {
                granted = false;
                }
            }

            Log.i("Permissions Check", "Permission: " + permission + " - has status: " + granted);

            return granted;
        }

这将始终返回相同的结果天气权限被授予。

Permissions Check: Permission: android.permission.READ_PHONE_STATE - has status: true
Permissions Check: Permission: android.permission.CALL_PHONE - has status: true
Permissions Check: Permission: android.permission.READ_CALL_LOG - has status: false
Permissions Check: Permission: android.permission.WRITE_CALL_LOG - has status: false
Permissions Check: Permission: android.permission.USE_SIP - has status: false
Permissions Check: Permission: android.permission.PROCESS_OUTGOING_CALLS - has status: false
Permissions Check: Permission: android.permission.CALL_PRIVILEGED - has status: false
Permissions Check: Permission: android.permission.MODIFY_PHONE_STATE - has status: false
Permissions Check: Permission: android.permission.VIBRATE - has status: true
Permissions Check: Permission: android.permission.INTERNET - has status: true
Permissions Check: Permission: android.permission.NFC - has status: true
Permissions Check: Permission: android.permission.ACCESS_NETWORK_STATE - has status: true
Permissions Check: Permission: android.permission.WRITE_EXTERNAL_STORAGE - has status: true
Permissions Check: Permission: android.permission.BIND_NFC_SERVICE - has status: false
Permissions Check: Permission: android.permission.WAKE_LOCK - has status: true
Permissions Check: Permission: android.permission.READ_CONTACTS - has status: true
Permissions Check: Permission: android.permission.READ_EXTERNAL_STORAGE - has status: true
Permissions Check: Permission: android.permission.ACCESS_FINE_LOCATION - has status: true

2 个答案:

答案 0 :(得分:0)

您需要为目标api 22编译应用程序,或者您应该在运行时请求权限。请参阅:http://developer.android.com/training/permissions/requesting.html

答案 1 :(得分:0)

这只是Google地图许可位置的示例之一。你可以添加更多内容来检查其他内容。

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && getActivity().checkSelfPermission( Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED && getActivity().checkSelfPermission(Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
     if(!shouldShowRequestPermissionRationale(Manifest.permission.ACCESS_FINE_LOCATION)) {
          alertOpenSetting();
     }else {
          checkLocationPermission();
          ActivityCompat.requestPermissions(getActivity(), new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, REQUEST_CODE_ASK_PERMISSIONS);
     }
}


public boolean checkLocationPermission()
{
    String permission = "android.permission.ACCESS_FINE_LOCATION";
    int res = getActivity().checkCallingOrSelfPermission(permission);
    return (res == PackageManager.PERMISSION_GRANTED);
}

@Override
public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
    switch (requestCode) {
        case 0: {
            //null or 0
        }
        case 1: {
            if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                insertPermissionWrapper();
                NetworkManager.getInstance().startLocationService();
            }else {
                //denied
            }
            return;
        }
        default:
            super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    }
}


private void alertOpenSetting() {
    AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(getActivity());
    alertDialogBuilder.setTitle("Location, Storage and Telephone permissions are required to use this app.");
    alertDialogBuilder.setMessage("Please enable these permissions in Permissions under app settings.");
    alertDialogBuilder.setNegativeButton("Go to setting", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int id) {
            goToSettings();
        }
    });
    alertDialogBuilder.create();
    alertDialogBuilder.show();
}

希望这是有帮助的