监听Android应用程序安装卸载的代码

清华大佬耗费三个月吐血整理的几百G的资源,免费分享!....>>>

  1. 需要通过receiver来监听;
  2. 在AndroidManifest.xml文件中注册的receiver中必须加上<data android:scheme="package"/>才可以;
  3. intent.getData().getSchemeSpecificPart()得到的是包名。

public class RegisterReceiver extends BroadcastReceiver {

    private final static String TAG = "RegisterReceiver";

    @Override
    public void onReceive(Context context, Intent intent) {
        String action = intent.getAction();

        if (Intent.ACTION_PACKAGE_REMOVED.equals(action)) {
            Log.d(TAG , "package has been removed");
            String packageName = intent.getData().getSchemeSpecificPart();
            Log.d(TAG, packageName);
        } else if(Intent.ACTION_PACKAGE_ADDED.equals(action)) {
            Log.d(TAG , "package has been added");
        } 
    }
}

AndroidManifest.xml中增加的receiver

<receiver android:name=".RegisterReceiver">
            <intent-filter>
                <action android:name="android.intent.action.PACKAGE_REMOVED" />               
                <action android:name="android.intent.action.PACKAGE_ADDED" />              
                <data android:scheme="package"/>
            </intent-filter>
        </receiver>