Android apk升级 兼容6.0 7.0 低版本 华为手机
2017-06-22 18:58
816 查看
上篇博客写了用DownloadManager,进行下载,结果发现有些手机并不能兼容,查找原因是DownloadManager.Request 的setDestinationInExternalPublicDir()函数问题,这直接根源挂钩,Request 有个默认路径,导致android 7.0的不兼容,具体原因不说,你们也应该知道了(API 禁止向您的应用外公开 file://URI,若要在应用间共享文件,您应发送一项 content://URI,并授予 URI 临时访问权限。进行此授权的最简单方式是使用 FileProvider类。 如需有关权限和共享文件的更多信息。)那,您说该怎么玩呢?那我们就用原始的HttpURLConnection 下载呗,你想呀,无非就是下载一个文件而已。
多的不说直接上代码,下面是个工具类直接用就可以:
}
那你说怎么用呢?很简单呀。
//开始下载
new UpdataAPP(mContext).updateAPP(apkDownLoadUrl);
下面是:AndroidManifest.xml的FileProvider:
xml下的filepaths:
多的不说直接上代码,下面是个工具类直接用就可以:
public class UpdataAPP { public Context context; private ProgressDialog pBar; private DownloadApkAsyncTask mAsyncTask = null; private int apkSzie; public UpdataAPP(Context context) { this.context = context; } /** * 版本更新----起 */ // 更新版本要用到的一些信息 public void updateAPP(String url) { mAsyncTask = new DownloadApkAsyncTask(); mAsyncTask.execute(url); pBar=new ProgressDialog(context); pBar.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); pBar.setTitle("正在下载"); pBar.setMessage("请稍候..."); pBar.setCanceledOnTouchOutside(false); pBar.setProgress(0); pBar.show(); } class DownloadApkAsyncTask extends AsyncTask<String, Integer, Integer> { private static final int DOWNLOAD_OK = 0x00; private static final int DOWNLOAD_ERROR = 0x01; private File apkFile = null; private File updateDir = null; public DownloadApkAsyncTask() { super(); } @Override protected Integer doInBackground(String... params) { try { URL url = new URL(params[0]); HttpURLConnection connection = (HttpURLConnection) url .openConnection(); connection.setConnectTimeout(15000); InputStream in = connection.getInputStream(); int curSize = 0; int progress = 0; int totalSize = connection.getContentLength(); apkSzie=totalSize; pBar.setMax(totalSize); // 设置进度条的总长度 if (android.os.Environment.MEDIA_MOUNTED .equals(android.os.Environment .getExternalStorageState())) { updateDir = new File(Environment .getExternalStorageDirectory().getPath() + "/townnet/apk/"); } else { updateDir = new File("/data/data/" + context.getPackageName() + "/apk/"); } if (!updateDir.exists()) { updateDir.mkdirs(); } apkFile = new File(updateDir.getPath(), "townnet.apk"); if (apkFile.exists()) { apkFile.delete(); } // 修改文件夹及安装包的权限,供第三方应用访问 try { Runtime.getRuntime().exec( "chmod 705 " + updateDir.getPath()); Runtime.getRuntime().exec("chmod 604 " + apkFile.getPath()); } catch (Exception e) { e.printStackTrace(); } int temp; byte[] bytes = new byte[1024]; FileOutputStream out = new FileOutputStream(apkFile); while ((temp = in.read(bytes)) != -1) { out.write(bytes, 0, temp); curSize += temp; progress = curSize * 100 / totalSize; publishProgress(curSize); } in.close(); out.close(); } catch (Exception e) { e.printStackTrace(); return DOWNLOAD_ERROR; } return DOWNLOAD_OK; } @Override protected void onPreExecute() { super.onPreExecute(); } @Override protected void onPostExecute(Integer result) { super.onPostExecute(result); if (result == DOWNLOAD_OK) { Intent intent = new Intent(Intent.ACTION_VIEW); Uri photoURI = FileProvider.getUriForFile(context, context.getPackageName() + ".provider", new File(apkFile.getPath()) ); intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); if (Build.VERSION.SDK_INT >= 24) { intent.setDataAndType(photoURI, "application/vnd.android.package-archive"); } else { intent.setDataAndType(Uri.parse("file://" + apkFile.getPath()), "application/vnd.android.package-archive"); } context.startActivity(intent); } } @Override protected void onProgressUpdate(Integer... values) { super.onProgressUpdate(values); pBar.setProgress(values[0]); // 这里就是关键的实时更新进度了! if (pBar.getProgress()==apkSzie){ pBar.cancel(); } } @Override protected void onCancelled() { super.onCancelled(); if (pBar!=null){ pBar.cancel(); } } }
}
那你说怎么用呢?很简单呀。
//开始下载
new UpdataAPP(mContext).updateAPP(apkDownLoadUrl);
下面是:AndroidManifest.xml的FileProvider:
<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/filepaths" /> </provider>
xml下的filepaths:
相关文章推荐
- Android 升级apk 兼容6.0 7.0
- Android7.0的手机升级安装APK时,出现FileUriExposedException错误
- android 使用FileProvider 兼容apk 在7.0版本无法安装
- [Android开发] app在华为手机的应用市场和安装时候显示未兼容android7.0
- Android调用系统相机、相册功能,适配6.0权限获取以及7.0以后获取URI(兼容多版本)
- android app版本升级(DownloadManager、适配6.0、7.0)
- Android手机获取相机权限终极大招(兼容国产手机小米华为魅族以及6.0以下系统)
- android版本更新适配7.0,解决7.0手机无法更新安装apk
- 兼容Android系统7.0,升级安装apk
- 关于android指纹识别兼容6.0以下版本
- 史前巨作之 Android 5.0 6.0 7.0 版本 调用相机闪退的完美解决方案
- 图片选择器(兼容6.0及7.0版本)
- 兼容Android 7.0/6.0 启动系统相机/相册 选择图片方法
- Android 7.0版本升级解析包错误
- Android android 6.0权限校验及版本兼容问题
- Android 华为手机获取EMUI版本
- Android拍照及图片裁剪、调用系统相册(兼容6.0以上权限处理及7.0以上文件管理)
- Android——UI篇:android视频压缩:兼容7.0版本。
- [android] apk 版本升级
- 安装 assets 中的apk ,兼容6.0权限 处理7.0的崩溃