Android 監聽程式的安裝和卸載

來源:互聯網
上載者:User

有些時候,需要監聽到程式是否安裝或者卸載成功,在android系統中,安裝和卸載都會發送廣播,當應用安裝完成後系統會發android.intent.action.PACKAGE_ADDED廣播。可以通過intent.getDataString()獲得所安裝的包名。當卸載程式時系統發android.intent.action.PACKAGE_REMOVED廣播。同樣intent.getDataString()獲得所卸載的包名。

    所以,只需要自訂一個BroadcastReceiver,來對系統廣播進行監聽和處理。

    1、自訂廣播
          自訂廣播MyInstalledReceiver繼承自BroadcastReceiver,實現其onReceive()方式,具體代碼如下:


[html]
import android.content.BroadcastReceiver; 
import android.content.Context; 
import android.content.Intent; 
 
public class BootReceiver extends BroadcastReceiver{ 
       
    @Override   
    public void onReceive(Context context, Intent intent){ 
        //接收安裝廣播  
        if (intent.getAction().equals("android.intent.action.PACKAGE_ADDED")) {    
            String packageName = intent.getDataString();    
            System.out.println("安裝了:" +packageName + "包名的程式");      
        }    
        //接收卸載廣播   
        if (intent.getAction().equals("android.intent.action.PACKAGE_REMOVED")) {    
            String packageName = intent.getDataString();    
            System.out.println("卸載了:"  + packageName + "包名的程式"); 
  
        } 
    } 

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;

public class BootReceiver extends BroadcastReceiver{
     
    @Override 
    public void onReceive(Context context, Intent intent){
        //接收安裝廣播
        if (intent.getAction().equals("android.intent.action.PACKAGE_ADDED")) {  
            String packageName = intent.getDataString();  
            System.out.println("安裝了:" +packageName + "包名的程式");    
        }  
        //接收卸載廣播 
        if (intent.getAction().equals("android.intent.action.PACKAGE_REMOVED")) {  
            String packageName = intent.getDataString();  
            System.out.println("卸載了:"  + packageName + "包名的程式");
 
        }
    }
}      2、註冊監聽

            1) xml 方式:在AndroidManifest.xml 設定檔的Application節點下,添加自訂的註冊監聽 MyInstalledReceiver,在AndroidManifest.xml 添加的註冊監聽,其生命週期預設是整個應用的生命週期。          


[html]
<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
    package="com.example.apk" 
    android:versionCode="1" 
    android:versionName="1.0" > 
 
    <application 
        android:icon="@drawable/ic_launcher" 
        android:label="@string/app_name" > 
        <activity 
            android:name=".MainActivity" 
            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=".MyInstalledReceiver" > 
            <intent-filter> 
                <action android:name="android.intent.action.PACKAGE_ADDED" /> 
                <action android:name="android.intent.action.PACKAGE_REMOVED" /> 
 
                <data android:scheme="package" /> 
            </intent-filter> 
        </receiver> 
    </application> 
    <uses-sdk android:minSdkVersion="3" /> 
</manifest> 

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.apk"
    android:versionCode="1"
    android:versionName="1.0" >

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <activity
            android:name=".MainActivity"
            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=".MyInstalledReceiver" >
            <intent-filter>
                <action android:name="android.intent.action.PACKAGE_ADDED" />
                <action android:name="android.intent.action.PACKAGE_REMOVED" />

                <data android:scheme="package" />
            </intent-filter>
        </receiver>
    </application>
    <uses-sdk android:minSdkVersion="3" />
</manifest>         2) 代碼方式:一般在Activity的onStart()方法中註冊監聽,在onDestroy()方法中登出監聽(也可以在onStop()方法中登出,其生命週期登出時結束)。 


[html]
@Override 
public void onStart(){ 
    super.onStart(); 
     
    installedReceiver = new MyInstalledReceiver(); 
    IntentFilter filter = new IntentFilter(); 
     
    filter.addAction("android.intent.action.PACKAGE_ADDED"); 
    filter.addAction("android.intent.action.PACKAGE_REMOVED"); 
    filter.addDataScheme("package"); 
     
    this.registerReceiver(installedReceiver, filter); 

 
@Override 
public void onDestroy(){ 
    if(installedReceiver != null) { 
        this.unregisterReceiver(installedReceiver); 
    } 
     
    super.onDestroy(); 

 @Override
 public void onStart(){
  super.onStart();
  
  installedReceiver = new MyInstalledReceiver();
  IntentFilter filter = new IntentFilter();
  
  filter.addAction("android.intent.action.PACKAGE_ADDED");
  filter.addAction("android.intent.action.PACKAGE_REMOVED");
  filter.addDataScheme("package");
  
  this.registerReceiver(installedReceiver, filter);
 }
 
 @Override
 public void onDestroy(){
  if(installedReceiver != null) {
   this.unregisterReceiver(installedReceiver);
  }
  
  super.onDestroy();
 }                3、儲存安裝、卸載資訊

                      一般時候,需要使用到程式的安裝、卸載資訊,所以需要將資訊儲存起來,比較好的方式是使用Sqlite資料庫,也可以使用喜好設定等。

 


 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.