gpt4 book ai didi

android - 如何在没有提示的情况下以编程方式安装 android 应用程序,

转载 作者:行者123 更新时间:2023-11-29 23:55:07 31 4
gpt4 key购买 nike

我正在尝试在没有提示的情况下以编程方式安装应用程序。意思是,安装应用程序时不会显示用户必须按 install 选项的弹出窗口。我关注了THIS回答。但是每当我运行代码时,它都会抛出错误

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)

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

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

以下是我用来安装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 文件夹中。如果您的应用程序不在该文件夹中,那么您将不会被授予权限并且您的应用程序将失败。

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

首先,您必须将此权限添加到您的 android list 中。 <uses-permission android:name="android.permission.INSTALL_PACKAGES" /> Android Studio 可能会提示这是一个系统权限,不会被授予。别担心,因为您的应用程序将被安装到/system/priv-app 文件夹,它将获得此系统权限。

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

 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 中,如果安装失败,请确保您已使用发布 key 对应用程序进行签名。有时使用调试 key 签名会阻止授予 Install_Packages 权限

关于android - 如何在没有提示的情况下以编程方式安装 android 应用程序,,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50462853/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com