以开启麦克风权限为例
1.在AndroidManifest.xml中加上
<uses-permission android:name="android.permission.RECORD_AUDIO"/>
2.在AppActivity.java文件的protected voidonCreate(Bundle savedInstanceState) {}方法里可加入以下代码:
PackageManager pkgManager= getPackageManager();
boolean audioSatePermission =
pkgManager.checkPermission(Manifest.permission.RECORD_AUDIO, getPackageName()) == PackageManager.PERMISSION_GRANTED;
if(Build.VERSION.SDK_INT>=23&&!audioSatePermission) {
requestPermission();
}
3.requestPermission();方法的具体写法:
private void requestPermission() {
ActivityCompat.requestPermissions(this,newString[] {Manifest.permission.RECORD_AUDIO},
REQUEST_PERMISSION);
}
网友评论