Course Background:
In Android has a very good control mechanism, developers need to know how to use, ordinary users need to know how to distinguish, combined to effectively prevent the spread of the Android platform virus.
Core content:
1. Define Permissions
2. Do permission checking in the code
3. Add a permission check for the base component
Request permission Instance
Androidmanifest.xml:
<!--Android Installer can prompt the user according to this permission, the permission information required by the program--><uses-permission android:name= "Android.permission.INTERNET"/ >
Activity_main.xml:
<WebView android:layout_width= "Match_parent" android:layout_height= "Match_parent" Android:id= "@+id/wv" ></WebView>
Mainactivity.java:
Private= (WebView) Findviewbyid (R.ID.WV); Wv.loadurl ("http://www.lanyunwork.com/"); // access to Web pages requires Internet access first
To add a permission check for code
Androidmanifest.xml:
<permission android:name= "Com.lanyunwork.l312_checkpermissionincode.permission.SAY_HELLO"/>< Uses-permission android:name= "Com.lanyunwork.l312_checkpermissionincode.permission.SAY_HELLO"/>
Hello.java:
Public classHello { Public Static FinalString Permission_say_hello = "Com.lanyunwork.l312_checkpermissionincode.permission.SAY_HELLO"; Public Static voidSayHello (Context context) {intCheckresult =context.checkcallingorselfpermission (Permission_say_hello); if(Checkresult! =packagemanager.permission_granted) { Throw NewSecurityException ("Execute SayHello method requires Com.lanyunwork.l312_checkpermissionincode.permission.SAY_HELLO permission"); } System.out.println ("Hello Jikexueyuan"); }}
Then call Hello.sayhello (this) in the mainactivity;
If you do not add permissions in Androidmanifest.xml uses-..., you will get an error
To add a permission check for a base component
The same principle is true for configuring permissions in activity.
In the same application, this permission does not work.
In other applications, to start this program, you have to add <uses-..., then the Android installer will know that the app will require XX permissions.
Android Note 3.12 Android Access system