Android custom Permissions

Source: Internet
Author: User

To implement this function, two applications a and B must have corresponding permissions to access an activity in.

1. First, let's look at the implementation of.

First add a textview to activity A, and write some content for convenience. Set manifest. xml below to add a custom permission

 

Code

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="giuz.blog1"
android:versionCode="1"
android:versionName="1.0">
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name="giuz.blog1.ActivityBlog1"
android:label="@string/app_name"
android:permission="giuz.permission.SEE">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>

<permission android:protectionLevel="normal" android:name="giuz.permission.SEE">
  </permission>


</manifest>

Note the underline section.

The first underline indicates that the Android: name of the activity requires package name + class name: giuz. blog1.activityblog1. Otherwise, the activity cannot be found.

<Permission Android: protectionlevel = "normal" Android: Name = "giuz. Permission. See"> </permission>This sentence,
Android: protectionlevel is required. Its value is normal or dangerous, signature, signatureorsystem.
Android: the value of name is a custom permission in the format of **. Permission. ** (giuz. X. Permission. See is also acceptable ).

2. B's implementation
B's activity is accessed through intent. In my code, the following code is included in the Click Event of a button:
  Intent intent = new intent ();
// The following is implemented using component:
// If I write intent. setclassname ("giuz. blog1", "giuz. blog1.activityblog1 ")
// But an error is reported.
Intent. setcomponent (New componentname ("giuz. blog1", "giuz. blog1.activityblog1 "));
Startactivity (intent)

The first parameter in the second row is the package name of the accessed activity of a, and the second parameter is the package name + class name.

Because A has the custom permission, and B does not have the permission to access it now, if you access it now, the permission denial error will occur.
Add the following statement to manifest. xml of B.

  <uses-permission android:name="giuz.permission.SEE"></uses-permission>

In this way, B has access permissions.
Paste two running sheets

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.