Android Dynamic Get Permissions

Source: Internet
Author: User

changes in Android permissions

In previous versions of Android6.0, Android's permissions were all configured at the time of installation. However, this often leads to some security problems.

Google's solution:

There are two main categories of permissions in Android:

Normal: Permissions do not directly relate to the user's privacy. If your app configures this class of permissions in the manifest manifest file, the system automatically agrees.

Dangerous permissions allow the app to access the user's privacy data. If your app is configured with this class of permissions in manifest, it will take a long time for the user to agree to such permissions.

Note: If your app requires a dangerous permission, you must check for this permission every time you perform an action that requires this permission. The application user can always cancel this permission, and cannot consider agreeing to have it permanently.

Demo Code:
1  PackageCom.example.it.android_callphone;2 3 Importandroid.content.Intent;4 ImportAndroid.content.pm.PackageManager;5 ImportAndroid.net.Uri;6 ImportAndroid.support.annotation.NonNull;7 ImportAndroid.support.v4.app.ActivityCompat;8 ImportAndroid.support.v4.content.ContextCompat;9 Importandroid.support.v7.app.AppCompatActivity;Ten ImportAndroid.os.Bundle; One ImportAndroid.view.View; A ImportAndroid.widget.Button; - ImportAndroid.widget.EditText; - ImportAndroid.widget.Toast; the  - ImportJava.net.URI; - Importjava.security.Permission; - Importjava.util.jar.Manifest; +  -  Public classMainactivityextendsappcompatactivity { +  A     intRequest_call_phone_permission=0; at Intent Intent; - EditText Txtnumber; - @Override -     protected voidonCreate (Bundle savedinstancestate) { -         Super. OnCreate (savedinstancestate); - Setcontentview (r.layout.activity_main); in  -Button Btncall =(Button) Findviewbyid (r.id.btncall); toTxtnumber =(EditText) Findviewbyid (r.id.txtnumber); +  -Btncall.setonclicklistener (NewView.onclicklistener () { the @Override *              Public voidOnClick (View v) { $                 //If you have permission to execute directlyPanax Notoginseng                 if(Contextcompat.checkselfpermission (mainactivity. ThisAndroid. Manifest.permission.CALL_PHONE) = =packagemanager.permission_granted) { -Intent =NewIntent (); the                     //Add a call action + intent.setaction (intent.action_call); A                     //Add Data theString number =Txtnumber.gettext (). toString (). Trim (); +Intent.setdata (Uri.parse ("Tel:" +Number )); - startactivity (intent); $                 } $                 //If you do not have permission then request permission -                 Else{ -Activitycompat.requestpermissions (mainactivity. This,Newstring[]{android. manifest.permission.call_phone},request_call_phone_permission); the                 } -             }Wuyi         }); the     } -  Wu     /* - * This callback method is executed when the request gets permission to execute its own business logic About     * */ $ @Override -      Public voidOnrequestpermissionsresult (intRequestcode, @NonNull string[] permissions, @NonNullint[] grantresults) { -         if(requestcode== This. Request_call_phone_permission) { -  A             if(grantresults[0]==packagemanager.permission_granted) { +                 //execute your own business logic the startactivity (intent); -}Else{ $Toast.maketext ( This, "Permission Denied", Toast.length_short). Show (); the             } the         } the         Super. Onrequestpermissionsresult (requestcode,permissions,grantresults); the     } -}

Android Dynamic Get Permissions

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.