Since the company has been using API22 [Android 5.0] as the target version of the project, many functions on API22 cannot be used, such as software management permissions. Meanwhile, the current recommended TargetVersion is API25, also known as Android7.0, so I decided to improve the TargetVersion of the project in the subsequent framework building.

Introduction to the


The new permission mechanism better protects users’ privacy. Google divides Permissions into two categories: Normal Permissions, which generally do not involve users’ privacy and do not require users’ authorization, such as mobile phone vibration and Internet access. The other category is Dangerous Permission, which generally involves user privacy and requires user authorization, such as reading SDcard and accessing the address book.

Ordinary permissions

ACCESS_LOCATION_EXTRA_COMMANDS ACCESS_NETWORK_STATE ACCESS_NOTIFICATION_POLICY ACCESS_WIFI_STATE BLUETOOTH BLUETOOTH_ADMIN BROADCAST_STICKY CHANGE_NETWORK_STATE CHANGE_WIFI_MULTICAST_STATE CHANGE_WIFI_STATE DISABLE_KEYGUARD EXPAND_STATUS_BAR GET_PACKAGE_SIZE INSTALL_SHORTCUT INTERNET KILL_BACKGROUND_PROCESSES MODIFY_AUDIO_SETTINGS NFC READ_SYNC_SETTINGS READ_SYNC_STATS RECEIVE_BOOT_COMPLETED REORDER_TASKS REQUEST_INSTALL_PACKAGES SET_ALARM SET_TIME_ZONE SET_WALLPAPER SET_WALLPAPER_HINTS TRANSMIT_IR UNINSTALL_SHORTCUT USE_FINGERPRINT VIBRATE WAKE_LOCK WRITE_SYNC_SETTINGS

Dangerous permissions

(This part is the part that we are using and need to deal with)


group:android.permission-group.CONTACTS


permission:android.permission.WRITE_CONTACTS


permission:android.permission.GET_ACCOUNTS


permission:android.permission.READ_CONTACTS

group:android.permission-group.PHONE

permission:android.permission.READ_CALL_LOG

permission:android.permission.READ_PHONE_STATE

permission:android.permission.CALL_PHONE

permission:android.permission.WRITE_CALL_LOG

permission:android.permission.USE_SIP

permission:android.permission.PROCESS_OUTGOING_CALLS

permission:com.android.voicemail.permission.ADD_VOICEMAIL

group:android.permission-group.CALENDAR

permission:android.permission.READ_CALENDAR

permission:android.permission.WRITE_CALENDAR

group:android.permission-group.CAMERA

permission:android.permission.CAMERA

group:android.permission-group.SENSORS

permission:android.permission.BODY_SENSORS

group:android.permission-group.LOCATION

permission:android.permission.ACCESS_FINE_LOCATION

permission:android.permission.ACCESS_COARSE_LOCATION

group:android.permission-group.STORAGE

permission:android.permission.READ_EXTERNAL_STORAGE

permission:android.permission.WRITE_EXTERNAL_STORAGE

group:android.permission-group.MICROPHONE

permission:android.permission.RECORD_AUDIO

group:android.permission-group.SMS permission:android.permission.READ_SMS permission:android.permission.RECEIVE_WAP_PUSH permission:android.permission.RECEIVE_MMS permission:android.permission.RECEIVE_SMS permission:android.permission.SEND_SMS permission:android.permission.READ_CELL_BROADCASTS

The new run-time permission mechanism is backward compatible. For versions below API23, there is no need to process dangerous permission applications, and it is still registered in the old way. However, for API23 and above, dynamic permission applications need to be made when such dangerous permissions are needed, otherwise they will not be used. Dangerous permissions are classified by group, which can be used when determining permissions. However, it is recommended to determine a single permission and apply for multiple permissions to ensure safety.

Related API introduction

1. Add required permissions to the AndroidManifest file


2. Check permissions














3. Permission request











4. Handle post-request callbacks



callback – do nothing








// Should we show an explanation?
if(ActivityCompat.shouldShowRequestPermissionRationale(thisActivity,Manifest.permission.READ_CONTACTS)){
// Show an expanation to the user *asynchronously* -- don't block
// this thread waiting for the user's response! After the user
// sees the explanation, try again to request the permission.
}Copy the code

If rejected again, have to do not use the permission, such as Tmall you do not allow to read SMS permission will exit. // No permission was obtained and the permission was denied. The last application will not be applied if the application fails.

// Did not obtain permission and permission was denied, the last request (failed to apply)if (ActivityCompat.shouldShowRequestPermissionRationale(this,
                Manifest.permission.WRITE_EXTERNAL_STORAGE)) {

            // Show an expanation to the user *asynchronously* -- don't block // this thread waiting for the user's response! After the user
            // sees the explanation, try again to request the permission.

        } else{// No access permission, Apply for permission to ActivityCompat. RequestPermissions (this, new String [] {the Manifest. Permission. WRITE_APN_SETTINGS}, Constant.number.PERMISSION_REQUEST_CODE); } } @Override public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @nonnull int[] grantResults {switch (requestCode) {// Write permission to disk succeeded, update APP, otherwise failed exitcase Constant.number.HUNDRED:
            if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                // permission was granted, yay! Do the
                // contacts-related task you need to do.
                updateApp(updateBean);
            } else {
                // permission denied, boo! Disable the
                // functionality that depends on this permission.
                    return;
            }
            break; // The disk read permission is successful. Check whether the disk has downloaded a new installation package. If yes, modify the text of the upgrade buttoncase Constant.number.HUNDRED_AND_ONE:
            if (grantResults.length > 0 && grantResults[1] == PackageManager.PERMISSION_GRANTED) {
                File file = new File(Constant.string.DOWNLOAD_PATH);
                File[] files = file.listFiles();
                if(null ! = files) {for (int i = 0, count = files.length; i < count; i++) {
                        String apkName = files[i].getName();
                        if (TextUtils.equals(apkName, SpUtil.getString(this, Constant.string.UPDATE_APP_NAME, Constant.string.DEFAULT_APP_NAME)) && SpUtil.getLong(this, Constant.string.DOWNLOAD_APK_SIZE + Constant.string.DEFAULT_APP_NAME, Constant.number.ZERO) == files[i].length()) {
                            mIsUpdateComplete = true; mBtnUpdate.setText(getResources().getString(R.string.install_now)); }}}}break;
    }
    super.onRequestPermissionsResult(requestCode, permissions, grantResults);
}Copy the code

The final test, see the results


Hard disk read and write permission








Android helicopter