Android 6.0以上動態獲取權限問題,百度語音識別

因爲機器人的板子升級到了android 7.0系統,因而出現了語音喚醒不了,識別不了的問題,立刻意識到應該是android 6.0以上須要動態獲取權限致使的緣由,因而乎寫了動態申請權限,調用了動態權限回調。果真好了。接下來貼代碼,但願對你們有幫助。android

/** * 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);    }}@Overridepublic 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方法,完成。
相關文章
相關標籤/搜索