[Android Development] Custom permissions

Source: Internet
Author: User

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:

 Public classMainactivity extends Activity {Privatebutton button; @Overrideprotected voidonCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);                Setcontentview (R.layout.activity_main); Button=(Button) Findviewbyid (R.id.button); Button.setonclicklistener (NewView.onclicklistener () {@Override Public voidOnClick (View v) {Intent Intent=NewIntent (); Intent.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 <activity2     android:name="com.example.testandroid.BActivity  "3     android:exported="true" >4 </ 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 <permission android: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:

Properties meaning Whether you must
name custom permission names, you need to follow the Android permissions definition naming scheme : *.permission.* is
protectionlevel

Rights-related risk levels. 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 indicates that permissions are risky, and the system may require the user to enter relevant information before granting this permission;
Signature Indicates that the permission is granted only if the digital signature used by the application is the same as that of the application that claims the permission, and the
Signatureorsystem means that the permission is granted to an application with the same digital signature or to an Android package class. 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-periodically defined permissions. If you do want to set this property, you might use the following attribute instead: Android.permisson-group.system_tools

no
label you can use it Short description of permissions no
description use it to provide a more useful description of the use of permissions and the objects that are protected no
icon permissions can be associated with icons other than the resource directory (such as @drawable/myicon) no

2. When bactivity in B is restricted, the bactivity must be declared as follows:

1 <activity2     android:name= "com.example.testandroid.BActivity" 3     android:exported= "true" 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 <uses-permission 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.

[Android Development] Custom 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.