Permission issues in Android

Permission issues in Android

In Android programs, permissions must be declared when executing functions such as accessing the network and reading contacts. When the Android system version is less than 6.0, all permissions only need to be declared in the AndroidManifest file to use the corresponding functions. However, in Android version 6.0 and above, Android divides permissions into ordinary permissions and dangerous permissions. The use of ordinary permissions is the same as in previous Android versions. Just declare them in the AndroidManifest file and the system will automatically authorize them for us. However, dangerous permissions must not only be declared in the AndroidManifest file, but also need to be determined by code when using permissions and the results of user authorization must be processed accordingly. So which permissions are dangerous permissions? All dangerous permissions on Android are given below. Except for the dangerous permissions in the table below, the other permissions are all ordinary permissions on Android:

We can roughly remember dangerous permissions by permission groups:

  1. Read and write calendar: android.permission.READ_CALENDAR android.permission.WRITE_CALENDAR
  2. Use camera: android.permission.CAMERA
  3. Read and write contacts: android.permission.READ_CONTACTS android.permission.WRITE_CONTACTS
  • android.permission.GET_ACCOUNTS
  1. Location services: android.permission.ACCESS_FINE_LOCATION ACCESS_COARSE_LOCATION
  2. Phone: android.permission.READ_PHONE_STATE android.permission.CALL_PHONE android.permission.READ_CALL_LOG android.permission.WRITE_CALL_LOG android.permission.ADD_VOICEMAIL android.permission.USE_SIP android.permission.PROGRESS_OUTGOING_CALLS
  3. Use sensors: android.permission.BODY_SENSORS
  4. SMS: android.permission.SEND_SMS android.permission.RECEIVE_SMS android.permission.READ_SMS android.permission.RECEIVE_WAP_PUSH RECEIVE_MMS
  5. Read and write phone storage: android.permission.READ_EXTERNAL_STORAGE android.permission.WRITE_EXTERNAL_STORAGE

Well, the above are all dangerous permissions of Android. When we use these permissions, we must not only declare them in the AndroidManifest file, but also handle the user's authorization in the code. Here is a simple example to see how to handle dangerous permissions in the code:

Create a new Android project:

activity_main.xml:

  1. <?xml version= "1.0" encoding= "utf-8" ?>
  2. <LinearLayout xmlns:android= "http://schemas.android.com/apk/res/android"  
  3. xmlns:tools= "http://schemas.android.com/tools"  
  4. android:id= "@+id/activity_main"  
  5. android:layout_width= "match_parent"  
  6. android:layout_height= "match_parent"  
  7. android:orientation= "vertical"  
  8. android:gravity= "center_horizontal"  
  9. tools:context= "com.example.administrator.blogandroidpermissiondeal.MainActivity" >
  10. <EditText
  11. android:id= "@+id/phonenumberEditText"  
  12. android:layout_width= "wrap_content"  
  13. android:layout_height= "wrap_content"  
  14. android:hint= "Enter the phone number you want to call" />
  15. <Button
  16. android:id= "@+id/callPhoneButton"  
  17. android:layout_width= "wrap_content"  
  18. android:layout_height= "wrap_content"  
  19. android:text= "Call" />
  20. </LinearLayout>

A very simple layout file, one row of EditText controls for the phone number, and one row of Buttons for making calls::

