码迷,mamicode.com
首页 > 移动开发 > 详细

(九)Android权限系统

时间:2016-01-24 15:24:32      阅读:163      评论:0      收藏:0      [点我收藏+]

标签:

一、WebView请求权限实例

1.WebView获取网页访问权限的xml布局文件和MainActivity中的程序如下

<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.若想获得访问权限还需要在AndroidManifest.xml文件中定义uses-permission访问Internet的权限
<uses-permission android:name="android.permission.INTERNET"/>

二、为代码添加权限检查

1.自定义一个Hello类,该类中定义了一个sayHello的静态方法,若想让之访问全局信息,需传递一个Context参数,检查权限调用Context的checkCallingOrSelfPermission方法,判断是否通过了权限调用PackageManager中的PERMISSION_GRANTED或者PERMISSION_DENIED

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);
if (permission!=

PackageManager.PERMISSION_GRANTED

){ //此外还有

PackageManager.PERMISSION_DENIED


throw new SecurityException("无法获得com.example.shiyanshi.checkpermissionincode.permission.SYA_HELLO的访问权限");
}
System.out.println("已经获得权限");
}
}
2.在AndroidManifest.xml中定义permission权限,并且使用uses-permission允许了权限的访问,否则在上面的权限检查代码中不会通过
<permission android:name="com.example.shiyanshi.checkpermissionincode.permission.SYA_HELLO"/>
<uses-permission android:name="com.example.shiyanshi.checkpermissionincode.permission.SYA_HELLO"/>
3.在MainActivity中调用该方法直接是Hello.sayHello(this)就可以。

三、为基本组件添加权限检查

1.app Module中AndroidManifest.xml文件

app中的应用程序首先定义了一个permission,然后在其它程序中要被调用的Activity说明该应用程序的权限,最后anotherapp中要调用声明了权限的Activity,其要声明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"/> <!--隐式Intent调用时使用—>

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

</manifest>

2.anotherapp中的AndroidManifest.xml

<?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>
3.anotherapp中的调用
startActivity(new Intent("com.example.shiyanshi.staranotheratythroughpermissionctrl.intent.action.Main2Activity"));
 

(九)Android权限系统

标签:

原文地址:http://www.cnblogs.com/ql698214/p/5154927.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!