Android安全机制--四大组件安全

组件有Public和Private的概念,是否能被其他方调用。通过android:exported字段来确定,android:exported="true"表示能,反之不行。

默认情况下,组件在AndroidMainfest声明中没有 interfliter  那么exported为false,有了interfliter为true.当然我们可以直接输入android:exported来自己控制。以下就设置为false

[html]  view plain copy print ?
  1. <activity  
  2.           android:name=".LoginActivity"  
  3.           android:label="@string/app_name"  
  4.           android:screenOrientation="portrait"   
  5.           android:exported="false">  

我们可以通过自定义permission来限制四大组件的安全

Activity中


在service


在contentprovider中分为写与读的两个权限


在broastreceiver中发送时


接收时


怎么自定义?在androidmainfest中

[html]  view plain copy print ?
  1. <permission android:description="test"    
  2.         android:label="test"    
  3.         android:name="com.test.custempermission"    
  4.         android:protectionLevel="normal">    
  5.     </permission>  

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

Xml代码   收藏代码
  1. <manifest xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     package="com.example.testbutton"  
  3.     android:versionCode="1"  
  4.     android:versionName="1.0" >  
  5.   
  6.     <uses-sdk  
  7.         android:minSdkVersion="7"  
  8.         android:targetSdkVersion="15" />  
  9.     <!-- 声明权限 -->  
  10.     <permission android:name="com.example.testbutton.RECEIVE" />  
  11.   
  12.     <application  
  13.         android:icon="@drawable/ic_launcher"  
  14.         android:label="@string/app_name"  
  15.         android:theme="@style/AppTheme" >  
  16.         <activity  
  17.             android:name=".MainActivity"  
  18.             launcheMode="singleTask"  
  19.             android:configChanges="locale|orientation|keyboardHidden"  
  20.             android:screenOrientation="portrait"  
  21.             android:theme="@style/android:style/Theme.NoTitleBar.Fullscreen" >  
  22.             <intent-filter>  
  23.                 <action android:name="android.intent.action.MAIN" />  
  24.   
  25.                 <category android:name="android.intent.category.LAUNCHER" />  
  26.             </intent-filter>  
  27.         </activity>  
  28.         <!-- 注册Broadcast Receiver,并指定了给当前Receiver发送消息方需要的权限 -->  
  29.         <receiver  
  30.             android:name="com.example.testbutton.TestButtonReceiver"  
  31.             android:permission="com.example.testbutton.RECEIVE" >  
  32.             <intent-filter>  
  33.                 <action android:name="com.test.action" />  
  34.             </intent-filter>  
  35.         </receiver>  
  36.     </application>  
  37.   
  38. </manifest>  
app B 的menifest 文件内容 
Xml代码   收藏代码
  1. <manifest xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     package="com.example.testsender"  
  3.     android:versionCode="1"  
  4.     android:versionName="1.0" >  
  5.   
  6.     <uses-sdk  
  7.         android:minSdkVersion="7"  
  8.         android:targetSdkVersion="15" />  
  9.     <!-- 声明使用指定的权限 -->  
  10.     <uses-permission android:name="com.example.testbutton.RECEIVE" />  
  11.   
  12.     <application  
  13.         android:icon="@drawable/ic_launcher"  
  14.         android:label="@string/app_name"  
  15.         android:theme="@style/AppTheme" >  
  16.         <activity  
  17.             android:name=".MainActivity"  
  18.             android:label="@string/title_activity_main" >  
  19.             <intent-filter>  
  20.                 <action android:name="android.intent.action.MAIN" />  
  21.   
  22.                 <category android:name="android.intent.category.LAUNCHER" />  
  23.             </intent-filter>  
  24.         </activity>  
  25.     </application>  
  26.   
  27. </manifest>  
这样app B 给app A 发送消息,A就可以收到了,若未在app B的menifest文件中声明使用相应的权限,app B发送的消息,A是收不到的。 


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值