Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to understand the permission problem in Android

2025-01-22 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

Today, I will talk to you about how to understand the permissions in Android. Many people may not know much about it. In order to make you understand better, the editor has summarized the following for you. I hope you can get something according to this article.

In the Android program, it is necessary to declare permissions when performing 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. But in the Android6.0 version or above, Android divides permissions into normal permissions and dangerous permissions, in which the use of ordinary permissions is the same as the previous Android version, which can be declared directly in the AndroidManifest file, and the system will automatically authorize it for us, but dangerous permissions should not only be declared in the AndroidManifest file, but also need to be judged by code when using permissions and deal with the results of user authorization. So which permissions are dangerous permissions? all the dangerous permissions of Android are listed below, so except for the dangerous permissions in the following table, all the other permissions are Android normal permissions:

We can jot down the dangerous permissions in the way of permission groups:

Read and write calendar: android.permission.READ_CALENDAR android.permission.WRITE_CALENDAR

Use camera: android.permission.CAMERA

Read-write contact: android.permission.READ_CONTACTS android.permission.WRITE_CONTACTS

Android.permission.GET_ACCOUNTS

Location service: android.permission.ACCESS_FINE_LOCATION ACCESS_COARSE_LOCATION

Tel: 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 Sensor: 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 mobile phone storage: android.permission.READ_EXTERNAL_STORAGE android.permission.WRITE_EXTERNAL_STORAGE

Okay, these are all the dangerous permissions of Android. When we use these permissions, we not only need to declare them in the AndroidManifest file, but also need to deal with the authorization of the user in the code. Let's take a simple example to see how to deal with dangerous permissions in the code:

Create a new Android project:

Activity_main.xml:

Very simple layout file, one line of EditText control is used for phone number, one line of Button is used to make calls::

Then there 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 to make phone calls to our application. * if so, call directly. If not, apply to the user. And callback onRequestPermissionResult method * / if (ContextCompat.checkSelfPermission (MainActivity.this, Manifest.permission.CALL_PHONE)! = PackageManager.PERMISSION_GRANTED) {/ * * below is the permission application, the second parameter is filled in the permission name, if there are multiple permissions, then the second parameter String array adds 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 action will call this method. Regardless of whether the user allows or forbids it, * We need to make the 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, "call permission denied by the user", Toast.LENGTH_SHORT). Show ();}}

In MainActivity.java, we dealt with the permissions we needed.

Finally, don't forget to declare phone permissions in the AndroidManifest file:

Let's run it:

Let's enter a number and click the "Dial" button:

Since this is the first time we have run this program, the user has not authorized our program before, so the permission application dialog box appears, and we click DENY (No):

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

Successfully enter the dialing interface and dial the phone number we entered!

When we allow it, the program no longer needs to be authorized by the user, that is, it can make a call directly (unless the user withdraws our permission to make a call in application management).

After reading the above, do you have any further understanding of how to understand the permissions in Android? If you want to know more knowledge or related content, please follow the industry information channel, thank you for your support.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report