Android 監控程序安裝和刪除的實現

通過閱讀Android SDK裏關於intent.action這部分裏面的描述,我們可以找到一些與package相關的系統廣播

  1. android.intent.action.PACKAGE_ADDED       
  2. android.intent.action.PACKAGE_CHANGED       
  3. android.intent.action.PACKAGE_DATA_CLEARED       
  4. android.intent.action.PACKAGE_INSTALL     
  5. android.intent.action.PACKAGE_REMOVED     
  6. android.intent.action.PACKAGE_REPLACED       
  7. 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 應用程序的安裝和刪除

詳細的實現代碼如下:

  1. package zy.Broadcast;   
  2. import android.content.BroadcastReceiver;   
  3. import android.content.Context;   
  4. import android.content.Intent;   
  5. import android.widget.Toast;   
  6. public class getBroadcast extends BroadcastReceiver {   
  7.         @Override   
  8.         public void onReceive(Context context, Intent intent) {   
  9.                   
  10.                   if(Intent.ACTION_PACKAGE_ADDED.equals(intent.getAction())){   
  11.                     Toast.makeText(context, "有應用被添加", Toast.LENGTH_LONG).show();   
  12.             }   
  13.                 else  if(Intent.ACTION_PACKAGE_REMOVED.equals(intent.getAction())){   
  14.                     Toast.makeText(context, "有應用被刪除", Toast.LENGTH_LONG).show();   
  15.             }   
  16.              /*   else  if(Intent.ACTION_PACKAGE_CHANGED.equals(intent.getAction())){  
  17.                     Toast.makeText(context, "有應用被改變", Toast.LENGTH_LONG).show();  
  18.             }*/   
  19.                 else  if(Intent.ACTION_PACKAGE_REPLACED.equals(intent.getAction())){   
  20.                     Toast.makeText(context, "有應用被替換", Toast.LENGTH_LONG).show();   
  21.             }   
  22.                /* else  if(Intent.ACTION_PACKAGE_RESTARTED.equals(intent.getAction())){  
  23.                     Toast.makeText(context, "有應用被重啓", Toast.LENGTH_LONG).show();  
  24.             }*/   
  25.               /*  else  if(Intent.ACTION_PACKAGE_INSTALL.equals(intent.getAction())){  
  26.                     Toast.makeText(context, "有應用被安裝", Toast.LENGTH_LONG).show();  
  27.             }*/   
  28.                
  29.         }   
  30.           
  31. }   

然後在AndroidManifest.xml中聲明這幾個Action的<intent-filter>即可在系統裏捕獲這些廣播消息

具體的源代碼如下

  1. <receiver android:name="getBroadcast" android:enabled="true" >   
  2.          <intent-filter>   
  3.              <action android:name="android.intent.action.PACKAGE_ADDED"></action>   
  4.              <!-- <action android:name="android.intent.action.PACKAGE_CHANGED"></action>-->   
  5.              <action android:name="android.intent.action.PACKAGE_REMOVED"></action>   
  6.              <action android:name="android.intent.action.PACKAGE_REPLACED"></action>   
  7.              <!-- <action android:name="android.intent.action.PACKAGE_RESTARTED"></action>-->   
  8.            <!--    <action android:name="android.intent.action.PACKAGE_INSTALL"></action>-->   
  9.                <data android:scheme="package"></data>   
  10.               </intent-filter>   
  11. </receiver>  

另: intent.getDataString()可以得到安裝的是哪個apk,如:

      package:com.android.myapp

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章