Android监听安装卸载

  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>

 

posted @ 2018-07-17 15:53  星辰之力  阅读(476)  评论(0编辑  收藏  举报