Android应用内更新下载APK和安装

无意中发现项目本身使用的DownloadManager下载内部更新有问题,无法正常下载,记录一下解决问题的过程。

使用DownloadManager

import android.app.DownloadManager;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.IntentFilter;
import android.database.ContentObserver;
import android.database.Cursor;
import android.net.Uri;
import android.os.Environment;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;

import com.fourszhan.dpt.utils.Constant;
import com.fourszhan.dpt.utils.SpUtil;
import com.fourszhan.dpt.utils.ToastUtil;
import com.fourszhan.dpt.utils.Utils;

import java.io.File;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import static android.content.Context.DOWNLOAD_SERVICE;

/**
 * @author huxh
 * @date 2018/10/11.
 */
public class UpdateDownLoadManager {


    private final Context context;
    private ProgressDialog mAlertDialog;
    private UpdataBroadcastReceiver mReceiver;

    public UpdateDownLoadManager(Context context) {
        this.context = context;
    }

    private DownloadChangeObserver downloadObserver;
    private long lastDownloadId = 0;
    /**
     * "content://downloads/my_downloads"必须这样写不可更改
     */
    public static final Uri CONTENT_URI = Uri.parse("content://downloads/my_downloads");
    private String NetUrl = "下载地址";

    private void initView(boolean isForce) {
        mAlertDialog = new ProgressDialog(context);
        mAlertDialog.setTitle("正在更新");
        mAlertDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
        mAlertDialog.setCanceledOnTouchOutside(false);
        String btn = "";
        if (isForce) {
            btn = "退出";
            mAlertDialog.setCancelable(false);
        } else {
            btn = "隐藏";
        }
        mAlertDialog.setButton(ProgressDialog.BUTTON_POSITIVE, "安装", Message.obtain());

        mAlertDialog.setButton(ProgressDialog.BUTTON_NEUTRAL, btn,Message.obtain());
        mAlertDialog.show();
        mAlertDialog.getButton(ProgressDialog.BUTTON_POSITIVE).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mAlertDialog.getProgress() == 100) {
                    Utils.install(context);
                } else {
                    ToastUtil.showToast(context, "等待下载完成");
                }
            }
        });
        mAlertDialog.getButton(ProgressDialog.BUTTON_NEUTRAL).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mAlertDialog.dismiss();
                if (isForce) {
                    DownloadManager dowanloadmanager = (DownloadManager) context.getSystemService(DOWNLOAD_SERVICE);
                    dowanloadmanager.remove(lastDownloadId);
                    Utils.finishApp();
                }
            }
        });
        File file = new File(Environment.DIRECTORY_DOWNLOADS + "/名称.apk");
        if (file.exists() && file.isFile()) {
            file.delete();
        }
    }

    public class DownLoadTask implements Runnable {

        @Override
        public void run() {
            initDownLoad();
        }
    }

    private void initDownLoad() {
        //1.得到下载对象
        DownloadManager dowanloadmanager = (DownloadManager) context.getSystemService(DOWNLOAD_SERVICE);
        //2.创建下载请求对象,并且把下载的地址放进去
        DownloadManager.Request request = new DownloadManager.Request(Uri.parse(NetUrl));
        //3.给下载的文件指定路径

        request.setDestinationInExternalFilesDir(context, Environment.DIRECTORY_DOWNLOADS, "xiuxiu.apk");
        //4.设置显示在文件下载Notification(通知栏)中显示的文字。6.0的手机Description不显示
        //5更改服务器返回的minetype为android包类型
        request.setMimeType("application/vnd.android.package-archive");
        //6.设置在什么连接状态下执行下载操作
        request.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_MOBILE | DownloadManager.Request.NETWORK_WIFI);
        //7. 设置为可被媒体扫描器找到
        request.allowScanningByMediaScanner();
        //8. 设置为可见和可管理
        request.setVisibleInDownloadsUi(true);
        lastDownloadId = dowanloadmanager.enqueue(request);
        Log.i("download", "initDownLoad: "+lastDownloadId);
        //9.保存id到缓存
        SpUtil.putLong(Constant.DOWNLOAD_ID, lastDownloadId);
        //10.采用内容观察者模式实现进度
        downloadObserver = new DownloadChangeObserver(null);
        mReceiver = new UpdataBroadcastReceiver();
        IntentFilter filter = new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE);
        filter.addAction("自定义广播内容");
        context.registerReceiver(mReceiver, filter);
        context.getContentResolver().registerContentObserver(CONTENT_URI, true, downloadObserver);
    }

    public void startDownload(boolean isForce) {
        initView(isForce);
        ExecutorService cachedThreadPool = Executors.newCachedThreadPool();
        cachedThreadPool.execute(new DownLoadTask());
    }

    //用于显示下载进度
    private class DownloadChangeObserver extends ContentObserver {

        public DownloadChangeObserver(Handler handler) {
            super(handler);
        }

        @Override
        public void onChange(boolean selfChange) {
            Log.i("download", "onChange: ");
            DownloadManager.Query query = new DownloadManager.Query();
            query.setFilterById(lastDownloadId);
            DownloadManager dManager = (DownloadManager) context.getSystemService(DOWNLOAD_SERVICE);
            final Cursor cursor = dManager.query(query);
            if (cursor != null && cursor.moveToFirst()) {
                final int totalColumn = cursor.getColumnIndex(DownloadManager.COLUMN_TOTAL_SIZE_BYTES);
                final int currentColumn = cursor.getColumnIndex(DownloadManager.COLUMN_BYTES_DOWNLOADED_SO_FAR);
                int totalSize = cursor.getInt(totalColumn);
                int currentSize = cursor.getInt(currentColumn);
                float percent = (float) currentSize / (float) totalSize;
                int progress = Math.round(percent * 100);
                mAlertDialog.setProgress(progress);
                if (progress == 100) {
                    mAlertDialog.setProgress(100);
                    context.getContentResolver().unregisterContentObserver(downloadObserver);
                    context.unregisterReceiver(mReceiver);
                }
            }
        }

    }

}

