安装后如何启动我的应用程序?

时间:2016-12-26 04:00:55

标签: android root su

我的代码使用pm install(root)从下载文件夹安装apk。问题是,在安装应用程序后,我需要自动启动已安装的应用程序。我该怎么做?

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 个答案:

答案 0 :(得分:0)

您可以为Action PACKAGE_INSTALLED注册广播接收器,在该接收器中您可以编写用于启动该应用程序的启动活动的逻辑

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

    }