(ix) Android access system

Source: Internet
Author: User

One, WebView request permission Instance

1.WebView the XML layout file that gets access to the Web page and the programs in Mainactivity are as follows

<webview
Android:layout_width= "Match_parent"
android:layout_height= "Wrap_content"
Android:id= "@+id/wv" ></WebView>
webview= (WebView) Findviewbyid (R.ID.WV);
Webview.loadurl (http://www.jikexueyuan.com);
2. If you want to gain access, you also need to define uses-permission access to the Internet in the Androidmanifest.xml file
<uses-permission android:name= "Android.permission.INTERNET"/>
Second, add permission check for code

1. Customize a Hello class, which defines a SayHello static method, if you want to access the global information, you need to pass a context parameter, check the permission to invoke the context of the checkcallingorselfpermission method to determine whether the permission_granted or permission_denied in the Packagemanager is invoked through the permission

public class Hello {
private static final String say_hello= "Com.example.shiyanshi.checkpermissionincode.permission.SYA_HELLO";
public static void SayHello (

Context

Context) {
int Permission=context.

Checkcallingorselfpermission

(Say_hello);

packagemanager.permission_granted

) {//and also

Packagemanager.permission_denied


throw new SecurityException ("Unable to gain access to Com.example.shiyanshi.checkpermissionincode.permission.SYA_HELLO");
}
System.out.println ("has been granted permission");
}
}
2. Define permission permissions in Androidmanifest.xml, and use Uses-permission to allow access to permissions, otherwise it will not be passed in the permission check code above
<permission android:name= "Com.example.shiyanshi.checkpermissionincode.permission.SYA_HELLO"/>
<uses-permission android:name= "Com.example.shiyanshi.checkpermissionincode.permission.SYA_HELLO"/>
3. Call this method directly in Mainactivity Hello.sayhello (this).
Iii. adding permission checks for basic components

Androidmanifest.xml files in 1.app module

An application in the app first defines a permission, and then in other programs the activity to be invoked describes the permissions of the application, and finally the activity that declares the permission is called in Anotherapp to declare uses-permission.

<?xml version= "1.0" encoding= "Utf-8"?>
<manifest xmlns:android= "Http://schemas.android.com/apk/res/android"
Package= "Com.example.shiyanshi.staranotheratythroughpermissionctrl" >

<permission android:name= "Com.example.shiyanshi.staranotheratythroughpermissionctrl.permission.Aty2"/>

    <application
Android:allowbackup= "true"
android:icon= "@mipmap/ic_launcher"
Android:label= "@string/app_name"
Android:supportsrtl= "true"
Android:theme= "@style/apptheme" >
<activity android:name= ". Mainactivity ">
<intent-filter>
<action android:name= "Android.intent.action.MAIN"/>

<category android:name= "Android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
<activity
Android:name= ". Main2activity "

android:permission= "Com.example.shiyanshi.staranotheratythroughpermissionctrl.permission.Aty2" >

            <intent-filter>

<action android:name= "Com.example.shiyanshi.staranotheratythroughpermissionctrl.intent.action.Main2Activity" />
<category android:name= "Android.intent.category.DEFAULT"/> <!--implicit intent invoke using-

            </intent-filter>
</activity>
</application>

</manifest>

Androidmanifest.xml in 2.anotherapp

<?xml version= "1.0" encoding= "Utf-8"?>
<manifest xmlns:android= "Http://schemas.android.com/apk/res/android"
Package= "Com.example.shiyanshi.anotherapp" >

<uses-permission android:name= "Com.example.shiyanshi.staranotheratythroughpermissionctrl.permission.Aty2"/ >

    <application
Android:allowbackup= "true"
android:icon= "@mipmap/ic_launcher"
Android:label= "@string/app_name"
Android:supportsrtl= "true"
Android:theme= "@style/apptheme" >
<act ivity android:name= ". Mainactivity ">
<intent-filter>
<action android:name= "Android.intent.action.MAIN"/>
<category android:name= "Android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
</application>

</manifest>
Calls in 3.anotherapp
StartActivity (New Intent ("Com.example.shiyanshi.staranotheratythroughpermissionctrl.intent.action.Main2Activity "));

(ix) Android rights system

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.