app安装完成和卸载完成监听

1 监听广播

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 注册广播

  
<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>  

你可能感兴趣的:(Android)