I. Description
Android 6.0, Google will be divided into two categories of authority, a class is normal Permission, such permissions generally do not involve user privacy, is not required to authorize the user, such as mobile phone shake, access to the network, and the other is dangerous Permission, Generally involves the user's privacy, requires users to authorize, such as reading SDcard, access to contacts and so on.
Ii. List of dangerous rights
The following table lists all the dangerous permissions for Android and the remaining permissions are normal permissions
Note that each dangerous permission in the table belongs to a permission group, and we use the permission name when the permission is processed at run time, and when the user agrees to authorize, the other permissions in that permission group are also authorized
Iii. requesting permission when the program is running
Take calls as an example
The interface is simple, just a button, click on the dial 10086.
<?xml version= "1.0" encoding= "Utf-8"?>
<android.support.constraint.constraintlayout xmlns:android= "Http://schemas.android.com/apk/res/android"
xmlns:app= "Http://schemas.android.com/apk/res-auto"
Xmlns:tools= "Http://schemas.android.com/tools"
Android:layout_width= "Match_parent"
android:layout_height= "Match_parent"
Tools:context= ". Mainactivity ">
<button
Android:id= "@+id/make_call"
Android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content"
android:text= "Make_call"/>
</android.support.constraint.ConstraintLayout>
Code:
Package com.czlt.test.contentprovidertest;
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.util.Log;
Import Android.view.View;
Import Android.widget.Button;
Import Android.widget.Toast;
public class Mainactivity extends Appcompatactivity {
Private Button MakeCall;
@Override
protected void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Setcontentview (R.layout.activity_main);
MakeCall = Findviewbyid (R.id.make_call);
Makecall.setonclicklistener (New View.onclicklistener () {
@Override
public void OnClick (View v) {
Determine if the user has authorized the program. First parameter: Context second parameter: specific permission name.
if (Contextcompat.checkselfpermission (Mainactivity.this, Manifest.permission.CALL_PHONE)
! = packagemanager.permission_granted) {
Unauthorized, call the Activitycompat.requestpermissions () method to request authorization from the user
Parameters: 1. Activity instances, 2. Permission to apply an array group, 3. Request code, as long as it is a unique value
Activitycompat.requestpermissions (Mainactivity.this,
New String[]{manifest.permission.call_phone},
1);
} else {
Authorized, Direct call
Call ();
}
}
});
}
/**
- Calling
*/
private void Call () {
try {
Intent Intent = new Intent (Intent.action_call);
Intent.setdata (Uri.parse ("tel:10086"));
StartActivity (Intent);
} catch (SecurityException e) {
E.printstacktrace ();
}
}
/**
- Request authorization result
- @param requestcode request code, corresponding to the request code in Activitycompat.requestpermissions ()
- @param permissions Permissions List
- @param grantresults Request Result
*/
@Override
public void onrequestpermissionsresult (int requestcode, @NonNull string[] permissions,
@NonNull int[] grantresults) {
Switch (requestcode) {
Case 1:
if (grantresults.lengt H > 0 && grantresults[0] = = packagemanager.permission_granted) {
//user consent rights, make phone calls
call ();
} else {
//user does not agree to the permission, prompting the user
Toast.maketext (mainactivity.this, "You denied the permission", Toast.length_short). Show ();
}
Break;
Default:
break;
}
}
}
Of course, the configuration file Androidmanifest.xml, also add permissions
Android Runtime permissions (dangerous permissions)