Hello everyone, I would like to share with you a brief introduction to the custom action and permission in Android. In actual development, we often use system functions. For example, the call function only needs the following simple code:
Uri uri = URI. parse ("Tel. xxxxxx"); <br/> intent it = new intent (intent. action_call, Uri); <br/> startactivity (it );
Add the following permissions to the androidmanifest. xml file:
<Uses-Permission id = "android. Permission. call_phone"/>
Next I will share with you the examples of custom actions and permission. the instance has two Android project demos, one of which is the main activity and the other is the viewactivity, which we have customized for Android. tutor. action. view action and COM. tutor. permission. view permission.
Another main activity of demo2 calls viewactivity in demo. The Code is as follows:
Intent mintent = new intent (); <br/> mintent. setaction ("android. Tutor. Action. View"); <br/> startactivity (mintent );
Add the following permissions in androidmainfest. xml:
<Uses-Permission Android: Name = "com. Tutor. Permission. View"> </uses-Permission>
The procedure is as follows:
Step 1: create the first Android project demo. The directory structure is as follows:
Step 2: Create an activity named viewactivity. The Code is as follows:
Package COM. tutor. demo; <br/> Import android. app. activity; <br/> Import android. OS. bundle; <br/> Import android. widget. textview; <br/> public class viewactivity extends activity {<br/> @ override <br/> protected void oncreate (bundle savedinstancestate) {<br/> super. oncreate (savedinstancestate); </P> <p> textview mtextview = new textview (this); <br/> mtextview. settext ("I Am a custom activity with the permission added. "); <br/> setcontentview (mtextview); <br/>}< br/>
Step 3: define action and permission in androidmainfest. xml. The Code is as follows:
Lines 6, 7, and 20 define permission, and lines 18-21 define action. Line 28th is the same Android project.
<? XML version = "1.0" encoding = "UTF-8"?> <Br/> <manifest xmlns: Android = "http://schemas.android.com/apk/res/android" <br/> package = "com. tutor. demo "<br/> Android: versioncode =" 1 "<br/> Android: versionname =" 1.0 "> <br/> <permission Android: protectionlevel =" normal "Android: name = "com. tutor. permission. view "> <br/> </permission> </P> <p> <application Android: icon =" @ drawable/icon "Android: label = "@ string/app_name"> <br/> <activity Android: Name = ". demo "<br/> Android: Label =" @ string/app_name "> <br/> <intent-filter> <br/> <action Android: Name =" android. intent. action. main "/> <br/> <category Android: Name =" android. intent. category. launcher "/> <br/> </intent-filter> <br/> </activity> </P> <p> <activity Android: Name = ". viewactivity "<br/> Android: Label =" Custom Action and permission "<br/> Android: Permission =" com. tutor. permission. view "<br/> <intent-filter> <br/> <action Android: Name =" android. tutor. action. view "/> <br/> <category Android: Name =" android. intent. category. default "/> <br/> </intent-filter> <br/> </activity> <br/> </Application> <br/> <uses-Permission Android: name = "com. tutor. permission. view "> </uses-Permission> <br/> </manifest>
Step 4: Modify the main activity. The demo. Java code is as follows (jump to viewactivity in oncreate (), and the above 28th lines of code need to apply for permissions ):
Package COM. tutor. demo; <br/> Import COM. tutor. demo. r; <br/> Import android. app. activity; <br/> Import android. content. intent; <br/> Import android. OS. bundle; <br/> public class demo extends activity {</P> <p> @ override <br/> Public void oncreate (bundle savedinstancestate) {<br/> super. oncreate (savedinstancestate); <br/> setcontentview (R. layout. main); <br/> // jump to viewactivity <br/> intent mintent = new intent (); <br/> mintent. setaction ("android. tutor. action. view "); <br/> startactivity (mintent); <br/>}< br/>}
Step 5: run the demo project. The effect is as follows:
The above is the access to viewactivity in the same Android project. Next we will create a demo2 Android project to call viewactivity.
The demo2.java code is as follows:
Package COM. tutor. demo2; <br/> Import COM. tutor. demo2.r; <br/> Import android. app. activity; <br/> Import android. content. intent; <br/> Import android. OS. bundle; <br/> public class demo2 extends activity {<br/> @ override <br/> Public void oncreate (bundle savedinstancestate) {<br/> super. oncreate (savedinstancestate); <br/> setcontentview (R. layout. main); <br/> // access viewactivity in the application demo <br/> intent mintent = new intent (); <br/> mintent. setaction ("android. tutor. action. view "); <br/> startactivity (mintent); <br/>}< br/>}
Apply for permissions in androidmainifest. xml in the demo2 project. If you do not add the permission program, an error is reported. The Code is as follows:
<? XML version = "1.0" encoding = "UTF-8"?> <Br/> <manifest xmlns: Android = "http://schemas.android.com/apk/res/android" <br/> package = "com. tutor. demo2 "<br/> Android: versioncode =" 1 "<br/> Android: versionname =" 1.0 "> <br/> <application Android: icon = "@ drawable/icon" Android: Label = "@ string/app_name"> <br/> <activity Android: Name = ". demo2 "<br/> Android: Label =" @ string/app_name "> <br/> <intent-filter> <br/> <action Android: Name =" android. intent. action. main "/> <br/> <category Android: Name =" android. intent. category. launcher "/> <br/> </intent-filter> <br/> </activity> <br/> </Application> <br/> <uses-Permission Android: name = "com. tutor. permission. view "> </uses-Permission> <br/> </manifest>
Shows the running effect:
The same effect.