DownloadUtil

移动开发 waitig 480℃ 百度已收录 0评论

在apk版本升级的时候以前都是起一个service然后处理,偶然发现这个DownloadManager
网上百度了一下学习下
参考学习资料

http://www.jianshu.com/p/6816977bfdeb

单位项目中的工具类,感觉写的不好,先马克在这

public class DownloadUtil {

    private DownloadManager mDownloadManager;
    private Context mContext;
    private long downloadId;

    public DownloadUtil(Context context) {
        mContext = context;
    }

    public void download(String url) {
        final String packageName = "com.android.providers.downloads";
        int state = mContext.getPackageManager().getApplicationEnabledSetting(packageName);
        //检测下载管理器是否被禁用
        if (state == PackageManager.COMPONENT_ENABLED_STATE_DISABLED
                || state == PackageManager.COMPONENT_ENABLED_STATE_DISABLED_USER
                || state == PackageManager.COMPONENT_ENABLED_STATE_DISABLED_UNTIL_USED) {
            AlertDialog.Builder builder = new AlertDialog.Builder(mContext).setTitle("温馨提示").setMessage
                    ("系统下载管理器被禁止,需手动打开").setPositiveButton("确定", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    dialog.dismiss();
                    try {
                        Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
                        intent.setData(Uri.parse("package:" + packageName));
                        mContext.startActivity(intent);
                    } catch (ActivityNotFoundException e) {
                        Intent intent = new Intent(Settings.ACTION_MANAGE_APPLICATIONS_SETTINGS);
                        mContext.startActivity(intent);
                    }
                }
            }).setNegativeButton("取消", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    dialog.dismiss();
                }
            });
            builder.create().show();
        } else {
            DownloadManager manager = (DownloadManager) mContext
                    .getSystemService(Context.DOWNLOAD_SERVICE);
            DownloadManager.Request request = new DownloadManager.Request(
                    Uri.parse(url));
            request.setTitle("丫丫学车下载中...");
            request.setDescription("");
            request.allowScanningByMediaScanner();// 设置可以被扫描到
            request.setVisibleInDownloadsUi(true);// 设置下载可见
            String fileName = url.substring(url.lastIndexOf("/"));// 解析fileName
            request.setDestinationInExternalPublicDir(
                    Environment.DIRECTORY_DOWNLOADS, fileName);// 设置下载位置,sdcard/Download/fileName
            long refernece = manager.enqueue(request);// 加入下载并取得下载ID
            SharedPreferences sPreferences = mContext
                    .getSharedPreferences("downloadplato", 0);
            sPreferences.edit().putLong("plato", refernece).commit();//保存此次下载ID
        }
    }

//    private BroadcastReceiver mReceiver = new BroadcastReceiver() {
//        @Override
//        public void onReceive(Context context, Intent intent) {
//            checkStatus();
//        }
//    };

    /**
     * 检查下载状态
     */
//    private void checkStatus() {
//        mContext.unregisterReceiver(mReceiver);
//        DownloadManager.Query query = new DownloadManager.Query();
//        query.setFilterById(downloadId);
//        Cursor cursor = mDownloadManager.query(query);
//        if (cursor.moveToFirst()) {
//            int status = cursor.getInt(cursor.getColumnIndex(DownloadManager.COLUMN_STATUS));
//            switch (status) {
//                //下载暂停
//                case DownloadManager.STATUS_PAUSED:
//                    break;
//                //下载延迟
//                case DownloadManager.STATUS_PENDING:
//                    break;
//                //正在下载
//                case DownloadManager.STATUS_RUNNING:
//                    break;
//                //下载完成
//                case DownloadManager.STATUS_SUCCESSFUL:
//                    File apkFile = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS), "yayaxueche.apk");
//                    PackageUtil.installAPK(mContext,apkFile);
//                    break;
//                //下载失败
//                case DownloadManager.STATUS_FAILED:
//                    Toast.makeText(mContext, R.string.DownLoadErr, Toast.LENGTH_SHORT).show();
//                    break;
//            }
//        }
//        cursor.close();
//    }

}

调用方式

DownloadUtil downloadUtil = new DownloadUtil(MainActivity.this);
                                downloadUtil.download(HttpUrlConstants.SERVER_RESOUSE_URL + url);

感觉还是

http://www.jianshu.com/p/6816977bfdeb

这个链接里边写的比较好一点
感觉downLoadManager不是很还用,不如起一个service


学习资料
http://www.jianshu.com/p/2c57efe49ef2

https://github.com/shuhaoSCode/AndroidUpdate

本文由【waitig】发表在等英博客
本文固定链接:DownloadUtil
欢迎关注本站官方公众号,每日都有干货分享!
等英博客官方公众号
点赞 (0)分享 (0)