[英]What is the concept of making the program download then install the apk
下载程序然后安装 apk 的概念是什么我已经制作了安装程序但是当我制作安装程序时它不起作用
download.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String getUrl = "https://apptrue.online/files/Truejek.apk";
DownloadManager.Request request = new DownloadManager.Request(Uri.parse(getUrl));
String title = URLUtil.guessFileName(getUrl, null, null);
request.setTitle(title);
request.setDescription("Sedang mendowload file");
String cookie = CookieManager.getInstance().getCookie(getUrl);
request.addRequestHeader("cookie", cookie);
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, title);
DownloadManager downloadManager = (DownloadManager)getSystemService(DOWNLOAD_SERVICE);
downloadManager.enqueue(request);
Toast.makeText(UpdateActivity.this, "Download dimulai", Toast.LENGTH_SHORT).show();
checkWriteExternalStoragePermission();
}
});
install.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
PackageInstaller.Session session = null;
try {
PackageInstaller packageInstaller = getPackageManager().getPackageInstaller();
PackageInstaller.SessionParams params = new PackageInstaller.SessionParams(
PackageInstaller.SessionParams.MODE_FULL_INSTALL);
int sessionId = packageInstaller.createSession(params);
session = packageInstaller.openSession(sessionId);
addApkToInstallSession(Environment.DIRECTORY_DOWNLOADS, session);
DevicePolicyManager dpm = (DevicePolicyManager) getApplicationContext().getSystemService(Context.DEVICE_POLICY_SERVICE);
dpm.clearDeviceOwnerApp(getApplicationContext().getPackageName());
checkWriteExternalStoragePermission();
Context context = UpdateActivity.this;
Intent intent = new Intent(context, UpdateActivity.class);
intent.setAction(PACKAGE_INSTALLED_ACTION);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_IMMUTABLE | PendingIntent.FLAG_UPDATE_CURRENT);
IntentSender statusReceiver = pendingIntent.getIntentSender();
// Commit the session (this will start the installation workflow).
session.commit(statusReceiver);
} catch (IOException e) {
throw new RuntimeException("Tidak dapat menginstal paket", e);
} catch (RuntimeException e) {
if (session != null) {
session.abandon();
}
throw e;
}
}
});
}
private void addApkToInstallSession(String assetName, PackageInstaller.Session session)
throws IOException {
// It's recommended to pass the file size to openWrite(). Otherwise installation may fail
// if the disk is almost full.
try (OutputStream packageInSession = session.openWrite("package", 0, -1);
InputStream is = getAssets().open(assetName)) {
byte[] buffer = new byte[1638400];
int n;
while ((n = is.read(buffer)) >= 0) {
packageInSession.write(buffer, 0, n);
}
}
}
// Note: this Activity must run in singleTop launchMode for it to be able to receive the intent
// in onNewIntent().
@Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
Bundle extras = intent.getExtras();
if (PACKAGE_INSTALLED_ACTION.equals(intent.getAction())) {
int status = extras.getInt(PackageInstaller.EXTRA_STATUS);
String message = extras.getString(PackageInstaller.EXTRA_STATUS_MESSAGE);
switch (status) {
case PackageInstaller.STATUS_PENDING_USER_ACTION:
// This test app isn't privileged, so the user has to confirm the install.
Intent confirmIntent = (Intent) extras.get(Intent.EXTRA_INTENT);
startActivity(confirmIntent);
break;
case PackageInstaller.STATUS_SUCCESS:
Toast.makeText(this, "Berhasil di install!", Toast.LENGTH_SHORT).show();
break;
case PackageInstaller.STATUS_FAILURE:
case PackageInstaller.STATUS_FAILURE_ABORTED:
case PackageInstaller.STATUS_FAILURE_BLOCKED:
case PackageInstaller.STATUS_FAILURE_CONFLICT:
case PackageInstaller.STATUS_FAILURE_INCOMPATIBLE:
case PackageInstaller.STATUS_FAILURE_INVALID:
case PackageInstaller.STATUS_FAILURE_STORAGE:
Toast.makeText(this, "Install gagal! " + status + ", " + message,
Toast.LENGTH_SHORT).show();
break;
default:
Toast.makeText(this, "Status tidak dikenal diterima dari penginstal: " + status,
Toast.LENGTH_SHORT).show();
}
}
}
private void checkWriteExternalStoragePermission () {
if (ActivityCompat.checkSelfPermission(UpdateActivity.this, Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) {
} else {
// If we don't have permission than requesting the permission
requestWriteExternalStoragePermission();
}
}
private void requestWriteExternalStoragePermission () {
if (ActivityCompat.checkSelfPermission(UpdateActivity.this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(UpdateActivity.this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, MY_PERMISSION_REQUEST_WRITE_EXTERNAL_STORAGE);
} else {
ActivityCompat.requestPermissions(UpdateActivity.this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, MY_PERMISSION_REQUEST_WRITE_EXTERNAL_STORAGE);
}
}
@Override
public void onRequestPermissionsResult ( int requestCode, @NonNull String[] permissions,
@NonNull int[] grantResults){
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if (requestCode == MY_PERMISSION_REQUEST_WRITE_EXTERNAL_STORAGE && grantResults.length == 1 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
} else {
Toast.makeText(UpdateActivity.this, "Izin Tidak Diberikan.", Toast.LENGTH_SHORT).show();
}
}
}
它不适用于 android 12 和 13那么使用安装程序 package 的错误在哪里
PackageInstaller packageInstaller = getPackageManager().getPackageInstaller();
PackageInstaller.SessionParams params = new PackageInstaller.SessionParams(
PackageInstaller.SessionParams.MODE_FULL_INSTALL);
int sessionId = packageInstaller.createSession(params);
session = packageInstaller.openSession(sessionId);
addApkToInstallSession(Environment.DIRECTORY_DOWNLOADS, session);
DevicePolicyManager dpm = (DevicePolicyManager) getApplicationContext().getSystemService(Context.DEVICE_POLICY_SERVICE);
dpm.clearDeviceOwnerApp(getApplicationContext().getPackageName());
checkWriteExternalStoragePermission();
Context context = UpdateActivity.this;
Intent intent = new Intent(context, UpdateActivity.class);
intent.setAction(PACKAGE_INSTALLED_ACTION);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_IMMUTABLE | PendingIntent.FLAG_UPDATE_CURRENT);
IntentSender statusReceiver = pendingIntent.getIntentSender();
// Commit the session (this will start the installation workflow).
session.commit(statusReceiver);
我开始通过本地网站链接部署应用程序,而不是使用 google play 控制台来分发它,所以我想创建自己的安装程序概念
你可以用一行完成它(在AndroidUtilCode的帮助下)
AppUtils.installApp(apkFile)
其中apkFile
是您要安装/更新的 apk 的File
object。 这将向用户提示安装对话框。
请记住在您的清单中包含安装包的权限
<uses-permission android:name="android.permission.REQUEST_INSTALL_PACKAGES" />
这是一个protection level: normal
权限,意味着它不需要用户通过运行时权限授予它,但用户必须在应用程序的设置中启用被认为安装包的选项。
我还没有在 Android 12 和 13 中尝试过这个,但它应该可以工作并为你节省很多行
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.