1

I need to write Android application (lets call it App1), that suggests to user to install another Android application (lets call it App2). If user agrees to install App2, I need App1 to do some operations right after App2 is successfully installed. What is the best way to organize this process? I was thinking about periodically check if App2 is installed, but I think that it is not the smartest way.

Mykhailo Granik
  • 368
  • 8
  • 23

1 Answers1

2

You can detect that an app is installed, using a BroadcastReceiver.

In onCreate:

BroadcastReceiver appInstalledReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
    String action = intent.getAction();
    if (Intent.ACTION_PACKAGE_ADDED.equals(action)) {
        Uri data = intent.getData();
        String packageName = data.getEncodedSchemeSpecificPart();
        // check if packageName is App2
        }
    }           
};

And then somewhere in onResume:

IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction(Intent.ACTION_PACKAGE_ADDED);
intentFilter.addAction(Intent.ACTION_PACKAGE_INSTALL);
intentFilter.addDataScheme("package");
registerReceiver(appInstalledReceiver, intentFilter);
josephus
  • 8,284
  • 1
  • 37
  • 57