Android项目之APK升级管理

APK升级采用DownloadManager类,它是系统提供的下载方法,支持断点续传、通知栏显示。

downloadManager = (DownloadManager) application.getSystemService(Context.DOWNLOAD_SERVICE);

DownloadManager.Request 用来执行下载任务

DownloadManager.Request request = new DownloadManager.Request(Uri.parse(newVersionInfo.getDownloadUrl()));
request.setMimeType("application/vnd.android.package-archive");//设置文件类型  此参数为apk类型
request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, apkName);//外部存储路径,文件名

//request.setTitle(“Title”);设置下载中通知栏提示的标题
//request.setDescription(“Description”);设置下载中通知栏提示的介绍
//request.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_WIFI);表示下载允许的网络类型
//request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, apkName);//设置下载路径为系统的下载目录
//request.setAllowedOverRoaming(true);允许漫游 默认true
//request.addRequestHeader(String header, String value);请求头
//request.setDestinationInExternalFilesDir(this,path, name); 上下文,路径,文件名

downloadId = downloadManager.enqueue(request);
downloadManager.remove(REFERENCE_1, REFERENCE_2, REFERENCE_3);//移除下载

DownloadManager.Query 用来查询下载任务的信息

DownloadManager.Query  query = new Query(); 
    query.setFilterById(reference); 

    Cursor myDownload = downloadManager.query(query); 
    if (myDownload.moveToFirst()) { 
      int fileNameIdx =  
        myDownload.getColumnIndex(DownloadManager.COLUMN_LOCAL_FILENAME); //文件名
      int fileUriIdx =  
        myDownload.getColumnIndex(DownloadManager.COLUMN_LOCAL_URI); //文件uri
      int fileSizeIdx =  
        myDownload.getColumnIndex(DownloadManager.COLUMN_TOTAL_SIZE_BYTES);//文件大小 
      int bytesDLIdx =  
        myDownload.getColumnIndex(DownloadManager.COLUMN_BYTES_DOWNLOADED_SO_FAR);//文件下载大小
    } 
    myDownload.close(); 

下载成功后的广播 DownloadManager.ACTION_DOWNLOAD_COMPLETE

class CompleteReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        // get complete download id
        long completeDownloadId = intent.getLongExtra(DownloadManager.EXTRA_DOWNLOAD_ID, -1);
        // to do here
    }
};

点击正在下载的notification将会发出广播:DownloadManager.ACTION_NOTIFICATION_CLICKED

文件下载成功后启动安装:

private void installApk(long downloadApkId) {
        Intent install = new Intent(Intent.ACTION_VIEW);
        Uri downloadFileUri = downloadManager.getUriForDownloadedFile(downloadApkId);
        if (downloadFileUri != null) {
            install.setDataAndType(downloadFileUri, "application/vnd.android.package-archive");
            install.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            getApplication().startActivity(install);
        } else {
            Log.e("DownloadManager", "download error");
        }
    }
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Android 11 中,可以使用 PackageInstaller API 来实现应用程序的静默升级。以下是实现静默升级的步骤: 1. 获取应用程序的 APK 文件。 2. 创建 PackageInstaller.SessionParams 对象。 3. 调用 PackageInstaller.createSession() 方法创建一个会话。 4. 通过会话 ID 打开输出流,并将 APK 文件写入输出流中。 5. 启动会话,等待应用程序安装完成。 以下是一个简单的示例代码: ```java private void installPackageSilently(String apkPath) { // 获取应用程序的 APK 文件 File apkFile = new File(apkPath); // 创建 PackageInstaller.SessionParams 对象 PackageInstaller.SessionParams params = new PackageInstaller.SessionParams( PackageInstaller.SessionParams.MODE_FULL_INSTALL); // 调用 PackageInstaller.createSession() 方法创建一个会话 PackageInstaller packageInstaller = context.getPackageManager().getPackageInstaller(); int sessionId = packageInstaller.createSession(params); try { // 通过会话 ID 打开输出流,并将 APK 文件写入输出流中 PackageInstaller.Session session = packageInstaller.openSession(sessionId); OutputStream out = session.openWrite("app", 0, -1); FileInputStream in = new FileInputStream(apkFile); 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)); } catch (IOException e) { e.printStackTrace(); } } private IntentSender createIntentSender(Context context, int sessionId) { Intent intent = new Intent(context, getClass()); intent.putExtra(EXTRA_SESSION_ID, sessionId); PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0); return pendingIntent.getIntentSender(); } ``` 需要注意的是,静默升级需要在系统签名的应用程序中运行,并且需要 android.permission.INSTALL_PACKAGES 权限。另外,如果应用程序已经在运行,则静默升级可能会失败。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值