Android 6.0 and above dynamic access permission problem, Baidu voice recognition

Since the robot's board was upgraded to android 7.0 system, there was a problem that the voice could not wake up and could not be recognized. I immediately realized that it should be caused by the need to dynamically obtain permissions for android 6.0 and above, so I wrote a dynamic application permission and called the dynamic permission. callback. Sure enough. Next, I will post the code, I hope it will help you.

/**
* 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);
//Entering here means no permission.
Log.e("---- ----->", "No permission");
} else {

Log.e("--------->", "Already authorized");
}
}
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: {
// authorization is allowed
if (grantResults.length > 0
&& grantResults[0] == PackageManager.PERMISSION_GRANTED) {
Log.e( "--------->", "Authorization request was granted");
// permission was granted, yay! Do the
// contacts-related task you need to do.

} else {
Log.e("- ------->", "Authorization request denied");
}
return;
}
}
}



Then call the initpermission method in the oncreate method to complete.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324472899&siteId=291194637