Android Summary series: Android permissions

Source: Internet
Author: User

Permissions are a security mechanism. Android permissions are primarily used to restrict the use of certain restrictive features within the application and for component access between applications. In Android development, you will basically encounter the need for networking, and we know that we need to add the necessary privileges for networking:

1 <  android:name= "Android.permission.INTERNET"/>

In fact, in the development process, when we use some of the features of the system features, and such attributes need to include the appropriate permissions, if the corresponding declaration in the Androidmanifest.xml file, then the error will run and prompt: Java.lang.SecurityException: Permission denial ...

Based on this error, it is generally possible to add the appropriate permissions in Androidmanifest.xml by Uses-permission.

One, Android permissions list:

So what are the features of restricted access in Android? What are the required permission names for the specific attributes? It can be found in the official Android documentation.

Http://developer.android.com/reference/android/Manifest.permission.html

It is important to note that different permissions may correspond to different API levels, so compatibility issues may occur.

Second, the Android custom permissions:

Sometimes, we may encounter the following requirements scenario: When a user does something in an application, it launches another application, the most common one that opens another application directly and enters one of the activity (for example, a list of recommended apps in some apps, When the user clicks the program will first determine whether other applications have installed, if none prompts the user to download, if there is a direct access to open. Sometimes, when you need to be secure, you need to add restricted access restrictions to such operations. We have custom permissions available in Android.

In order to clarify the custom permissions, first, the access activity between different programs to increase the permission limit for example. Assume that there is mainactivity in application A, and that there are aactivity and bactivity in application B. Now I want to open the bactivity in B directly through the mainactivity in a.

So, if the permission is not considered, how does the mainactivity in a directly open the bactivity in B? Generally, you can do this in the following ways:

1  Public classMainactivityextendsActivity {2 3     Privatebutton button;4 5 @Override6     protected voidonCreate (Bundle savedinstancestate) {7         Super. OnCreate (savedinstancestate);8 Setcontentview (r.layout.activity_main);9         Tenbutton =(Button) Findviewbyid (R.id.button); OneButton.setonclicklistener (NewView.onclicklistener () { A @Override -              Public voidOnClick (View v) { -Intent Intent =NewIntent (); theIntent.setclassname ("Com.example.testandroid", "com.example.testandroid.BActivity"); - startactivity (intent); -             } -         }); +     } -}

This code is well understood, mainly through the SetClass (string PackageName, String className) method in intent, and it is important to note that the full name of the package needs to be written at this time. Also, for bactivity in B, the following configuration is required in the Androidmanifest.xml file:

1 < Activity 2     Android:name = "Com.example.testandroid.BActivity" 3     android:exported= "true">4</activity  >

Be sure to set the value to True for the property android:exported in activity to indicate that it can be opened by another application. Alternatively, you can configure the following:

1 <Activity2     Android:name= "Com.example.testandroid.BActivity" >3     <Intent-filter>4         <ActionAndroid:name="" />5     </Intent-filter>6 </Activity>

Set an empty action Android:name property for the activity.

At this point, we have not yet used custom permissions. Suppose you now need to do some accessibility restrictions on external applications to open bactivity directly, and add a custom permission to it so that only external apps that declare this custom permission will be eligible to open bactivity. The steps are as follows:

1. Since it is a custom permission, you must first declare this permission:

In the Androidmanifest.xml in B, the USES-SDK label is usually followed by a permission label.

1 <Permissionandroid:description= "string resource"2 Android:icon= "drawable resource"3 Android:label= "string resource"4 Android:name= "string"5 Android:permissiongroup= "string"6 Android:protectionlevel=["Normal"| "Dangerous" | "Signature" | "Signatureorsystem"]/>

The specific meanings of each attribute are as follows:

Property Meaning Whether you must
Name Custom permission names, you need to follow the Android permissions definition naming scheme: *.permission.* Is
protectionlevel

"Risk levels" related to permissions. Must be one of the following values:
normal, Dangerous, signature, Signatureorsystem, depending on the level of protection, The system may take different actions when determining whether to grant permissions.
normal means that permissions are low-risk and not harmful to the system, users, or other applications;
dangerous means that permissions are high risk, and the system may require the user to enter relevant information before granting this permission;
signature means that the permission is granted only if the digital signature used by the application is the same as the one that the application uses to declare the permission;
signatureorsystem indicates that permissions are granted to applications with the same digital signature or to Android package classes. This level of protection is appropriate for very special situations, such as when multiple vendors need to share functionality through the system image

Yes
Permissiongroup

You can put permissions in a group, but you should avoid setting this property for self-defined permissions. If you do want to set this property, you might use the following attribute instead: Android.permisson-group.system_tools

Whether
Label You can use it to briefly describe the permissions Whether
Description Use it to provide a more useful description of the purpose of the permission and the object being protected Whether
Icon Permissions can be associated with icons other than the resource directory (such as @drawable/myicon) Whether

2. When the bactivity in B is restricted by permission, the following declaration is required for bactivity:

 <  activity  2  android:name  = "com.example.testandroid.BActivity"  3   android:exported  = "true"  Span style= "color: #008080;" >4   Android:label  = "B"  5   Android:permission  =" Corn.permission.CORN_OWN " >  6  </ activity  >   

3. When the activity in external application A wants to open the bactivity in B directly, you need to add the appropriate permissions:

1 <  android:name= "Corn.permission.CORN_OWN">2</  Uses-permission>

This is the general process of customizing permissions for the activity. In general, external applications must have this permission to access the activity receiver directly when the different application activity receiver defines the permissions and restricts the external access permissions.

Similarly, in other system components of Android, such as Broadcastreceiver, ContentProvider, and service, with the same permissions, the user can customize the permissions according to the actual need, only slightly different in detail. Do not introduce too much here.

 

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.