Android programming to monitor apk installation uninstall replace method

  • 2020-12-22 17:46:34
  • OfStack

This article describes the Android programming monitoring apk installation, uninstall, replace the method. To share for your reference, the details are as follows:


public class GetBroadcast extends BroadcastReceiver {
  private static GetBroadcast mReceiver = new GetBroadcast();
  private static IntentFilter mIntentFilter;
  public static void registerReceiver(Context context) {
    mIntentFilter = new IntentFilter();
    mIntentFilter.addDataScheme("package");
    mIntentFilter.addAction(Intent.ACTION_PACKAGE_ADDED);
    mIntentFilter.addAction(Intent.ACTION_PACKAGE_REMOVED);
    mIntentFilter.addAction(Intent.ACTION_PACKAGE_REPLACED);
    context.registerReceiver(mReceiver, mIntentFilter);
  }
  public static void unregisterReceiver(Context context) {
    context.unregisterReceiver(mReceiver);
  }
  @Override
  public void onReceive(Context context, Intent intent) {
    String action = intent.getAction();
    if (Intent.ACTION_PACKAGE_ADDED.equals(action)) {
      Toast.makeText(context, " There are applications added ", Toast.LENGTH_LONG).show();
    } else if (Intent.ACTION_PACKAGE_REMOVED.equals(action)) {
      Toast.makeText(context, " Some apps have been deleted ", Toast.LENGTH_LONG).show();
    }
    /*
    * else if(Intent.ACTION_PACKAGE_CHANGED.equals(action)){
    * Toast.makeText(context, " There are applications that have been changed ", Toast.LENGTH_LONG).show(); }
    */
    else if (Intent.ACTION_PACKAGE_REPLACED.equals(action)) {
      Toast.makeText(context, " There are applications that are replaced ", Toast.LENGTH_LONG).show();
    }
    /*
    * else if(Intent.ACTION_PACKAGE_RESTARTED.equals(action)){
    * Toast.makeText(context, " An application has been restarted ", Toast.LENGTH_LONG).show(); }
    */
    /*
    * else if(Intent.ACTION_PACKAGE_INSTALL.equals(action)){
    * Toast.makeText(context, " There are applications installed ", Toast.LENGTH_LONG).show(); }
    */
  }
}

I hope this article has been helpful in Android programming.


Related articles: