(九)Android权限系统
一、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权限系统的更多相关文章
- Android权限系统
		
Android系统为每个应用程序提供了一个安全的运行环境,不同程序间相互隔离,应用程序的数据等私有资源,外界无法访问.这个安全的运行环境由Android的权限系统(可称为沙箱系统)来提供.本文简单记录 ...
 - Android笔记(七十三) Android权限问题整理 非常全面
		
Android权限系统非常庞大,我们在Android系统中做任何操作都需要首先获取Android系统权限,本文记录了所有的Android权限问题,整理一下分享给大家. 访问登记属性 android.p ...
 - Android实训案例(九)——答题系统的思绪,自己设计一个题库的体验,一个思路清晰的答题软件制作过程
		
Android实训案例(九)--答题系统的思绪,自己设计一个题库的体验,一个思路清晰的答题软件制作过程 项目也是偷师的,决心研究一下数据库.所以写的还是很详细的,各位看官,耐着性子看完,实现结果不重要 ...
 - Android权限管理之Permission权限机制及使用
		
前言: 最近突然喜欢上一句诗:"宠辱不惊,看庭前花开花落:去留无意,望天空云卷云舒." 哈哈~,这个和今天的主题无关,最近只要不学习总觉得生活中少了点什么,所以想着围绕着最近面试过 ...
 - android 权限大全
		
教程 博客 淘帖 论坛›eoe·Android开发资源区›Android开发实例教程 191507 12 / 2 页下一页 android 权限大全 『癲瘋霸気』 于 2013-4-3 10: ...
 - Android的系统属性SystemProperties
		
创建与修改android属性用Systemproperties.set(name, value) 获取android属性用Systemproperties.get(name) 注意:android属性 ...
 - Android总结篇系列:Android 权限
		
权限是一种安全机制.Android权限主要用于限制应用程序内部某些具有限制性特性的功能使用以及应用程序之间的组件访问.在Android开发中,基本上都会遇到联网的需求,我们知道都需要加上联网所需要的权 ...
 - Android权限列表permission说明
		
网络上不乏android权限列表,但是很少有将列表和使用方法放在一起的,所以特此总结一下 需要在AndroidManifest.xml中定义相应的权限(以获取internet访问权限为例),如下: & ...
 - 高校手机签到系统——第一部分Authority权限系统(下)
		
很抱歉,之前寝室光纤断了,所以到现在才更新这个系列的第二篇博客.点击访问高校手机签到系统——第一部分Authority权限系统(上) 这几天我反思了一下上一篇写博上的方式,一味的贴代码式的,是否应该更 ...
 
随机推荐
- 使用lock_sga和pre_page_sga参数保证SGA常驻物理内存 .
			
Lock_sga LOCK_SGA locks the entire SGA into physical memory. It is usually advisable to lock the SGA ...
 - 在Swift中使用遗留的C API
			
Swift的类型系统的设计目的在于简化我们的生活,为此它强制用户遵守严格的代码规范来达到这一点.毫无疑问这是一件大好事,它鼓励程序员们编写 更好更正确的代码.然而,当Swift与历史遗留的代码库.特别 ...
 - linux/module.h: No such file or directory 内核模块编译过程
			
1.缺少Linux kernel头文件 To install just the headers in Ubuntu: sudo apt-get install linux-headers-$(unam ...
 - 三、Mp3帧分析(数据帧)
			
一. 帧 帧头长4字节,是的,是4个字节,共32位. 帧头后面可能有两个字节的CRC 校验,这两个字节的是否存在决定于FRAMEHEADER 信息的第16bit, 为0 则帧头后面无校验,为1 则有校 ...
 - Deppon接口开发
			
一.1) 支持的传输协议 http ,暂时只支持HTTP协议进行通信. (2) 支持的数据传输格式 Json ,所有接口暂只支持json消息格式. (3) 编码格式:UTF-8 交互编码格 ...
 - mysql操作SQL语句
			
二.数据库操作SQL语句1.显示服务器上当前存在什么数据库SHOW DATABASES; 2.创建名称为rewin的数据库CREATE DATABASE rewin; 3.删除名称为rewin的数据库 ...
 - js获取昨天日期
			
刚刚js做项目,遇到需要获取昨天日期的问题,网上找了下答案,感觉网上的答案都不太严谨,自己写了个,凑合能用吧,忘大神们抛砖指教. <script type="text/javascri ...
 - Android混淆配置文件规范
			
#打开project.properties文件中的proguard.config. -optimizationpasses 5 # 指定代码的压缩级别 -dontusemixedcaseclassna ...
 - sql server数据库区分大小写设置
			
数据库表中字段alter Table TableName 区分大小写 ALTER Column ColumnName VARCHAR(50) COLLATE Chinese_PRC_CS_AS不区分大 ...
 - PL/SQL分页查询
			
create or replace procedure fenye(tabelname in varchar2,currentpage in number,pageSize in number,inW ...