How to Install android app programatically without prompt,

陌路散爱 提交于 2019-12-10 11:37:46

问题


I am trying to install the application programatically without prompt. Means, installing the app without showing the pop-up where user has to press install option. I followed THIS answer. But whenever I am running the code, it is throwing the error

java.io.IOException: Error running exec(). Command: [su, -c, adb install -r /storage/emulated/0/update.apk] Working Directory: null Environment: null

Caused by: java.io.IOException: Permission denied at java.lang.ProcessManager.exec(Native Method) at java.lang.ProcessManager.exec(ProcessManager.java:209)

It says Permission denied, but doesn't tell which permission. The apk is in the storage of the device and I have provided following permissions in the manifest.

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

Following is the code that I use for installing the apk

 public void InstallAPK(String filename){
    File file = new File(filename);
    if(file.exists()){
        try {
            String command;
            command = "adb install -r " + filename;
            Process proc = Runtime.getRuntime().exec(new String[] { "su", "-c", command });
            proc.waitFor();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

And I call this function as:

InstallAPK(Environment.getExternalStorageDirectory().getAbsolutePath()+"/update.apk");

Can someone please help me with the permission that I am missing.


回答1:


The problem with what you are doing is that in order to get the INSTALL_PACKAGES permission your application has to be in the /system/priv-app folder. If your application is not in that folder then you will not be granted the permission and your application will fail.

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

First you must add this permission to your android manifest. <uses-permission android:name="android.permission.INSTALL_PACKAGES" /> Android studio may complain that this is a system permission and won't be granted. Don't worry, since your app is going to be installed to the /system/priv-app folder, it will get this system only permission.

After adding permission you can use the following static method to install packages. All you need to do is provide a url as a String that can be used to access the file, and a Context and the application will be installed.

 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 = context.getPackageManager().getPackageInstaller();
                PackageInstaller.SessionParams params = new PackageInstaller.SessionParams(
                        PackageInstaller.SessionParams.MODE_FULL_INSTALL);

                // set params
                int sessionId = packageInstaller.createSession(params);
                PackageInstaller.Session session = packageInstaller.openSession(sessionId);
                OutputStream out = session.openWrite("COSU", 0, -1);
                //get the input stream from the url
                HttpsURLConnection apkConn = (HttpsURLConnection) 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);
                }
                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, InstallComplete.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);
                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;
}

Note: If the installation fails even though the installer app is located in system/priv-app then ensure that you have signed the app with a release key. Sometimes signing with a debug key will prevent the Install_Packages permission from being granted



来源:https://stackoverflow.com/questions/50462853/how-to-install-android-app-programatically-without-prompt

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!