Android Permissions Control Custom Permissions _android

Source: Internet
Author: User

God, this article finally said how to customize the permissions, the left looking forward to the right, in fact, this custom permission is quite easy. For the sake of narration, I will use two apps as an example to illustrate this.

Permission app:used to define a new Permission
This app, defined as a privilege, I call the permission app.
Client app:used to access the specified activity of the Permission App
This app, which accesses these custom permissions, I call the client app.

First look at how to write permission App

First step

Permission app is very simple, its task is to set a Permission, using the < permission> tag, we assume that the content is as follows:

Copy Code code as follows:

<permission android:name= "Custom.permission.STARTACTIVITY" android:description= "@string/permission_dcr" Android:protectionlevel=signatureorsystem android:label= "label" ></permission>

Second Step

And then in order an activity, the activity is very simple to show the next line of words, such as "Hello from Custiom Permission activity!" There is no detail here.

Third Step

Most important: We need to specify the access rights for this activity, which is the right we just requested, which needs to be identified in the Androidmanifest.xml file as follows:

Copy Code code as follows:

<activity
Android:name= "Com.example.custompermission.MainActivity"
Android:label= "@string/app_name" android:permission= "Custom.permission.STARTACTIVITY" >
</activity>

The activity was then called a stamp that must be accessed using the "Custom.permission.STARTACTIVITY" permission.

And then write the client App.

As for how to write the client App, so ... easy, just two steps:

First step

In the Androidmanifest.xml file, first request permission, as follows:

Copy Code code as follows:

<uses-permission android:name= "Custom.permission.STARTACTIVITY"/>

Second Step

Accessing the Permission app indicates the activity that requires this permission, as follows:

Copy Code code as follows:

Intent in = new Intent ();
In.setclassname ("Com.example.custompermission", "com.example.custompermission.MainActivity");
StartActivity (in);

Done

We can test the effect, first install the permission app, then install the client app, and the results are as follows:

After clicking

In addition, I have been in the Android permission permission mechanism to mention the protection level problem, I also test this protection level, the following results in Y means normal access, n means not accessible.

It should be noted that the activity of using a custom permission if set:

Copy Code code as follows:

<activity
Android:name= "Com.example.custompermission.MainActivity"
Android:label= "@string/app_name" android:permission= "Custom.permission.STARTACTIVITY" >
<intent-filter>
<action android:name= "Android.intent.action.MAIN"/>
<category android:name= "Android.intent.category.LAUNCHER"/>
</intent-filter>

Then you can't start the app from the launcher. Because only your launcher must use Uses-permission to request permission to obtain Custom.permission.STARTACTIVITY, in fact your launcher is not able to request custom permission.

Launcher: Application is isn't installed on your phone. of errors.

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.