简体   繁体   中英

How do I launch my app after installing it?

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();
        }
    }

}

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
                }
            }
        }

    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM