如何在 phone 中从本地服务器 URL 安装 APK 而没有提示安装屏幕?
How to install APK from local server URL in phone without prompt to install screen?
我正在创建类似 Play 商店的应用程序,并将在此应用程序上发布我们所有的应用程序。所以我们的内部员工将在一个地方获得我们所有的应用程序,然后他们可以从这个应用程序安装或更新应用程序。我在下面提到的 URL 的帮助下创建了以下代码。代码 运行 成功但没有任何反应。我做错了什么吗?
我在安装按钮上调用下面的函数,如下所示
installPackage(getApplicationContext() , app_url);
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;
}
你有设备的root权限吗?来自您关注的 link
How to Install android app programatically without prompt,
明确提到
"Another way to install an app programatically without prompts assuming you have root access would be as follows"
先尝试获得设备的根访问权限,然后再执行您正在做的事情。
另一方面,如果您按照所遵循的方法成功,它对您没有任何帮助。您将无法对每个员工的设备进行 root,并且此方法对他们不起作用,除非您有一些带有 OEM 的 links,它们默认将您的应用程序添加为 Android Os。
还有: 对于 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.
据我所知,"skip" 提示的唯一方法是不将您的设备设置为出厂测试模式。实现方式因设备而异,通常需要恢复出厂设置。
我推荐的是看一下 ADB-solution。通过使用 ADB,只要设备处于开发人员模式(我认为这不是问题),您就可以轻松地从计算机将任何您想要的程序安装到设备中。我知道,这不是解决您的具体问题的方法,但我会看一下。
我正在创建类似 Play 商店的应用程序,并将在此应用程序上发布我们所有的应用程序。所以我们的内部员工将在一个地方获得我们所有的应用程序,然后他们可以从这个应用程序安装或更新应用程序。我在下面提到的 URL 的帮助下创建了以下代码。代码 运行 成功但没有任何反应。我做错了什么吗?
我在安装按钮上调用下面的函数,如下所示
installPackage(getApplicationContext() , app_url);
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;
}
你有设备的root权限吗?来自您关注的 link How to Install android app programatically without prompt,
明确提到
"Another way to install an app programatically without prompts assuming you have root access would be as follows"
先尝试获得设备的根访问权限,然后再执行您正在做的事情。
另一方面,如果您按照所遵循的方法成功,它对您没有任何帮助。您将无法对每个员工的设备进行 root,并且此方法对他们不起作用,除非您有一些带有 OEM 的 links,它们默认将您的应用程序添加为 Android Os。
还有: 对于 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.
据我所知,"skip" 提示的唯一方法是不将您的设备设置为出厂测试模式。实现方式因设备而异,通常需要恢复出厂设置。
我推荐的是看一下 ADB-solution。通过使用 ADB,只要设备处于开发人员模式(我认为这不是问题),您就可以轻松地从计算机将任何您想要的程序安装到设备中。我知道,这不是解决您的具体问题的方法,但我会看一下。