Next is MainActivity.java:

  1. package com.example.administrator.blogandroidpermissiondeal;
  2. import android.Manifest;
  3. import android.content.Intent;
  4. import android.content.pm.PackageManager;
  5. import android.net.Uri;
  6. import android.support.annotation.NonNull;
  7. import android.support.v4.app.ActivityCompat;
  8. import android.support.v4.content.ContextCompat;
  9. import android.support.v7.app.AppCompatActivity;
  10. import android.os.Bundle;
  11. import android. view . View ;
  12. import android.widget.Button;
  13. import android.widget.EditText;
  14. import android.widget.Toast;
  15. public class MainActivity extends AppCompatActivity {
  16. private Button button = null ;
  17. private EditText editText = null ;
  18. private static final int PERMISSION_REQUEST_CODE = 1;
  19. @Override
  20. protected void onCreate(Bundle savedInstanceState) {
  21. super.onCreate(savedInstanceState);
  22. setContentView(R.layout.activity_main);
  23. editText = (EditText) findViewById(R.id.phonenumberEditText);
  24. button = (Button) findViewById(R.id.callPhoneButton);
  25. button.setOnClickListener(new View .OnClickListener() {
  26. @Override
  27. public void onClick( View v) {
  28. /*
  29. * First determine whether the user has previously allowed our application to make calls.
  30. * If yes, then call directly, if not, then apply to the user and call back onRequestPermissionResult method
  31. */
  32. if(ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.CALL_PHONE)
  33. != PackageManager.PERMISSION_GRANTED) {
  34. /*
  35. * The following is an application for permissions. The second parameter is filled with the permission name. If there are multiple permissions, then the second parameter String array will add multiple permission parameters
  36. */
  37. ActivityCompat.requestPermissions(MainActivity.this,
  38. new String[]{Manifest.permission.CALL_PHONE}, PERMISSION_REQUEST_CODE);
  39. } else {
  40. callPhonenumber();
  41. }
  42. }
  43. });
  44. }
  45. private void callPhonenumber() {
  46. try {
  47. Intent intent = new Intent(Intent.ACTION_CALL);
  48. intent.setData(Uri.parse( "tel:" + editText.getText().toString()));
  49. startActivity(intent);
  50. }catch (Exception e) {
  51. e.printStackTrace();
  52. }
  53. }
  54. /*
  55. * When we apply for permission from the user, the result of the user's operation will call this method, regardless of whether the user allows or prohibits it.
  56. * We need to make corresponding processing in this method
  57. */
  58. @Override
  59. public void onRequestPermissionsResult( int requestCode, @NonNull String[] permissions, @NonNull int [] grantResults) {
  60. super.onRequestPermissionsResult(requestCode, permissions, grantResults);
  61. switch (requestCode) {
  62. /*
  63. * Judge the incoming requestCode
  64. */
  65. case PERMISSION_REQUEST_CODE:
  66. // If the user authorizes
  67. if(grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
  68. callPhonenumber();
  69. } else {
  70. Toast.makeText(this, "Phone permission has been denied by the user" , Toast.LENGTH_SHORT).show();
  71. }
  72. }
  73. }
  74. }

In MainActivity.java, we handle the permissions we need.

Finally, don't forget to declare the calling permission in the AndroidManifest file:

  1. <uses-permission android: name = "android.permission.CALL_PHONE" />

Let's run it:

We enter a number and click the "Call" button:

Because we are running this program for the first time, the user has not authorized our program before, so a permission request dialog box appears, and we click DENY:

The prompt box pops up successfully, so let's try again and click ALLOW:

Successfully entered the dialing interface and dialed the phone number we entered!

Once we allow it, the program will no longer need user authorization and can make calls directly (unless the user revokes our calling permission in the application management).

<<:  Google's hardware lineup gets a major update! There are as many flaws as highlights

>>:  The misunderstood dispute between BAT and other small apps

Recommend

Basic dance moves lead to lower limb paralysis? Dangerous "bending back"

|| || Written by reporter Lv Bingxin Photo and te...

Content operation: How to build a complete UGC content ecosystem?

What steps are needed to build a complete UGC com...

International Energy Agency: Global Renewable Energy Market Report May 2022

A survey report recently released by the Internat...

5 angles to make high-traffic Tik Tok videos!

There are two main points to focus on when doing ...

Ctrip's exploration and practice of physical linking technology

​Author|Ctrip Travel AI R&D team is committed...

Writing Scripts in Swift: Git Hooks

Preface This week, I decided to improve my Git wo...

New media planning and event operation tool library!

Marketing operations are inseparable from activit...

The Ice Bucket Game is becoming increasingly alien to charity

The "Ice Bucket Challenge" to raise awa...