- 什么是使程序下载然后安装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那么使用安装程序包的错误在哪里
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);
我开始通过本地webiste链接部署应用程序,而不是使用Google Play控制台来分发它,所以我想创建自己的安装程序概念
1条答案
按热度按时间hof1towb1#
您可以使用一行代码完成此操作(借助AndroidUtilCode)
其中
apkFile
是您要安装/更新的apk的File
对象。这将向用户提示安装对话框。请记住在清单中包含安装包的权限
这是一个
protection level: normal
权限,意味着它不需要用户通过运行时权限授予,但用户必须在被认为安装包的应用的设置中启用该选项。我还没有在Android 12和13中尝试过这个,但它应该可以工作,并为您节省大量的行