How do I launch my app after installing it?

205 views Asked by At

My code installs apk from downloads folder using pm install (root). The issue is, after the app gets installed, I need to launch the installed app automatically. How do I do that?

File sdCard = Environment.getExternalStorageDirectory();
    String fileStr = sdCard.getAbsolutePath() + "/download";// +
                                                            // "app-release.apk";

    File file = new File(fileStr, "xadb-build.apk");

    if (file.exists()) {
        try {
            String command;
            command = "pm install -r " + file;
            Process proc = Runtime.getRuntime().exec(
                    new String[] { "su", "-c", command });
            proc.waitFor();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
1

There are 1 answers

1
Hasif Seyd On

You can register a broadcast receiver for Action PACKAGE_INSTALLED, on in that receiver you could write the logic for launching that Application's Launch Activity

public class InstallReceiver extends BroadcastReceiver {

        public InstallReceiver()
        {

        }
        @Override
        public void onReceive(Context context, Intent intent) {

            Log.d("InstallReceiver", "Install detected.");
            String packageName = intent.getPackage();

            if ("your_app_packageName".equalsIgnoreCase(packageName)) {
                try {
                    Intent i = ctx.getPackageManager().getLaunchIntentForPackage(packageName);
                    i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    ctx.startActivity(i);
                } catch (NameNotFoundException e) {
                    // TODO Auto-generated catch block
                }
            }
        }

    }