关于8.0的一个一个apk的下载更新,由于做的软件是内部用的,所以之前8.0的还是很少,一直也不知道会出现问题。但是后来领导突然提出来了,所以就更新了一下。
其实8.0和7.0相比其实就一个权限的添加。
<uses-permission android:name="android.permission.REQUEST_INSTALL_PACKAGES" />
下面完整下载代码
在res下的xml创建名字为provider_paths的文件
<?xml version="1.0" encoding="utf-8"?> <paths <external-path path="." name="files_path" /> </paths>
在application下创建provider
<provider android:name="android.support.v4.content.FileProvider" android:authorities="${applicationId}.provider" android:exported="false" android:grantUriPermissions="true"> <meta-data android:name="android.support.FILE_PROVIDER_PATHS" android:resource="@xml/provider_paths" /> </provider>
java代码
拿取当前版本和服务器返回的版本想对比,如果低于服务器就开始下载
以下是在fragment中做的下载动作。
//动态申请权限 private void quanxian(){ RxPermissions.getInstance(getActivity()) // 申请权限 .request(Manifest.permission.WRITE_EXTERNAL_STORAGE) .subscribe(new Action1<Boolean>() { @Override public void call(Boolean granted) { if(granted){ //请求成功 showNoticeDialog(); }else{ // 请求失败回收当前服务 } } }); } //更新当前app版本 private void showNoticeDialog() { // 更新 AlertDialog.Builder dialog = new AlertDialog.Builder(getActivity()); dialog.setTitle("检测到新版本,立即更新吗") .setMessage("检测到新版本,立即更新吗").setMessage("更新内容:\n")//更新内容根据拿取版本时候服务器返回的进行书写 .setPositiveButton("更新", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { dialog.dismiss(); Toast.makeText(getActivity(), "正在通知栏下载中", Toast.LENGTH_SHORT).show(); // 显示下载对话框 showDownloadDialog(); } }); dialog.setNegativeButton("下次再说", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { dialog.dismiss(); } }); dialog.setCancelable(false); dialog.show(); } /** * 显示软件下载对话框 */ private void showDownloadDialog() { String downPath = http://xiaz.apk;//下载路径 根据服务器返回的apk存放路径 //使用系统下载类 mDownloadManager = (DownloadManager) getActivity().getSystemService(DOWNLOAD_SERVICE); Uri uri = Uri.parse(downPath); DownloadManager.Request request = new DownloadManager.Request(uri); request.setAllowedOverRoaming(false); //创建目录下载 request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, "下载.apk"); // 把id保存好,在接收者里面要用 downloadId = mDownloadManager.enqueue(request); //设置允许使用的网络类型,这里是移动网络和wifi都可以 request.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_MOBILE | DownloadManager.Request.NETWORK_WIFI); //机型适配 request.setMimeType("application/vnd.android.package-archive"); //通知栏显示 request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED); request.setTitle("下载"); request.setDescription("正在下载中..."); request.setVisibleInDownloadsUi(true); getActivity().registerReceiver(mReceiver, new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE)); } private BroadcastReceiver mReceiver = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { checkStatus(); } }; /** * 检查下载状态 */ private void checkStatus() { 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: installAPK(); break; //下载失败 case DownloadManager.STATUS_FAILED: Toast.makeText(getActivity(), "下载失败", Toast.LENGTH_SHORT).show(); break; } } cursor.close(); } /** * 7.0兼容 */ private void installAPK() { File apkFile = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS), "下载.apk"); Intent intent = new Intent(Intent.ACTION_VIEW); intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { Uri apkUri = FileProvider.getUriForFile(getActivity(), getActivity().getPackageName() + ".provider", apkFile); intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); intent.setDataAndType(apkUri, "application/vnd.android.package-archive"); } else { intent.setDataAndType(Uri.fromFile(apkFile), "application/vnd.android.package-archive"); } getActivity().startActivity(intent); }