How to use EasyPermissions

Starting with Android 6.0 (API level 23), users begin to grant permissions to applications when they are running, rather than when they are installed...

Starting with Android 6.0 (API level 23), users begin to grant permissions to applications when they are running, rather than when they are installed.

System authority can be divided into two types: normal authority and dangerous authority
-Normal permissions do not directly bring risks to users' privacy. If your app lists a normal permission in its list, it will be granted automatically.
-Dangerous permissions grant the app access to the user's confidential data. If your app lists a normal permission in its list, it will be granted automatically. If you list dangerous permissions, users must explicitly approve your app to use them.

1.Google's original method of requesting permission

//Normal access private void checkPermissionForNormal() { //Determine whether to agree to this authority if (ContextCompat.checkSelfPermission(this, Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) { //Application authority ActivityCompat.requestPermissions(this, new String[], 100); } } else { Toast.makeText(this, "Authorized CALL_PHONE", Toast.LENGTH_LONG).show(); } }

Callback of permission

@Override public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) { switch (requestCode) { case 100: { //If the returned result array is greater than 0, there is a result if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) { Toast.makeText(this, "Permission granted", Toast.LENGTH_SHORT).show(); callPhone(); } else { Toast.makeText(this, "Permission denied", Toast.LENGTH_SHORT).show(); } return; } } }

2. Encapsulated tripartite Library - EasyPermissions
This is a very simple application for permission

  • Import into storage
compile 'pub.devrel:easypermissions:0.1.5'
  • 1
  • Code block
  • package com.example.easypermissions_demo; import android.Manifest; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.util.Log; import android.view.View; import android.widget.Button; import android.widget.Toast; import java.util.List; import pub.devrel.easypermissions.EasyPermissions; public class MainActivity extends AppCompatActivity implements View.OnClickListener,EasyPermissions.PermissionCallbacks { private static final String TAG = "MainActivity"; private static final int CAMERA_REQUESTCODE = 101; private static final int LOCATION_CONTACTS_REQUESTCODE = 102; private Button button_camera; private Button button_location_and_contacts; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); button_camera = (Button) findViewById(R.id.button_camera); button_location_and_contacts = (Button) findViewById(R.id.button_location_and_contacts); button_camera.setOnClickListener(this); button_location_and_contacts.setOnClickListener(this); } @Override public void onClick(View v) { switch (v.getId()) { case R.id.button_camera: requestCamera(); break; case R.id.button_location_and_contacts: requestLocationAndContacts(); break; } } public void requestCamera() { if (EasyPermissions.hasPermissions(this, Manifest.permission.CAMERA)) { Toast.makeText(this, "Authorized Camera", Toast.LENGTH_LONG).show(); } else { // request for one permission EasyPermissions.requestPermissions(this, getString(R.string.rationale_camera), CAMERA_REQUESTCODE, Manifest.permission.CAMERA); } } public void requestLocationAndContacts() { String[] perms = { Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.READ_CONTACTS }; if (EasyPermissions.hasPermissions(this, perms)) { Toast.makeText(this, "Authorized contact and location information", Toast.LENGTH_LONG).show(); } else { // request for both permissions EasyPermissions.requestPermissions(this, getString(R.string.rationale_location_contacts), LOCATION_CONTACTS_REQUESTCODE, perms); } } @Override public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) { super.onRequestPermissionsResult(requestCode, permissions, grantResults); // EasyPermissions permission processing request result Log.i(TAG,"onRequestPermissionsResult:"+ requestCode); EasyPermissions.onRequestPermissionsResult(requestCode, permissions, grantResults, this); } //Agree to authorize @Override public void onPermissionsGranted(int requestCode, List<String> perms) { Log.d(TAG, "onPermissionsGranted:" + requestCode + ":" + perms.size()); } //Deny authorization @Override public void onPermissionsDenied(int requestCode, List<String> perms) { Log.d(TAG, "onPermissionsDenied:" + requestCode + ":" + perms.size()); } }

Original address: https://blog.csdn.net/zhu522959034/article/details/73647860

4 January 2020, 07:05 | Views: 3337

Add new comment

For adding a comment, please log in
or create account

0 comments