zoukankan      html  css  js  c++  java
  • Android 自定义权限 (<permission> <uses-permission>)

    在android系统的安全模型中,应用程序在默认的情况下不可以执行任何对其他应用程序,系统或者用户带来负面影响的操作。如果应用需要执行某些操作,就需要声明使用这个操作对应的权限。 (在manifest文件中 添加<uses-permission>标记) 

       android 系统提供了一系列这样的权限,具体可以查看android 权限,另外,android系统在新的版本中会增加一些permission,可以查看android 版本信息。 

       当然,app也可以自定义属于自己的permission 或属于开发者使用的同一个签名的permission。定义一个permission 就是在menifest文件中添加一个permission标签。 

    <permission android:description="string resource"
                android:icon="drawable resource"
                android:label="string resource"
                android:name="string"
                android:permissionGroup="string"
                android:protectionLevel=["normal" | "dangerous" | 
                                         "signature" | "signatureOrSystem"] />
    

      android:description :对权限的描述,一般是两句话,第一句话描述这个权限所针对的操作,第二句话告诉用户授予app这个权限会带来的后果 
    android:label: 对权限的一个简短描述 
    android:name :权限的唯一标识,一般都是使用 报名加权限名 
    android:permissionGroup: 权限所属权限组的名称 
    android:protectionLevel: 权限的等级, 
    normal 是最低的等级,声明次权限的app,系统会默认授予次权限,不会提示用户 
    dangerous  权限对应的操作有安全风险,系统在安装声明此类权限的app时会提示用户 
    signature  权限表明的操作只针对使用同一个证书签名的app开放 
    signatureOrSystem  与signature类似,只是增加了rom中自带的app的声明 

    android:name 属性是必须的,其他的可选,未写的系统会指定默认值 

    下面通过指定一个BroadcastReceiver的权限来实验 
    首先创建了两个app,app A ,app B ; 
    app A中注册了一个BroadcastReceiver ,app B 发送消息 
    app A的menifest文件: 

    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
        package="com.example.testbutton"
        android:versionCode="1"
        android:versionName="1.0" >
    
        <uses-sdk
            android:minSdkVersion="7"
            android:targetSdkVersion="15" />
        <!-- 声明权限 -->
        <permission android:name="com.example.testbutton.RECEIVE" />
    
        <application
            android:icon="@drawable/ic_launcher"
            android:label="@string/app_name"
            android:theme="@style/AppTheme" >
            <activity
                android:name=".MainActivity"
                launcheMode="singleTask"
                android:configChanges="locale|orientation|keyboardHidden"
                android:screenOrientation="portrait"
                android:theme="@style/android:style/Theme.NoTitleBar.Fullscreen" >
                <intent-filter>
                    <action android:name="android.intent.action.MAIN" />
    
                    <category android:name="android.intent.category.LAUNCHER" />
                </intent-filter>
            </activity>
            <!-- 注册Broadcast Receiver,并指定了给当前Receiver发送消息方需要的权限 -->
            <receiver
                android:name="com.example.testbutton.TestButtonReceiver"
                android:permission="com.example.testbutton.RECEIVE" >
                <intent-filter>
                    <action android:name="com.test.action" />
                </intent-filter>
            </receiver>
        </application>
    
    </manifest>
    

      app B 的menifest 文件内容

    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
        package="com.example.testsender"
        android:versionCode="1"
        android:versionName="1.0" >
    
        <uses-sdk
            android:minSdkVersion="7"
            android:targetSdkVersion="15" />
        <!-- 声明使用指定的权限 -->
        <uses-permission android:name="com.example.testbutton.RECEIVE" />
    
        <application
            android:icon="@drawable/ic_launcher"
            android:label="@string/app_name"
            android:theme="@style/AppTheme" >
            <activity
                android:name=".MainActivity"
                android:label="@string/title_activity_main" >
                <intent-filter>
                    <action android:name="android.intent.action.MAIN" />
    
                    <category android:name="android.intent.category.LAUNCHER" />
                </intent-filter>
            </activity>
        </application>
    
    </manifest>
    

      这样app B 给app A 发送消息,A就可以收到了,若未在app B的menifest文件中声明使用相应的权限,app B发送的消息,A是收不到的。 

    另外,也可在app B 的menifest文件中声明权限时,添加android:protectionLevel=“signature”,指定app B只能接收到使用同一证书签名的app 发送的消息。

  • 相关阅读:
    【LeetCode】Validate Binary Search Tree
    【LeetCode】Search in Rotated Sorted Array II(转)
    【LeetCode】Search in Rotated Sorted Array
    【LeetCode】Set Matrix Zeroes
    【LeetCode】Sqrt(x) (转载)
    【LeetCode】Integer to Roman
    贪心算法
    【LeetCode】Best Time to Buy and Sell Stock III
    【LeetCode】Best Time to Buy and Sell Stock II
    CentOS 6 上安装 pip、setuptools
  • 原文地址:https://www.cnblogs.com/zhengtu2015/p/5892013.html
Copyright © 2011-2022 走看看