一、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"));
 

最新文章

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

热门文章

  1. 基于 koajs 的前后端分离实践
  2. 第002篇 深入体验C#项目开发(一)
  3. HTML中的figure与figcaption标签
  4. Winform单例模式与传值
  5. BestCoder 2nd Anniversary 1002 Arrange
  6. jQuery 获取 多个 复选框 和 javascript 对比
  7. YouTube视频插入Markdown
  8. js禁止中文输入 最简洁的【禁止输入中文】
  9. Python之路第八天,进阶-设计模式
  10. 1 起步-Pro Git---VCS比较、git基本原理、git配置