Android 6.0: Dynamic permission management solution
For Android 6.0Version (Api 23) has introduced many new features, greatly improving the user experience and bringing new burdens to programmers.Dynamic permission managementThis is the case. On the one hand, it makes it easier for users to control their privacy and on the other hand, it is necessary to adapt to application permissions. The times are always evolving and programs are always people-oriented. Let's add dynamic permission management for applications! A very good solution is provided here.
Android system includesDefault authorization prompt boxBut we still need to set our own page. The reason is that the authorization box provided by the system containsNo longer promptOption. If the user chooses, the authorization prompt cannot be triggered. Use the custom prompt page to give the user manual authorization modification guide.
GitHub in this example.
In Api 23, permissions must be dynamically obtained and the core permissions must meet. Standard Process:
If you click,No longer prompt, The System Authorization pop-up window will not pop up. The process will change:
This is the process. Let's look at the code. <喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> VcD4NCjxociAvPg0KPGgyIGlkPQ = "1-permission"> 1. Permission
In AndroidManifest, add two permissions,RecordingAndModify volume.
<code class="hljs xml"> <!--{cke_protected}{C}%3C!%2D%2D%E5%8D%B1%E9%99%A9%E6%9D%83%E9%99%90%2D%2D%3E--> <uses-permission android:name="android.permission.RECORD_AUDIO"> <!--{cke_protected}{C}%3C!%2D%2D%E4%B8%80%E8%88%AC%E6%9D%83%E9%99%90%2D%2D%3E--> <uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS"></uses-permission></uses-permission></code>
Dangerous permissions must be authorized. General permissions are not required.
Check permission
/*** Tool class for permission check *
* Created by wangchenlong on 16/1/26. */public class PermissionsChecker {private final Context mContext; public PermissionsChecker (Context context) {mContext = context. getApplicationContext ();} // judge the permission set public boolean lacksPermissions (String... permissions) {for (String permission: permissions) {if (lacksPermission (permission) {return true ;}} return false ;} // determine whether the permission is missing. private boolean lacksPermission (String permission) {return ContextCompat. checkSelfPermission (mContext, permission) = PackageManager. PERMISSION_DENIED ;}}
2. Home Page
Assume that the homepage requires permissions. Before the page is displayed, that is, when onResume is used, the permission is checked,
If the permission is missing, the permission acquisition page is displayed. If the permission is denied, the permission is directly disabled.
Public class MainActivity extends AppCompatActivity {private static final int REQUEST_CODE = 0; // Request Code // all required PERMISSIONS static final String [] PERMISSIONS = new String [] {Manifest. permission. RECORD_AUDIO, Manifest. permission. MODIFY_AUDIO_SETTINGS}; @ Bind (R. id. main_t_toolbar) Toolbar mTToolbar; private PermissionsChecker mPermissionsChecker; // permission detector @ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); ButterKnife. bind (this); setsuppactionactionbar (mTToolbar); mPermissionsChecker = new PermissionsChecker (this) ;}@ Override protected void onResume () {super. onResume (); // if (mPermissionsChecker. lacksPermissions (PERMISSIONS) {startPermissionsActivity () ;}} private void startPermissionsActivity () {PermissionsActivity. startActivityForResult (this, REQUEST_CODE, PERMISSIONS);} @ Override protected void onActivityResult (int requestCode, int resultCode, Intent data) {super. onActivityResult (requestCode, resultCode, data); // when the request is denied, the page is closed. if (requestCode = REQUEST_CODE & resultCode = PermissionsActivity. PERMISSIONS_DENIED) {finish ();}}}
The core permissions must be met. For example, camera permissions are required. If the user is not authorized, the camera permissions are directly disabled.
3. Authorization page
On the authorization page, use the default authorization page of the system. When the user rejects the authorization page, instruct the user to manually set it. When the user fails to perform the operation again, the system returns the continue prompt. when you exit the authorization page manually, send a notification of failed authorization to the use page.
/*** Permission acquisition page *
* Created by wangchenlong on 16/1/26. */public class PermissionsActivity extends AppCompatActivity {public static final int PERMISSIONS_GRANTED = 0; // permission authorization public static final int PERMISSIONS_DENIED = 1; // permission rejection private static final int PERMISSION_REQUEST_CODE = 0; // private static final String EXTRA_PERMISSIONS = "me. chunyu. clwang. permission. extra_permission "; // permission parameter private stati C final String PACKAGE_URL_SCHEME = "package:"; // private PermissionsChecker mChecker; // permission detector private boolean isRequireCheck; // whether system permission check is required // public interface of the current permission page is started public static void startActivityForResult (Activity activity, int requestCode, String... permissions) {Intent intent = new Intent (activity, PermissionsActivity. class); intent. putExtra (EXTRA_PERMISSIONS, permissions); ActivityCompat. start ActivityForResult (activity, intent, requestCode, null) ;}@ Override protected void onCreate (@ Nullable Bundle savedInstanceState) {super. onCreate (savedInstanceState); if (getIntent () = null |! GetIntent (). hasExtra (EXTRA_PERMISSIONS) {throw new RuntimeException ("PermissionsActivity needs to be started using the static startActivityForResult method! ");} SetContentView (R. layout. activity_permissions); mChecker = new PermissionsChecker (this); isRequireCheck = true ;}@ Override protected void onResume () {super. onResume (); if (isRequireCheck) {String [] permissions = getPermissions (); if (mChecker. lacksPermissions (permissions) {requestPermissions (permissions); // request permission} else {allPermissionsGranted (); // all permissions have been obtained} else {isRequireCheck = true ;}} // return the passed permission parameter private String [] getPermissions () {return getIntent (). getStringArrayExtra (EXTRA_PERMISSIONS);} // The request permission is compatible with the private void requestPermissions (String... permissions) {ActivityCompat. requestPermissions (this, permissions, PERMISSION_REQUEST_CODE);} // you have obtained the private void allPermissionsGranted () {setResult (PERMISSIONS_GRANTED); finish ();} /*** process user permissions. * If you obtain all the permissions, go directly. * If the permission is missing, the prompt is Dialog. ** @ param requestCode Request Code * @ param permissions permission * @ param grantResults result */@ Override public void onRequestPermissionsResult (int requestCode, @ NonNull String [] permissions, @ NonNull int [] grantResults) {if (requestCode = grants & hasAllPermissionsGranted (grantResults) {isRequireCheck = true; allPermissionsGranted ();} else {isRequireCheck = false; showMissingPermissionDialog () ;}// private boolean hasAllPermissionsGranted (@ NonNull int [] grantResults) {for (int grantResult: grantResults) {if (grantResult = PackageManager. PERMISSION_DENIED) {return false ;}} return true ;}// the private void showMissingPermissionDialog () {AlertDialog is displayed when the permission is missing. builder builder = new AlertDialog. builder (PermissionsActivity. this); builder. setTitle (R. string. help); builder. setMessage (R. string. string_help_text); // reject and exit the Application builder. setNegativeButton (R. string. quit, new DialogInterface. onClickListener () {@ Override public void onClick (DialogInterface dialog, int which) {setResult (PERMISSIONS_DENIED); finish () ;}}); builder. setPositiveButton (R. string. settings, new DialogInterface. onClickListener () {@ Override public void onClick (DialogInterface dialog, int which) {startdeleettings () ;}}); builder. show () ;}// set private void startreceivettings () {Intent intent = new Intent (Settings. ACTION_APPLICATION_DETAILS_SETTINGS); intent. setData (Uri. parse (PACKAGE_URL_SCHEME + getPackageName (); startActivity (intent );}}
Pay attention to the use of the isRequireCheck parameter to prevent overlap with the system prompt box.
System Authorization prompt: ActivityCompat. requestPermissions and ActivityCompat are compatible with earlier versions.
Effect
This is the key part. Although dynamic permission authorization brings some trouble to programmers, it is still necessary for users. We should also welcome it. After all, every programmer is half a product manager.
OK, that's all! Enjoy it.