问题描述
我试图以编程方式在没有提示的情况下安装应用程序.意味着在安装应用程序时不会显示弹出窗口,用户无需在其中按安装选项.我遵循了这的答案.但是每当我运行代码时,它都会引发错误
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:权限被拒绝 在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");
有人可以请我帮忙吗?
推荐答案
您正在做的问题是,为了获得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.
添加权限后,您可以使用以下静态方法来安装软件包.您需要做的就是提供一个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应用,的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!