Detailed explanation of Android application adapting to Android 7.0 permission requirements

  • 2021-10-13 08:31:00
  • OfStack

For Android 6.0 or below, the permission requirement for Android application is obtained directly by default when installing the application; However, for Android 6.0 and above, there are higher requirements for obtaining application rights. The application can only run normally if it obtains relevant permissions. On the contrary, if the user does not give relevant permissions, the application directly exits or even crashes, and cannot run normally.

For details, please refer to the following case codes for obtaining mobile phone positioning, mobile phone read-write storage and mobile phone status permissions:


public class AppMainActivity extends AppCompatActivity {
 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  // In App Request permission is required to perform user actions in 
  getPermissionToReadUserContacts();
 }
 // Defining a request 
 private static final int READ_CONTACTS_REQUEST = 1;
 // Ask when the user performs an operation that requires permission 
 public void getPermissionToReadUserContacts() {
  /**
   * 1) Use ContextCompat.chefkSelfPermission(), Because Context.permission
   *  Only used in lollipop system 
   * 2 ) Always check permissions (even if they are granted) because users may remove your permissions from settings */
  if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION)
    != PackageManager.PERMISSION_GRANTED ||
    ContextCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE)
      != PackageManager.PERMISSION_GRANTED ||
    ContextCompat.checkSelfPermission(this, Manifest.permission.READ_PHONE_STATE)
      != PackageManager.PERMISSION_GRANTED) {

   // The permission is to get, check whether the user has been questioned and rejected, and if so, give more 
   // Explanation 
   if (ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.ACCESS_FINE_LOCATION) ||
     ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.READ_EXTERNAL_STORAGE) ||
     ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.READ_PHONE_STATE)) {
    // Show why you need to read contacts on the interface 
    Toast.makeText(this, " Need Location Permission , Mobile phone permissions and storage permissions can work normally ", Toast.LENGTH_SHORT).show();
   }
   // Initiate a request to obtain user permission , You can request multiple permissions here 
   ActivityCompat.requestPermissions(this, new String[]{
       Manifest.permission.ACCESS_FINE_LOCATION,
       Manifest.permission.READ_EXTERNAL_STORAGE,
       Manifest.permission.READ_PHONE_STATE},
     READ_CONTACTS_REQUEST);
  }
 }
 // From requestPermissions() Method callback result 
 @Override
 public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
  // Make sure it's our request 
  if (requestCode == READ_CONTACTS_REQUEST) {
   if (grantResults.length == 3 && grantResults[0] == PackageManager.PERMISSION_GRANTED
     && grantResults[1] == PackageManager.PERMISSION_GRANTED
       && grantResults[2] == PackageManager.PERMISSION_GRANTED) {
    System.out.println(" The application permission was obtained successfully ");
   } else {
    System.out.println(" Application permission acquisition failed ");
   }
  } else {
   super.onRequestPermissionsResult(requestCode, permissions, grantResults);
  }
 }
}

Related articles: