如何申请存储权限?

时间:2016-12-21 18:56:23

标签: android android-permissions

我正在尝试调用我的类来检查存储权限

// check for permissions
    permissions.checkpermissions(getBaseContext());

但问题是,它不是让我在课堂上实现这个我得到以下错误 -

“类型权限的方法onRequestPermissionsResult(int,String [],int [])必须覆盖或实现超类型方法”

“方法requestPermissions(String [],int)未定义类型权限”

这是我的代码 -

public class permissions {

public static void checkpermissions(Context context) {
    // Check if we're running on Android 6.0 or higher
    if (Build.VERSION.SDK_INT >= 23) {
        if (!Settings.System.canWrite(context)) {
            requestPermissions(new String[] {
                    Manifest.permission.WRITE_EXTERNAL_STORAGE,
                    Manifest.permission.READ_EXTERNAL_STORAGE }, 2909);
        } else {
            // continue with your code
        }
    } else {
        // continue with your code
    }
    // **check if app has permission 1**//

}

// **check if app has permission 2**//
@Override
public void onRequestPermissionsResult(int requestCode,
        String[] permissions, int[] grantResults) {
    switch (requestCode) {
    case 2909: {
        if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
            Log.e("Permission", "Granted");
        } else {
            Log.e("Permission", "Denied");
        }
        return;
    }
    }
}

// **check if app has permission 2**//

}

1 个答案:

答案 0 :(得分:0)

您无法在任意类上调用requestPermissions()或覆盖onRequestPermissionsResult()。这些是Activity。您已经拥有Activity。摆脱此permissions课程,并将与权限相关的逻辑移至Activity

或者,使用many libraries that help you implement runtime permissions之一。