[Android development tips] Android 6.0 dynamically obtains permissions and handles them at runtime

With the release of Android 6.0, some changes have also been brought. Here we mainly talk about the handling of runtime permissions in Android 6.0.

About runtime permissions

In the old permission management system, the permission is only asked to the user once when the App is installed, and the user can install the permission App only after they agree to the permission (some in-depth customization systems also say). Once the App is installed, it can secretly do something unknown.

At the beginning of Android 6.0, App can be installed directly. When App is running, users will be asked to grant permissions one by one. A Dialog box will pop up to let users choose whether to authorize a permission to App (this Dialog can't be customized by developers). When App needs users to grant inappropriate permissions, users can refuse, and users can also face the permissions of each App on the settings page Bank management.

Special note: this dialog box is not automatically popped up by the system when the developer calls the function of a certain permission, but needs to be manually called by the developer. If you call directly without applying for permission, the App will crash.

Permissions requested at run time are required

CALENDAR(Calendar) 
 READ_CALENDAR
 WRITE_CALENDAR
CAMERA(Camera) 
 CAMERA
CONTACTS(Contact) 
 READ_CONTACTS
 WRITE_CONTACTS
 GET_ACCOUNTS
LOCATION(Location) 
 ACCESS_FINE_LOCATION
 ACCESS_COARSE_LOCATION
MICROPHONE(Microphone) 
 RECORD_AUDIO
PHONE(Mobile) 
 READ_PHONE_STATE
 CALL_PHONE
 READ_CALL_LOG
 WRITE_CALL_LOG
 ADD_VOICEMAIL
 USE_SIP
 PROCESS_OUTGOING_CALLS
SENSORS(Sensors) 
 BODY_SENSORS
SMS(SMS) 
 SEND_SMS
 RECEIVE_SMS
 READ_SMS
 RECEIVE_WAP_PUSH
 RECEIVE_MMS
STORAGE(Memory card) 
 READ_EXTERNAL_STORAGE
 WRITE_EXTERNAL_STORAGE

Specific adaptation operation code:

public interface PermissionListener {
    void granted();
    void denied(List<String> deniedList);
}
public class BaseActivity extends Activity {
    private static PermissionListener mListener;
    private static Activity activity ;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        activity = this ;
    }
    /**
     * Application authority
     */
    public static void requestRuntimePermissions(
            String[] permissions, PermissionListener listener) {
        mListener = listener;
        List<String> permissionList = new ArrayList<>();
        // Traverse the permissions of each application and put the failed permissions in the collection
        for (String permission : permissions) {
            if (ContextCompat.checkSelfPermission(activity, permission) !=
                    PackageManager.PERMISSION_GRANTED) {
                permissionList.add(permission);
            } else {
                mListener.granted();
            }
        }
        // Application authority
        if (!permissionList.isEmpty()) {
            ActivityCompat.requestPermissions(activity,
                    permissionList.toArray(new String[permissionList.size()]), 1);
        }
    }

    /**
     * Post application processing
     */
    @Override
    public void onRequestPermissionsResult(
            int requestCode, String[] permissions, int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        if (grantResults.length > 0) {
            List<String> deniedList = new ArrayList<>();
            // Traverse all the requested permissions and put the denied permissions into the collection
            for (int i = 0; i < grantResults.length; i++) {
                int grantResult = grantResults[i];
                if (grantResult == PackageManager.PERMISSION_GRANTED) {
                    mListener.granted();
                } else {
                    deniedList.add(permissions[i]);
                }
            }
            if (!deniedList.isEmpty()) {
                mListener.denied(deniedList);
            }
        }
    }
        }

The specific use is as follows:

private static String[] PERMISSIONS_STORAGE = {
            "android.permission.READ_EXTERNAL_STORAGE",
            "android.permission.WRITE_EXTERNAL_STORAGE",
            "android.permission.ACCESS_FINE_LOCATION"};


if (Build.VERSION.SDK_INT >= 23) {//Judge whether the current system is Android 6.0
            requestRuntimePermissions(PERMISSIONS_STORAGE, new PermissionListener() {
                @Override
                public void granted() {
                    //Permission application passed
                }

                @Override
                public void denied(List<String> deniedList) {
                    //Permission application failed
                    for (String denied : deniedList) {
                        if (denied.equals("android.permission.ACCESS_FINE_LOCATION")) {
                            CustomToast.INSTANCE.showToast(SDK_WebApp.this, "Failed to locate, please check whether to open the locating permission!");
                        } else {
                            CustomToast.INSTANCE.showToast(SDK_WebApp.this, "No file read / write permission,Please check        Check if it is open!");
                        }
                    }
                }
            });
        }

Tags: Android Mobile

Posted on Sun, 03 May 2020 07:05:02 -0400 by njm