Search code examples
androidsdkapk

How to install APK from local server URL in phone without prompt to install screen?


I am creating app like play store and will publish all our applications on this application. so our internal employee will get all our application in one place and then they can install or update app from this app. I have created the following code with the help of URL mentioned below. Code running successfully but nothing happening. Am I doing something wrong ?

I call below function on install button click like below

installPackage(getApplicationContext() , app_url);

https://stackoverflow.com/a/50548442/7223676

public static boolean installPackage(final Context context, final String url)
            throws IOException {
        //Use an async task to run the install package method
        AsyncTask<Void,Void,Void> task = new AsyncTask<Void, Void, Void>() {
            @Override
            protected Void doInBackground(Void... voids) {
                try {
                    PackageInstaller packageInstaller = null;
                    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                        packageInstaller = context.getPackageManager().getPackageInstaller();
                    }
                    PackageInstaller.SessionParams params = null;
                    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                        params = new PackageInstaller.SessionParams(
                                PackageInstaller.SessionParams.MODE_FULL_INSTALL);
                    }

                    // set params
                    int sessionId = 0;
                    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                        sessionId = packageInstaller.createSession(params);
                    }
                    PackageInstaller.Session session = null;
                    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                        session = packageInstaller.openSession(sessionId);
                    }
                    OutputStream out = null;
                    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                        out = session.openWrite("COSU", 0, -1);
                    }
                    //get the input stream from the url
                    HttpURLConnection apkConn = (HttpURLConnection) new URL(url).openConnection();
                    InputStream in = apkConn.getInputStream();
                    byte[] buffer = new byte[65536];
                    int c;
                    while ((c = in.read(buffer)) != -1) {
                        out.write(buffer, 0, c);
                    }
                    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                        session.fsync(out);
                    }
                    in.close();
                    out.close();
                    //you can replace this intent with whatever intent you want to be run when the applicaiton is finished installing
                    //I assume you have an activity called InstallComplete
                    Intent intent = new Intent(context, AppActivity.class);
                    intent.putExtra("info", "somedata");  // for extra data if needed..
                    Random generator = new Random();
                    PendingIntent i = PendingIntent.getActivity(context, generator.nextInt(), intent, PendingIntent.FLAG_UPDATE_CURRENT);
                    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                        session.commit(i.getIntentSender());
                    }
                } catch (Exception ex){
                    Log.e("AppStore","Error when installing application. Error is " + ex.getMessage());
                }

                return null;
            }
        };
        task.execute(null,null);
        return true;
    }

Solution

  • Do you have root access of the device? From the link which you are following How to Install android app programatically without prompt,

    It is clearly mentioned that

    "Another way to install an app programatically without prompts assuming you have root access would be as follows"

    Try getting the root access of your device first and then do what you are doing.

    On a side not, if you succeed with the method you are following, it will not help you in any way. You will not be able to root every employ's device and this method will not work from them, unless you have some links with OEMs which by default add your application with the Android Os.

    ALSO : For Implementing Google Play Application Silent Install Feature On Android

    Silent install on Google Experience devices is only possible by Google Play. More generally, only stores that come preloaded on your hardware can accomplish this, since they need to use the operating system's signing key.

    This is for security reasons. Users need to be able to accept permissions for new installations. The OS has no way to verify whether non-official stores have properly done this.

    Third-party applications must use PackageManager to install new apps, which will display permissions on your behalf and require an explicit user approval.

    Attempting to circumvent this may fall under the "prohibited actions" clause of the Google Play DDA, which puts your developer account at risk for suspension. Don't do it.