测试了一下,发现lastDownloadId = dowanloadmanager.enqueue(request);将任务加入到下载队列中了,但是就是不开始下载,进度永远是0。不抛出异常,网上搜索也没有相关信息,百度了几个demo,自己新开了一个项目发现都是能加入下载队列(enqueue返回id),但是不能开始下载,猜测是保存下载文件的路径错误或是没有读写权限?但是不打印日志也摸不到头脑。被迫放弃使用DownloadManager了(欢迎大佬指出这个问题的原因或解决方式),自己访问链接下载。

使用Retrofit2+Rxjava下载并安装
参考http://www.mamicode.com/info-detail-2358540.htmlhttps://www.cnblogs.com/newjeremy/p/7294519.html
参考的这两篇代码都比较简洁,除了retrofit2 rxjava2外没有使用其他的第三方依赖了,比较好理解。试验后也确实没有出现问题,所以选择了他们。

所需权限

    <uses-permission android:name="android.permission.REQUEST_INSTALL_PACKAGES"/>
    <uses-permission android:name="android.permission.INTERNET" />

下载方法

    /**
     * 下载单文件,该方法不支持断点下载
     *
     * @param url                  文件地址
     * @param destDir              存储文件夹
     * @param fileName             存储文件名
     * @param fileDownLoadObserver 监听回调
     */
    public void downloadFile(@NonNull String url, final String destDir, final String fileName, final FileDownLoadObserver<File> fileDownLoadObserver) {
        Retrofit retrofit = new Retrofit.Builder()
                .client(new OkHttpClient())
                .baseUrl(BASE_URL)//BASE_URL自己定义
                .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        retrofit
                .create(Request.class)
                .downLoadFile(url)
                .subscribeOn(Schedulers.io())//subscribeOn和ObserOn必须在io线程,如果在主线程会出错
                .observeOn(Schedulers.io())
                .observeOn(Schedulers.computation())//需要
                .map(new Function<ResponseBody, File>() {
                    @Override
                    public File apply(@NonNull ResponseBody responseBody) throws Exception {
                        return fileDownLoadObserver.saveFile(responseBody, destDir, fileName);
                    }
                })
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(fileDownLoadObserver);
    }

