标签:申请 state gem context storage read on() override work
由于机器人的板子升级到了android 7.0系统,于是出现了语音唤醒不了,识别不了的问题,马上意识到应该是android 6.0以上需要动态获取权限导致的原因,于是乎写了动态申请权限,调用了动态权限回调。果然好了。接下来贴代码,希望对大家有帮助。
/**
* android 6.0 以上需要动态申请权限
*/
private void initPermission() {
String permissions[] = {
Manifest.permission.RECORD_AUDIO,
Manifest.permission.READ_PHONE_STATE,
Manifest.permission.ACCESS_NETWORK_STATE,
Manifest.permission.READ_EXTERNAL_STORAGE,
Manifest.permission.WRITE_EXTERNAL_STORAGE,
Manifest.permission.MODIFY_AUDIO_SETTINGS,
Manifest.permission.ACCESS_WIFI_STATE,
Manifest.permission.WAKE_LOCK,
Manifest.permission.RECEIVE_BOOT_COMPLETED
};
ArrayList<String> toApplyList = new ArrayList<String>();
for (String perm : permissions) {
if (PackageManager.PERMISSION_GRANTED != ContextCompat.checkSelfPermission(this, perm)) {
toApplyList.add(perm);
//进入到这里代表没有权限.
Log.e("--------->", "没有权限");
} else {
Log.e("--------->", "已经被授权");
}
}
String tmpList[] = new String[toApplyList.size()];
if (!toApplyList.isEmpty()) {
ActivityCompat.requestPermissions(this, toApplyList.toArray(tmpList), 123);
}
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
// super.onRequestPermissionsResult(requestCode, permissions, grantResults);
switch (requestCode) {
case 1: {
// 授权被允许
if (grantResults.length > 0
&& grantResults[0] == PackageManager.PERMISSION_GRANTED) {
Log.e("-------->", "授权请求被允许");
// permission was granted, yay! Do the
// contacts-related task you need to do.
} else {
Log.e("-------->", "授权请求被拒绝");
}
return;
}
}
}
然后在oncreate方法里调用initpermission方法,完成。
标签:申请 state gem context storage read on() override work
原文地址:https://www.cnblogs.com/carina-jiang/p/8856309.html