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

來源:互聯網
上載者:User

本文主要討論如何監控 Android 程式包的安裝和刪除

 

 

Android系統的安裝方式我在 《Android 應用程式安裝方式 的詳細調研》一文中已經做了詳細的闡述,連結如下

http://www.bkjia.com/kf/201201/116677.html

 

 

基於這些安裝方式,我們如何對系統進行的安裝進行監控呢?

 

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

[c-sharp] android.intent.action.PACKAGE_ADDED     
android.intent.action.PACKAGE_CHANGED     
android.intent.action.PACKAGE_DATA_CLEARED     
android.intent.action.PACKAGE_INSTALL   
android.intent.action.PACKAGE_REMOVED   
android.intent.action.PACKAGE_REPLACED     
android.intent.action.PACKAGE_RESTARTED    
android.intent.action.PACKAGE_ADDED   
android.intent.action.PACKAGE_CHANGED   
android.intent.action.PACKAGE_DATA_CLEARED   
android.intent.action.PACKAGE_INSTALL 
android.intent.action.PACKAGE_REMOVED 
android.intent.action.PACKAGE_REPLACED   
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

[c-sharp] package zy.Broadcast; 
import android.content.BroadcastReceiver; 
import android.content.Context; 
import android.content.Intent; 
import android.widget.Toast; 
public class getBroadcast extends BroadcastReceiver { 
        @Override 
        public void onReceive(Context context, Intent intent) { 
                
                  if(Intent.ACTION_PACKAGE_ADDED.equals(intent.getAction())){ 
                    Toast.makeText(context, "有應用被添加", Toast.LENGTH_LONG).show(); 
            } 
                else  if(Intent.ACTION_PACKAGE_REMOVED.equals(intent.getAction())){ 
                    Toast.makeText(context, "有應用被刪除", Toast.LENGTH_LONG).show(); 
            } 
             /*   else  if(Intent.ACTION_PACKAGE_CHANGED.equals(intent.getAction())){
                    Toast.makeText(context, "有應用被改變", Toast.LENGTH_LONG).show();
            }*/ 
                else  if(Intent.ACTION_PACKAGE_REPLACED.equals(intent.getAction())){ 
                    Toast.makeText(context, "有應用被替換", Toast.LENGTH_LONG).show(); 
            } 
               /* else  if(Intent.ACTION_PACKAGE_RESTARTED.equals(intent.getAction())){
                    Toast.makeText(context, "有應用被重啟", Toast.LENGTH_LONG).show();
            }*/ 
              /*  else  if(Intent.ACTION_PACKAGE_INSTALL.equals(intent.getAction())){
                    Toast.makeText(context, "有應用被安裝", Toast.LENGTH_LONG).show();
            }*/ 
             
        } 
        

package zy.Broadcast;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.widget.Toast;
public class getBroadcast extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent) {
              
                  if(Intent.ACTION_PACKAGE_ADDED.equals(intent.getAction())){
                    Toast.makeText(context, "有應用被添加", Toast.LENGTH_LONG).show();
            }
                else  if(Intent.ACTION_PACKAGE_REMOVED.equals(intent.getAction())){
                    Toast.makeText(context, "有應用被刪除", Toast.LENGTH_LONG).show();
            }
             /*   else  if(Intent.ACTION_PACKAGE_CHANGED.equals(intent.getAction())){
                    Toast.makeText(context, "有應用被改變", Toast.LENGTH_LONG).show();
            }*/
                else  if(Intent.ACTION_PACKAGE_REPLACED.equals(intent.getAction())){
                    Toast.makeText(context, "有應用被替換", Toast.LENGTH_LONG).show();
            }
               /* else  if(Intent.ACTION_PACKAGE_RESTARTED.equals(intent.getAction())){
                    Toast.makeText(context, "有應用被重啟", Toast.LENGTH_LONG).show();
            }*/
              /*  else  if(Intent.ACTION_PACKAGE_INSTALL.equals(intent.getAction())){
                    Toast.makeText(context, "有應用被安裝", Toast.LENGTH_LONG).show();
            }*/
           
        }
      
}

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

具體的原始碼如下

[c-sharp] <?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
      package="zy.Broadcast" 
      android:versionCode="1" 
      android:versionName="1.0"> 
    <application android:icon="@drawable/icon" android:label="@string/app_name"> 
        <activity android:name=".Broadcast" 
                  android:label="@string/app_name"> 
            <intent-filter> 
                <action android:name="android.intent.action.MAIN" /> 
                <category android:name="android.intent.category.LAUNCHER" /> 
            </intent-filter> 
        </activity> 
      <receiver android:name="getBroadcast" android:enabled="true" > 
         <intent-filter> 
             <action android:name="android.intent.action.PACKAGE_ADDED"></action> 
             <!-- <action android:name="android.intent.action.PACKAGE_CHANGED"></action>--> 
             <action android:name="android.intent.action.PACKAGE_REMOVED"></action> 
             <action android:name="android.intent.action.PACKAGE_REPLACED"></action> 
             <!-- <action android:name="android.intent.action.PACKAGE_RESTARTED"></action>--> 
           <!--    <action android:name="android.intent.action.PACKAGE_INSTALL"></action>--> 
               <data android:scheme="package"></data> 
              </intent-filter> 
</receiver> 
    </application> 
    <uses-sdk android:minSdkVersion="7" /> 
     
</manifest>  
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="zy.Broadcast"
      android:versionCode="1"
      android:versionName="1.0">
    <application android:icon="@drawable/icon" android:label="@string/app_name">
        <activity android:name=".Broadcast"
                  android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
      <receiver android:name="getBroadcast" android:enabled="true" >
         <intent-filter>
          <action android:name="android.intent.action.PACKAGE_ADDED"></action>
          <!-- <action android:name="android.intent.action.PACKAGE_CHANGED"></action>-->
          <action android:name="android.intent.action.PACKAGE_REMOVED"></action>
          <action android:name="android.intent.action.PACKAGE_REPLACED"></action>
          <!-- <action android:name="android.intent.action.PACKAGE_RESTARTED"></action>-->
        <!--    <action android:name="android.intent.action.PACKAGE_INSTALL"></action>-->
            <data android:scheme="package"></data>
              </intent-filter>
</receiver>
    </application>
    <uses-sdk android:minSdkVersion="7" />
   
</manifest> 

 

 

把程式安裝之後 ,系統就會註冊這個BroadcastReceiver

然後有應用安裝刪除替換操作時時,就會彈出Toast提示

 

刪除應用

 

添加應用



有應用被替換




 

以上這樣,我們就可以實現監控Android 應用程式的安裝過程

 

至於攔截安裝過程,我也正在研究中,大家有好的idea可以與我 分享,謝謝

 摘自 Zy的技術心得
 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.