Request.class

public interface Request {
    @Streaming
    @GET
    Observable<ResponseBody> downLoadFile(@NonNull @Url String url);
}

FileDownLoadObserver.class


public abstract class FileDownLoadObserver<T> extends DefaultObserver<T> {

    @Override
    public void onNext(T t) {
        onDownLoadSuccess(t);
    }
    @Override
    public void onError(Throwable e) {
        onDownLoadFail(e);
    }
    //可以重写,具体可由子类实现
    @Override
    public void onComplete() {
    }
    //下载成功的回调
    public abstract void onDownLoadSuccess(T t);
    //下载失败回调
    public abstract void onDownLoadFail(Throwable throwable);
    //下载进度监听
    public abstract void onProgress(int progress,long total);

    /**
     * 将文件写入本地
     * @param responseBody 请求结果全体
     * @param destFileDir 目标文件夹
     * @param destFileName 目标文件名
     * @return 写入完成的文件
     * @throws java.io.IOException IO异常
     */
    public File saveFile(ResponseBody responseBody, String destFileDir, String destFileName) throws IOException {
        InputStream is = null;
        byte[] buf = new byte[2048];
        int len = 0;
        FileOutputStream fos = null;
        try {
            is = responseBody.byteStream();
            final long total = responseBody.contentLength();
            long sum = 0;

            File dir = new File(destFileDir);
            if (!dir.exists()) {
                dir.mkdirs();
            }
            File file = new File(dir, destFileName);
            fos = new FileOutputStream(file);
            while ((len = is.read(buf)) != -1) {
                sum += len;
                fos.write(buf, 0, len);
                final long finalSum = sum;
                //这里就是对进度的监听回调
                onProgress((int) (finalSum * 100 / total),total);
            }
            fos.flush();

            return file;

        } finally {
            try {
                if (is != null) is.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                if (fos != null) fos.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

安装方法

    private void install(String filePath) {
        android.util.Log.i(TAG, "开始执行安装: " + filePath);
        File apkFile = new File(filePath);
        Intent intent = new Intent(Intent.ACTION_VIEW);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            android.util.Log.w(TAG, "版本大于 N ,开始使用 fileProvider 进行安装");
            intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
            Uri contentUri = FileProvider.getUriForFile(
                    mContext
                    , "自己定义的provider的authorities"
                    , apkFile);
            intent.setDataAndType(contentUri, "application/vnd.android.package-archive");
        } else {
            android.util.Log.w(TAG, "正常进行安装");
            intent.setDataAndType(Uri.fromFile(apkFile), "application/vnd.android.package-archive");
        }
        mContext.startActivity(intent);
    }

声明Provider(不声明或声明错误的话,会崩溃提示没有权限)

        <provider
            android:name="androidx.core.content.FileProvider"
            android:authorities="自定义,在安装方法中的contentUri需要传入相同的值"
            android:exported="false"
            android:grantUriPermissions="true">
            <meta-data
                android:name="android.support.FILE_PROVIDER_PATHS"
                android:resource="@xml/apk_path" />
        </provider>

使用时

        downloadFile(FILE_NAME,mContext.getFilesDir().getPath(),FILE_NAME,new FileDownLoadObserver<File>() {
            @Override
            public void onDownLoadSuccess(File file) {
                android.util.Log.i("sss", "onDownLoadSuccess: 下载成功");
                install(file.getPath());
            }
            @Override
            public void onDownLoadFail(Throwable throwable) {
                android.util.Log.e("sss", "onDownLoadFail: 下载失败",throwable );
                Toast.makeText(mContext,"下载失败",Toast.LENGTH_LONG).show();
            }

            @Override
            public void onProgress(int progress,long total) {

            }
        });

实测可以在sdk版本24到30之间的所有版本运行,只要在配置清单中声明所需权限,并不需要动态申请权限。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值