android自定义权限的实现

jopen 9年前

在android开发中,有时需要利用自定义权限来保证android应用的安全,今天写一个自定义权限的小案例。包含了访问activity,service,以及发送广播可广播接受者,和自定义权限。

首先,新建两个工程,一个叫做security,另一个叫做securitytest

在seurity中定义,声明,并利用自定义的几个权限,在securitytest中就像用普通的权限一样,利用即可

security中声明权限如下:

<?xml version="1.0" encoding="utf-8"?>  <manifest xmlns:android="http://schemas.android.com/apk/res/android"      package="com.example.security"      android:versionCode="1"      android:versionName="1.0" >        <uses-sdk          android:minSdkVersion="11"          android:targetSdkVersion="18" />        <permission android:name="com.tct.permission.READ_PROVIDER"  android:protectionLevel="dangerous">      </permission>      <permission android:name="com.tct.apermission.WRITE_PROVIDER" android:protectionLevel="dangerous">      </permission>      <permission          android:name="com.tct.attach.start_activity"          android:protectionLevel="normal" >      </permission>      <permission          android:name="com.tct.attach.service.permission"          android:protectionLevel="normal" >      </permission>      <permission          android:name="com.tct.attach.broadcastreceiver"          android:protectionLevel="dangerous" >      </permission>        <uses-permission android:name="com.tct.attach.service.permission" />      <uses-permission android:name="com.tct.attach.start_activity" />      <uses-permission android:name="com.tct.attach.broadcastreceiver" />        <application          android:allowBackup="true"          android:icon="@drawable/ic_launcher"          android:label="@string/app_name"          android:theme="@style/AppTheme" >          <activity              android:name="com.example.security.MainActivity"              android:label="@string/app_name" >              <intent-filter>                  <action android:name="android.intent.action.MAIN" />                    <category android:name="android.intent.category.LAUNCHER" />              </intent-filter>          </activity>            <service              android:name="com.example.security.MyService"              android:permission="com.tct.attach.service.permission" >              <intent-filter>                  <action android:name="com.test.action.service" >                  </action>                    <category android:name="android.intent.category.DEFAULT" >                  </category>              </intent-filter>          </service>            <activity              android:name="com.example.security.MyActivity"              android:permission="com.tct.attach.start_activity" >              <intent-filter>                  <action android:name="android.intent.action.MAIN" >                  </action>                    <category android:name="android.intent.category.DEFAULT" >                  </category>              </intent-filter>          </activity>            <receiver              android:name="com.example.security.MyBroadcaseReceiver"              android:permission="com.tct.attach.broadcastreceiver" >              <intent-filter>                  <action android:name="com.tct.receiver.action" >                  </action>              </intent-filter>          </receiver>            <activity android:name="com.example.security.ShowReceiverActivity" >          </activity>          <activity android:name="com.example.security.MyBroadCast" >          </activity>            <provider              android:name="com.example.contentprovider.MyContentProvider"              android:authorities="com.liuhang.urlcontent"              android:exported="true" >  <!--             android:path="content://com.liuhang.urlcontent/person/3" -->              <path-permission                  android:pathPrefix="/person"                  android:readPermission="com.tct.apermission.WRITE_PROVIDER"                  android:writePermission="com.tct.apermission.WRITE_PROVIDER"                  ></path-permission>                            <grant-uri-permission android:pathPrefix="/person"/>          </provider>      </application>    </manifest>
securitytest中的AndroidManifest.xml文件如下:

<?xml version="1.0" encoding="utf-8"?>  <manifest xmlns:android="http://schemas.android.com/apk/res/android"      package="com.example.securitytest"      android:versionCode="1"      android:versionName="1.0" >        <uses-sdk          android:minSdkVersion="12"          android:targetSdkVersion="18" />              <uses-permission android:name="com.tct.attach.service.permission"/>      <uses-permission android:name="com.tct.attach.start_activity"/>      <uses-permission android:name="com.tct.attach.broadcastreceiver"/>      <uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>      <uses-permission android:name="com.tct.permission.READ_PROVIDER"/>      <uses-permission android:name="com.tct.apermission.WRITE_PROVIDER"/>      <application          android:allowBackup="true"          android:icon="@drawable/ic_launcher"          android:label="@string/app_name"          android:theme="@style/AppTheme" >          <activity              android:name="com.example.securitytest.MainActivity"              android:label="@string/app_name" >              <intent-filter>                  <action android:name="android.intent.action.MAIN" />                    <category android:name="android.intent.category.LAUNCHER" />              </intent-filter>          </activity>                    <receiver android:name="com.example.securitytest.MyTestReceiver">              <intent-filter>                  <action android:name="com.test.permission.sendbroadcast"></action>              </intent-filter>          </receiver>      </application>    </manifest>
只有当在securitytest中利用了这些权限才可以访问对应的activity,service,contentprovider以及接受和发送对应的广播。


源码如下:http://download.csdn.net/detail/mockingbirds/8109057


来自: http://blog.csdn.net//mockingbirds/article/details/40682679