Search code examples
androidinstallationbroadcastreceiverbroadcast

Listen for app installed / upgraded broadcast message in Android


Using Lookout app (https://play.google.com/store/apps/details?id=com.lookout), I see every time I install or upgrade app, it'll automatically scan this app to ensure it's not malicious.

Follow Lookout, I write a simple app which listen broadcast message whenever each app is installed or upgraded. AFAIK, there are some type of IntentFilter for broadcast message, it is:

  • Intent.ACTION_PACKAGE_ADDED
  • Intent.ACTION_PACKAGE_CHANGED
  • Intent.ACTION_PACKAGE_INSTALL

I hope Intent.ACTION_PACKAGE_ADDED is the answer but it's wrong (ACTION_PACKAGE_ADDED: A new application package has been installed on the device. The data contains the name of the package. Note that the newly installed package does not receive this broadcast.) while ACTION_PACKAGE_INSTALL is deprecated.

Can anyone tell me a better way? Any help is welcome.


Solution

  • If you are installing an application A, all other applications on the device will get the Intent that application A is the newly installed application but not A itself as it doesn't seem of any use. Now A will get broadcasts if other apps are later installed or changed.

    If you want to find out at when your app was installed or some other app's the last install or update time, you can always use PackageManager:

    PackageManager pm = context.getPackageManager();
    ApplicationInfo appInfo = pm.getApplicationInfo("app.package.name", 0);
    String appFile = appInfo.sourceDir; 
    long installed = new File(appFile).lastModified();
    

    here app.package.name is the package name of the app you want to find out the install time. If you want to use it for your app, pass in your app's package name.