从 root android 安装 apk,无需用户输入(语法上)

installing apk from a rooted android without user input (grammatically)

我从其他相关问题和他们的回答中得知,在获得 root 权限的 android 设备上,我们可以在没有用户输入的情况下安装 apk,其方式与 google play store 可能正在做的类似.

我已经 root 了我的设备并将我的应用程序安装在 /system/app 文件夹中。它现在显示为系统应用程序。这是我用来安装下载的 apk 文件的代码

private void install(UpdateRequest req, Uri apk) {
    Intent i;
    Log.e("INSTALL PROCESS ", " " + apk.toString());
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
        i = new Intent(Intent.ACTION_INSTALL_PACKAGE);
        i.putExtra(Intent.EXTRA_ALLOW_REPLACE, true);
    } else {
        i = new Intent(Intent.ACTION_VIEW);
    }
    i.setDataAndType(apk, "application/vnd.android.package-archive");
    i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

    startActivity(i);
}

以上实现将为用户显示安装提示。我希望应用程序在没有用户提示的情况下安装。我怎样才能做到这一点?

将此权限添加到清单:

<uses-permission android:name="android.permission.INSTALL_PACKAGES"/>

然后做:

 try {   
        String adbCommand = "adb install -r " + fileName;
        String[] commands = new String[]{"su", "-c", adbCommand};
        Process process = Runtime.getRuntime().exec(commands);
        process.waitFor();
    } catch (Exception e) {
        //Handle Exception
    }