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: - Read and write calendar: android.permission.READ_CALENDAR android.permission.WRITE_CALENDAR
- Use camera: android.permission.CAMERA
- Read and write contacts: android.permission.READ_CONTACTS android.permission.WRITE_CONTACTS
- android.permission.GET_ACCOUNTS
- Location services: android.permission.ACCESS_FINE_LOCATION ACCESS_COARSE_LOCATION
- 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
- Use sensors: android.permission.BODY_SENSORS
- SMS: android.permission.SEND_SMS android.permission.RECEIVE_SMS android.permission.READ_SMS android.permission.RECEIVE_WAP_PUSH RECEIVE_MMS
- 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: - <?xml version= "1.0" encoding= "utf-8" ?>
- <LinearLayout xmlns:android= "http://schemas.android.com/apk/res/android"
- xmlns:tools= "http://schemas.android.com/tools"
- android:id= "@+id/activity_main"
- android:layout_width= "match_parent"
- android:layout_height= "match_parent"
- android:orientation= "vertical"
- android:gravity= "center_horizontal"
- tools:context= "com.example.administrator.blogandroidpermissiondeal.MainActivity" >
- <EditText
- android:id= "@+id/phonenumberEditText"
- android:layout_width= "wrap_content"
- android:layout_height= "wrap_content"
- android:hint= "Enter the phone number you want to call" />
- <Button
- android:id= "@+id/callPhoneButton"
- android:layout_width= "wrap_content"
- android:layout_height= "wrap_content"
- android:text= "Call" />
- </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: - package com.example.administrator.blogandroidpermissiondeal;
- import android.Manifest;
- import android.content.Intent;
- import android.content.pm.PackageManager;
- import android.net.Uri;
- import android.support.annotation.NonNull;
- import android.support.v4.app.ActivityCompat;
- import android.support.v4.content.ContextCompat;
- import android.support.v7.app.AppCompatActivity;
- import android.os.Bundle;
- import android. view . View ;
- import android.widget.Button;
- import android.widget.EditText;
- import android.widget.Toast;
- public class MainActivity extends AppCompatActivity {
- private Button button = null ;
- private EditText editText = null ;
- private static final int PERMISSION_REQUEST_CODE = 1;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- editText = (EditText) findViewById(R.id.phonenumberEditText);
- button = (Button) findViewById(R.id.callPhoneButton);
- button.setOnClickListener(new View .OnClickListener() {
- @Override
- public void onClick( View v) {
- /*
- * First determine whether the user has previously allowed our application to make calls.
- * If yes, then call directly, if not, then apply to the user and call back onRequestPermissionResult method
- */
- if(ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.CALL_PHONE)
- != PackageManager.PERMISSION_GRANTED) {
- /*
- * 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
- */
- ActivityCompat.requestPermissions(MainActivity.this,
- new String[]{Manifest.permission.CALL_PHONE}, PERMISSION_REQUEST_CODE);
- } else {
- callPhonenumber();
- }
- }
- });
- }
- private void callPhonenumber() {
- try {
- Intent intent = new Intent(Intent.ACTION_CALL);
- intent.setData(Uri.parse( "tel:" + editText.getText().toString()));
- startActivity(intent);
- }catch (Exception e) {
- e.printStackTrace();
- }
- }
- /*
- * 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.
- * We need to make corresponding processing in this method
- */
- @Override
- public void onRequestPermissionsResult( int requestCode, @NonNull String[] permissions, @NonNull int [] grantResults) {
- super.onRequestPermissionsResult(requestCode, permissions, grantResults);
- switch (requestCode) {
- /*
- * Judge the incoming requestCode
- */
- case PERMISSION_REQUEST_CODE:
- // If the user authorizes
- if(grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
- callPhonenumber();
- } else {
- Toast.makeText(this, "Phone permission has been denied by the user" , Toast.LENGTH_SHORT).show();
- }
- }
- }
- }
In MainActivity.java, we handle the permissions we need. Finally, don't forget to declare the calling permission in the AndroidManifest file: - <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). |