本文主要討論如何監控 Android 程式包的安裝和刪除
Android系統的安裝方式我在 《Android 應用程式安裝方式 的詳細調研》一文中已經做了詳細的闡述,連結如下
http://blog.csdn.net/Zengyangtech/archive/2010/07/15/5737522.aspx
基於這些安裝方式,我們如何對系統進行的安裝進行監控呢?
通過閱讀Android SDK裡關於intent.action這部分裡面的描述,我們可以找到一些與package相關的系統廣播
android.intent.action.PACKAGE_ADDED<br />android.intent.action.PACKAGE_CHANGED<br />android.intent.action.PACKAGE_DATA_CLEARED<br />android.intent.action.PACKAGE_INSTALL<br />android.intent.action.PACKAGE_REMOVED<br />android.intent.action.PACKAGE_REPLACED<br />android.intent.action.PACKAGE_RESTARTED
其中
ACTION_PACKAGE_ADDED
在SDK裡的描述是
Broadcast Action: A new application package has been installed on the device.
ACTION_PACKAGE_REMOVED
在SDK裡的描述是
Broadcast Action: An existing application package has been removed from the device.
ACTION_PACKAGE_REPLACED
在SDK裡的描述是
Broadcast Action: A new version of an application package has been installed, replacing an existing version that was previously installed.
通過這三個廣播訊息 我們已經可以監控到Android 應用程式的安裝和刪除
詳細的實現代碼如下
getBroadcast.java
<br />package zy.Broadcast;<br />import android.content.BroadcastReceiver;<br />import android.content.Context;<br />import android.content.Intent;<br />import android.widget.Toast;<br />public class getBroadcast extends BroadcastReceiver {<br /> @Override<br /> public void onReceive(Context context, Intent intent) {</p><p> if(Intent.ACTION_PACKAGE_ADDED.equals(intent.getAction())){<br /> Toast.makeText(context, "有應用被添加", Toast.LENGTH_LONG).show();<br /> }<br /> else if(Intent.ACTION_PACKAGE_REMOVED.equals(intent.getAction())){<br /> Toast.makeText(context, "有應用被刪除", Toast.LENGTH_LONG).show();<br /> }<br /> /* else if(Intent.ACTION_PACKAGE_CHANGED.equals(intent.getAction())){<br /> Toast.makeText(context, "有應用被改變", Toast.LENGTH_LONG).show();<br /> }*/<br /> else if(Intent.ACTION_PACKAGE_REPLACED.equals(intent.getAction())){<br /> Toast.makeText(context, "有應用被替換", Toast.LENGTH_LONG).show();<br /> }<br /> /* else if(Intent.ACTION_PACKAGE_RESTARTED.equals(intent.getAction())){<br /> Toast.makeText(context, "有應用被重啟", Toast.LENGTH_LONG).show();<br /> }*/<br /> /* else if(Intent.ACTION_PACKAGE_INSTALL.equals(intent.getAction())){<br /> Toast.makeText(context, "有應用被安裝", Toast.LENGTH_LONG).show();<br /> }*/</p><p> }</p><p>}
然後在AndroidManifest.xml中聲明這幾個Action的<intent-filter>即可在系統裡捕獲這些廣播訊息
具體的原始碼如下
<?xml version="1.0" encoding="utf-8"?><br /><manifest xmlns:android="http://schemas.android.com/apk/res/android"<br /> package="zy.Broadcast"<br /> android:versionCode="1"<br /> android:versionName="1.0"><br /> <application android:icon="@drawable/icon" android:label="@string/app_name"><br /> <activity android:name=".Broadcast"<br /> android:label="@string/app_name"><br /> <intent-filter><br /> <action android:name="android.intent.action.MAIN" /><br /> <category android:name="android.intent.category.LAUNCHER" /><br /> </intent-filter><br /> </activity><br /> <receiver android:name="getBroadcast" android:enabled="true" ><br /> <intent-filter><br /> <action android:name="android.intent.action.PACKAGE_ADDED"></action><br /> <!-- <action android:name="android.intent.action.PACKAGE_CHANGED"></action>--><br /> <action android:name="android.intent.action.PACKAGE_REMOVED"></action><br /> <action android:name="android.intent.action.PACKAGE_REPLACED"></action><br /> <!-- <action android:name="android.intent.action.PACKAGE_RESTARTED"></action>--><br /> <!-- <action android:name="android.intent.action.PACKAGE_INSTALL"></action>--><br /> <data android:scheme="package"></data><br /> </intent-filter><br /></receiver><br /> </application><br /> <uses-sdk android:minSdkVersion="7" /></p><p></manifest>
把程式安裝之後 ,系統就會註冊這個BroadcastReceiver
然後有應用安裝刪除替換操作時時,就會彈出Toast提示
刪除應用
添加應用
有應用被替換
以上這樣,我們就可以實現監控Android 應用程式的安裝過程
至於攔截安裝過程,我也正在研究中,大家有好的idea可以與我
分享,謝謝