Android安全機制--四大組件安全
組件有Public和Private的概念,是否能被其他方調用。通過android:exported欄位來確定,android:exported=true表示能,反之不行。
預設情況下,組件在AndroidMainfest聲明中沒有 interfliter 那麼exported為false,有了interfliter為true.當然我們可以直接輸入android:exported來自己控制。以下就設定為false
[html]
- android:name=.LoginActivity
- android:label=@string/app_name
- android:screenOrientation=portrait
- android:exported=false>
我們可以通過自訂permission來限制四大組件的安全
Activity中
在service
在contentprovider中分為寫與讀的兩個許可權
在broastreceiver中發送時
接收時
怎麼自訂?在androidmainfest中
[html]
- android:label=test
- android:name=com.test.custempermission
- android:protectionLevel=normal>
下面通過指定一個BroadcastReceiver的許可權來實驗
首先建立了兩個app,app A ,app B ;
app A中註冊了一個BroadcastReceiver ,app B 發送訊息
app A的menifest檔案:
Xml代碼
- package=com.example.testbutton
- android:versionCode=1
- android:versionName=1.0 >
-
- android:minSdkVersion=7
- android:targetSdkVersion=15 />
-
-
- android:icon=@drawable/ic_launcher
- android:label=@string/app_name
- android:theme=@style/AppTheme >
- android:name=.MainActivity
- launcheMode=singleTask
- android:configChanges=locale|orientation|keyboardHidden
- android:screenOrientation=portrait
- android:theme=@style/android:style/Theme.NoTitleBar.Fullscreen >
-
-
-
-
-
-
- android:name=com.example.testbutton.TestButtonReceiver
- android:permission=com.example.testbutton.RECEIVE >
-
-
-
-
-
-
- app B 的menifest 檔案內容
Xml代碼
- package=com.example.testsender
- android:versionCode=1
- android:versionName=1.0 >
-
- android:minSdkVersion=7
- android:targetSdkVersion=15 />
-
-
- android:icon=@drawable/ic_launcher
- android:label=@string/app_name
- android:theme=@style/AppTheme >
- android:name=.MainActivity
- android:label=@string/title_activity_main >
-
-
-
-
-
-
-
-
- 這樣app B 給app A 發送訊息,A就可以收到了,若未在app B的menifest檔案中聲明使用相應的許可權,app B發送的訊息,A是收不到的。