Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> Android 監聽程序的安裝和卸載

Android 監聽程序的安裝和卸載

編輯:關於Android編程

有些時候,需要監聽到程序是否安裝或者卸載成功,在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數據庫,也可以使用首選項等。

 


 

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved