组件有Public和Private的概念,是否能被其他方调用。通过android:exported字段来确定,android:exported="true"表示能,反之不行。
默认情况下,组件在AndroidMainfest声明中没有 interfliter 那么exported为false,有了interfliter为true.当然我们可以直接输入android:exported来自己控制。以下就设置为false
-
<activity
-
android:name=".LoginActivity"
-
android:label="@string/app_name"
-
android:screenOrientation="portrait"
-
android:exported="false">
我们可以通过自定义permission来限制四大组件的安全
Activity中

在service

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

在broastreceiver中发送时

接收时

怎么自定义?在androidmainfest中
-
<permission android:description="test"
-
android:label="test"
-
android:name="com.test.custempermission"
-
android:protectionLevel="normal">
-
</permission>
下面通过指定一个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>
-
-
<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是收不到的。
Android安全机制--四大组件安全
原文:http://blog.csdn.net/chaoyue0071/article/details/45058955