zoukankan      html  css  js  c++  java
  • (九)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"));
     
  • 相关阅读:
    JDBC的URL
    使用CablleStatement调用存储过程
    PreparedStatement执行sql語句
    使用Statement执行DML和DQL语句
    使用Statement对象执行静态sql语句
    JDBC接口核心的API
    MapReduce源代码分析之JobSubmitter(一)
    React Native Android入门实战及深入源代码分析系列(2)——React Native源代码编译
    Oracle学习笔记(5)——查询
    springMVC4(5)RestTemplate控制层单元測试
  • 原文地址:https://www.cnblogs.com/ql698214/p/5154927.html
Copyright © 2011-2022 走看看