Search code examples
androidroot

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


I came to know from other related questions and their answers that on a rooted android device we can install an apk without user input, in a similar fashion as google play store might be doing.

I have rooted my device and installed my app in the /system/app folder. It appears as a system app now. Here is the code I use to install the downloaded apk file

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

The above implementation will show install prompt for user. I want the app to install without user prompt. How can I achieve that?


Solution

  • Add this permission to Manifest:

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

    Then do:

     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
        }