我创建了一个设备所有者应用程序。它可以将已经安装的应用程序转换为信息亭模式,我正在尝试远程更新信息亭应用程序,而不是从 Playstore,但它没有得到更新。任何人都可以帮我一些指示吗?
问问题
1292 次
1 回答
1
您必须在您的服务器上托管您的 apk,然后根据您指定的任何条件下载它。然后,您可以使用此代码更新您的应用程序。
public static boolean InstallAPK(Context context, String apk_file_name) {
try {
File apkfile = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator + apk_file_name);
if (apkfile.exists()) {
FileInputStream in = new FileInputStream(apkfile);
PackageInstaller packageInstaller = context.getPackageManager().getPackageInstaller();
PackageInstaller.SessionParams params = new PackageInstaller.SessionParams(
PackageInstaller.SessionParams.MODE_FULL_INSTALL);
params.setAppPackageName(context.getPackageName());
// set params
int sessionId = packageInstaller.createSession(params);
PackageInstaller.Session session = packageInstaller.openSession(sessionId);
OutputStream out = session.openWrite("COSU", 0, -1);
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();
session.commit(createIntentSender(context, sessionId));
return true;
} else
return false;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
private static IntentSender createIntentSender(Context context, int sessionId) {
PendingIntent pendingIntent = PendingIntent.getBroadcast(
context,
sessionId,
new Intent(ACTION_INSTALL_COMPLETE),
0);
return pendingIntent.getIntentSender();
}
于 2018-01-26T13:24:48.463 回答