您的位置:首页 > 移动开发 > Android开发

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 下载呗,你想呀,无非就是下载一个文件而已。

多的不说直接上代码,下面是个工具类直接用就可以:

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:

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: