如何在没有提示的情况下以编程方式安装 android 应用程序, [英] How to Install android app programatically without prompt,

查看:27
本文介绍了如何在没有提示的情况下以编程方式安装 android 应用程序,的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在没有提示的情况下以编程方式安装应用程序.意思是,安装应用程序时不会显示用户必须按安装选项的弹出窗口.我跟着这个回答.但是每当我运行代码时,它都会抛出错误

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:运行 exec() 时出错.命令:[su, -c, adbinstall -r/storage/emulated/0/update.apk] 工作目录:null环境:空

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

引起:java.io.IOException:权限被拒绝在 java.lang.ProcessManager.exec(本机方法)在 java.lang.ProcessManager.exec(ProcessManager.java:209)

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

它说权限被拒绝,但没有说明是哪个权限.apk 在设备的存储中,我在清单中提供了以下权限.

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"/>

以下是我用来安装apk的代码

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

我称这个函数为:

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

有人可以帮助我获得我丢失的许可吗.

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

推荐答案

你正在做的问题是,为了获得 INSTALL_PACKAGES 权限,你的应用程序必须在/system/priv-app 文件夹.如果您的应用程序不在该文件夹中,那么您将不会被授予权限并且您的应用程序将失败.

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.

假设您具有 root 访问权限,另一种以编程方式安装应用程序而不提示的方法如下:

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

首先,您必须将此权限添加到您的 android 清单中.<uses-permission android:name="android.permission.INSTALL_PACKAGES"/> Android studio 可能会抱怨这是系统权限,不会被授予.不用担心,由于您的应用将安装到/system/priv-app 文件夹中,因此它只会获得此系统权限.

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.

添加权限后,您可以使用以下静态方法安装软件包.您需要做的就是提供一个 url 作为可用于访问文件的 String,以及一个 Context 和应用程序将被安装.

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

注意:如果安装程序应用程序位于 system/priv-app 中但安装失败,请确保您已使用发布密钥对应用程序进行签名.有时使用调试密钥签名会阻止授予 Install_Packages 权限

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

这篇关于如何在没有提示的情况下以编程方式安装 android 应用程序,的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