(九)Android权限系统
2024-08-25 10:23:19
一、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"));
最新文章
- SQL 数据优化索引建suo避免全表扫描
- Java集合之ArrayList
- Piwik 扩展获取客户端IP地址
- HTTP下载文件名称编码说明
- xcodebuild
- ASP.NET CheckBoxList Operations with jQuery
- IE6 IE7 hasLayout bug之li间的3px垂直间距
- 列";xx";不在表Table中
- 拥抱.NET Core系列:Logging (1)
- html4与html5的区别及html5的一些新特性
- 第九章:Python の 网络编程基础(一)
- LOJ #2731. 「JOISC 2016 Day 1」棋盘游戏(dp)
- Javascript多线程
- Week 2 代码审查
- Git tag 标签操作
- Django之REST framework源码分析
- Blender 插件整理
- 递归和非递归分别实现求n的阶乘
- 1test
- Keil uVision4 for ARM 下增加支持C51,C5x