为什么RECORD_AUDIO每次都会在棉花糖中获得许可?

时间:2017-04-18 06:00:44

标签: android android-permissions

我正在使用我的应用程序中的媒体记录器。当我要求获得在marshmallow及以上版本中录制音频的许可时,它每次都会给我许可。这是我的代码。

private static final int REQUEST_RECORD_AUDIO_PERMISSION = 200;
// Requesting permission to RECORD_AUDIO
private boolean permissionToRecordAccepted = false;
private String[] permissions = {Manifest.permission.RECORD_AUDIO};

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    // Defined SoundLevelView in main.xml file
    setContentView(R.layout.activity_noice_meter);
    //check for permission
    ActivityCompat.requestPermissions(this, permissions, REQUEST_RECORD_AUDIO_PERMISSION);
}

@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
    super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    switch (requestCode) {
        case REQUEST_RECORD_AUDIO_PERMISSION:

            permissionToRecordAccepted = grantResults[0] == PackageManager.PERMISSION_GRANTED;

            break;
    }
    if (!permissionToRecordAccepted) {

        finish();
    } else {
        //Task to be done when permission is granted
        //init();
    }

}

3 个答案:

答案 0 :(得分:1)

试试这个,

private Context mContext=YourActivity.this;

private static final int REQUEST = 112;

if (Build.VERSION.SDK_INT >= 23) {
    String[] PERMISSIONS = {android.Manifest.permission.RECORD_AUDIO};
    if (!hasPermissions(mContext, PERMISSIONS)) {
        ActivityCompat.requestPermissions((Activity) mContext, PERMISSIONS, REQUEST );
    } else {
        //do here
    }
} else {
     //do here
}

获取权限结果

@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
    super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    switch (requestCode) {
        case REQUEST: {
            if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                    //do here
            } else {
                Toast.makeText(mContext, "The app was not allowed to record audio", Toast.LENGTH_LONG).show();
            }
        }
    }
}

检查marshmallow的权限

private static boolean hasPermissions(Context context, String... permissions) {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && context != null && permissions != null) {
        for (String permission : permissions) {
            if (ActivityCompat.checkSelfPermission(context, permission) != PackageManager.PERMISSION_GRANTED) {
                return false;
            }
        }
    }
    return true;
}

清单

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

答案 1 :(得分:0)

在onCreate中使用以下代码。在这里,您不会检查是否授予许可。

setTimeout

答案 2 :(得分:0)

新的运行时权限将像我们将应用程序的targetSdkVersion设置为23时所描述的那样工作。如果应用程序的targetSdkVersion设置为小于23.将假设应用程序尚未使用新的权限系统进行测试并将切换到相同的旧行为:用户必须在安装时接受每个权限,并且一旦安装就会被授予所有权限!

长话短说:请先检查yopur target sdkversion并执行@jitesh mohite所示的代码