android 版本更新服务

本文介绍了一种利用Android系统内置的DownloadManager服务实现应用程序静默更新的方法。通过接收下载完成的广播并自动安装更新包,提升了用户体验。文章详细讲解了如何设置下载请求、处理不同版本的权限需求,并提供了完整的代码示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

从获取到下载链接后,开启服务下载,下载完成后安装。其他业务逻辑均为处理。后期会添加静默下载完成后提示安装,断线续传,本地是否有下载过的版本等功能。

启动服务时,需传入url(下载链接)、filePath(存放路径)、authority(7.0 及清单文件中的 <provider>)。
 

采用DownloadManager 进行下载。通过接受DownloadManager发出的下载完成的广播,进行安装。

package flexible.xd.android_base.utils;

import android.app.DownloadManager;
import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.Uri;
import android.os.Build;
import android.os.Environment;
import android.os.IBinder;
import android.support.v4.content.FileProvider;
import android.text.TextUtils;

import java.io.File;

import flexible.xd.android_base.R;


/**
 * Created by flexible on 2018/12/11
 */
public class DownService extends Service {
    private DownloadManager downManager;
    private String filePath;
    private String authority;


    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public void onCreate() {
        super.onCreate();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        try {
            String url = intent.getStringExtra("url");
            filePath = intent.getStringExtra("filePath");
            authority = intent.getStringExtra("authority");
            if (TextUtils.isEmpty(url) || !url.startsWith("http")) {
                throw new RuntimeException("请传入正确的下载链接!");
            }
            if (TextUtils.isEmpty(authority) || !authority.endsWith("apk")) {
                throw new RuntimeException("请传入正确的存放链接!");
            }
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N && TextUtils.isEmpty("authority")) {
                throw new RuntimeException("7.0 及以上安装需要传入清单文件中的 <provider>的 authorities 属性");
            }
            handleVersion(url);
        } catch (Exception e) {
        }
        broadcaster();
        return super.onStartCommand(intent, flags, startId);
    }

    private void broadcaster() {
        IntentFilter filter = new IntentFilter();
        filter.addAction(DownloadManager.ACTION_DOWNLOAD_COMPLETE);
        registerReceiver(new DownLoadCompleteReceiver(), filter);
    }

    private void handleVersion(String v) {
        DownloadManager.Request request = new DownloadManager.Request(Uri.parse(v));
        //设置在什么网络情况下进行下载
//            request.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_WIFI);
        //设置通知栏标题
        request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
        request.setTitle(getResources().getString(R.string.app_name));
        request.setAllowedOverRoaming(false);
        //设置文件存放目录
        request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, filePath);
        downManager = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);
        downManager.enqueue(request);

    }

    class DownLoadCompleteReceiver extends BroadcastReceiver {
        public void onReceive(Context context, Intent intent) {
            if (intent.getAction().equals(DownloadManager.ACTION_DOWNLOAD_COMPLETE)) {
                installApp(getFileByPath(filePath), authority);
                stopSelf();
                unregisterReceiver(this);
            }
        }
    }

    public File getFileByPath(final String filePath) {
        if (filePath == null) return null;
        for (int i = 0, len = filePath.length(); i < len; ++i) {
            if (!Character.isWhitespace(filePath.charAt(i))) {
                return new File(filePath);
            }
        }
        return null;
    }

    public void installApp(final File file, final String authority) {
        if (file == null || !file.exists()) return;
        Utils.getApp().startActivity(getInstallAppIntent(file, authority, true));
    }

    public Intent getInstallAppIntent(final File file,
                                      final String authority,
                                      final boolean isNewTask) {
        if (file == null) return null;
        Intent intent = new Intent(Intent.ACTION_VIEW);
        Uri data;
        String type = "application/vnd.android.package-archive";
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.N) {
            data = Uri.fromFile(file);
        } else {
            intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
            data = FileProvider.getUriForFile(Utils.getApp(), authority, file);
        }
        intent.setDataAndType(data, type);
        return isNewTask ? intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK) : intent;
    }

}



 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值