Understanding Android's custom permissions, here's a piece of code. Including two Android projects, Project 2 attempted to invoke the privileged activity privactivity in Project 1. Android Project 1:
Application Name:custom Permission
Pacakage Name:com.cust.perm
1) Privileged Activities Privactivity.java
Package Com.cust.perm;import android.app.activity;import android.os.bundle;/** * Created by LSC on 14-11-4. */public class Privactivity extends Activity {@Override protected void onCreate (Bundle savedinstancestate) { Super.oncreate (savedinstancestate); Setcontentview (R.LAYOUT.ACTIVITY_PRIV); }}
2) Layout file Activity_priv.xml
<?xml version= "1.0" encoding= "Utf-8"? ><linearlayout xmlns:android= "http://schemas.android.com/apk/res/ Android "android:orientation=" vertical "android:layout_width=" match_parent "android:layout_height=" Match_parent "& Gt <textview android:layout_height= "wrap_content" android:layout_width= "fill_parent" android:text= "He Llo from Privactivity "android:textsize=" 20DP "/></linearlayout>
3) configuration file Androidmanifest.xml, where the <permission .../> part is a declaration of the custom permissions, With the permission name Syh.permission.STARTMYACTIVITY, which is called later in Project2 to invoke the privileged activity privactivity, you need to get the custom permission in the configuration file with that permission name.
<?xml version= "1.0" encoding= "Utf-8"? ><manifest xmlns:android= "http// Schemas.android.com/apk/res/android " package=" Com.cust.perm " Android:versioncode= "1" android:versionname= "1.0" > < uses-sdk android:minsdkversion= "7" android:targetsdkversion= " /> <application " android:allowbackup= "true" android:icon= "@drawable/ic_launcher" android:label= "@ String/app_name " android:theme=" @style/apptheme " > <activity android:name= "Com.cust. Perm. Custpermmainactivity " android:label=" @ String/app_name " > < intent-filter> <action android:name= "Android.intent.action.MAIN" /> <category android:name= " Android.intent.category.LAUNCHER " /> </intent-filter> </activity> <activity android:name= ". Privactivity "> <intent-filter> &nBsp; <action android:name= "Android.intent.action.MAIN"/> </intent-filter> </activity> </application> <permission android:name= "Syh.permission.STARTMYACTIVITY" android:protectionlevel= "Normal" android:label= "Start my activity" android:description= "@string/startmyactivitydesc" ></permission></manifest>
Android Project 2 (client):
Application Name:clientofcustompermission
Package Name:com.client.cust.perm
1) Activity Clientcustpermmainactivity.java
Package com.client.cust.perm;import android.app.activity;import android.content.intent;import android.os.Bundle;import android.view.View;public class ClientCustPermMainActivity extends activity { @Override protected void OnCreate (bundle savedinstancestate) { super.oncreate ( Savedinstancestate); setcontentview (R.layout.activity_ Clientcustperm) } //When the button is called, create a new intent, and then set the class name of the activity you want to start public void doclick (View view) { intent intent = new intent (); Intent.setclassname ("Com.cust.perm", "com.cust.perm.PrivActivity");//Invoke privileged activity in Project1 startactivity (intent); }}
2) Layout file Activity_clientcustperm.xml
<linearlayout xmlns:android= "http:// Schemas.android.com/apk/res/android " xmlns:tools=" Http://schemas.android.com/tools " android:layout_width= "Match_parent" android:layout_height= " Match_parent " android:orientation=" vertical " android:layout_ Margin= "20DP" tools:context= ". Clientcustpermmainactivity$placeholderfragment "> <Button android:layout_height= "Wrap_content" Android:layout_width= "Fill_parent" android:text= "Launch Privactivity " android:id=" @+id/btn " android:onclick= "DoClick"/></LINEARLAYOUT>
3) profile configuration file Androidmanifest.xml, where <uses-permission android:name= "Syh.permission.STARTMYACTIVITY"/> Declare this activity get custom permissions, you can call the privileged activity in Project1 with this phrase
<?xml version= "1.0" encoding= "Utf-8"? ><manifest xmlns:android= "http// Schemas.android.com/apk/res/android " package=" Com.client.cust.perm " android:versioncode= "1" android:versionname= "1.0" > <uses-sdk android:minsdkversion= "7" android:targetsdkversion= " /> <application" android:allowbackup= "true" android:icon= "@drawable/ic_launcher" android:label= "@string/app_name" android:theme= "@style/apptheme" > <activity android:name= "COm.client.cust.perm.ClientCustPermMainActivity " android:label= "@string/app_name" > <intent-filter> <action android:name= "Android.intent.action.MAIN" /> <category android: Name= "Android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> <uses-permission android:name= " Syh.permission.STARTMYACTIVITY "/></manifest>
Android Custom Permissions