7.0以上版本要注意:添加以下代碼。還要再M(fèi)anifast中增加Provider赃额,在res目錄下創(chuàng)建xml目錄加派,在xml目錄下創(chuàng)建file_path.xml文件
public class UpdateTaskextends AsyncTask?{
private Contextcontext;
? ? private ProgressDialogpd; //進(jìn)度條對話框
? ? public UpdateTask(Context context) {
this.context = context;
? ? }
@Override
? ? protected void onPreExecute() {
pd =new ProgressDialog(context);
? ? ? ? pd.setCanceledOnTouchOutside(false);
? ? ? ? pd.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
? ? ? ? pd.setMessage("正在下載更新");
? ? ? ? pd.setProgressNumberFormat("%1d kb/%2d kb");//進(jìn)度和總數(shù)
? ? ? ? pd.setCancelable(false);
? ? ? ? pd.show();
? ? ? ? super.onPreExecute();
? ? }
@Override
? ? protected FiledoInBackground(String... params) {
String path = params[0];
? ? ? ? String verNew = params[1];
? ? ? ? HttpURLConnection conn =null;
? ? ? ? File file =null;
? ? ? ? //如果相等的話表示當(dāng)前的sdcard掛載在手機(jī)上并且是可用的
? ? ? ? if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
URL url =null;
? ? ? ? ? ? try {
url =new URL(path);
? ? ? ? ? ? }catch (MalformedURLException e) {
e.printStackTrace();
? ? ? ? ? ? }
try {
if (url.getProtocol().toLowerCase().equals("https")) {
HttpsURLConnection https = (HttpsURLConnection) url.openConnection();
? ? ? ? ? ? ? ? ? ? https.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
? ? ? ? ? ? ? ? ? ? conn = https;
? ? ? ? ? ? ? ? }else {
conn = (HttpURLConnection) url.openConnection();
? ? ? ? ? ? ? ? }
conn.setConnectTimeout(10000);
? ? ? ? ? ? ? ? //獲取到文件的大小
? ? ? ? ? ? ? ? int count = conn.getContentLength();
? ? ? ? ? ? ? ? InputStream is = conn.getInputStream();
? ? ? ? ? ? ? ? file =new File(Environment.getExternalStorageDirectory(), "nodepp." + verNew +".apk");//下載到本地的app名
? ? ? ? ? ? ? ? FileOutputStream fos =new FileOutputStream(file);
? ? ? ? ? ? ? ? BufferedInputStream bis =new BufferedInputStream(is);
? ? ? ? ? ? ? ? byte[] buffer =new byte[1024];
? ? ? ? ? ? ? ? int len;
? ? ? ? ? ? ? ? int total =0;
? ? ? ? ? ? ? ? while ((len = bis.read(buffer)) != -1) {
fos.write(buffer, 0, len);
? ? ? ? ? ? ? ? ? ? total += len;
? ? ? ? ? ? ? ? ? ? //獲取當(dāng)前下載量
? ? ? ? ? ? ? ? ? ? publishProgress(total, count);
? ? ? ? ? ? ? ? }
fos.close();
? ? ? ? ? ? ? ? bis.close();
? ? ? ? ? ? ? ? is.close();
? ? ? ? ? ? ? ? return file;
? ? ? ? ? ? }catch (Exception e) {
e.printStackTrace();
? ? ? ? ? ? }finally {
}
}else {
return null;
? ? ? ? }
return file;
? ? }
@Override
? ? protected void onProgressUpdate(Integer... values) {
pd.setProgress(values[0] /1024);
? ? ? ? pd.setMax(values[1] /1024);
? ? ? ? super.onProgressUpdate(values);
? ? }
@Override
? ? protected void onPostExecute(File file) {
if (file !=null) {
//安裝apk
? ? ? ? ? ? Intent intent =new Intent();
? ? ? ? ? ? //執(zhí)行動(dòng)作
? ? ? ? ? ? intent.setAction(Intent.ACTION_VIEW);
? ? ? ? ? ? intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
? ? ? ? ? ? if(Build.VERSION.SDK_INT>=24) {//判讀版本是否在7.0以上
? ? ? ? ? ? ? ? //7.0以上需要通過FileProvider獲取uri
? ? ? ? ? ? ? ? Uri apkUri = FileProvider.getUriForFile(context, "com.nodepp.smartnode.fileprovider", file);
? ? ? ? ? ? ? ? intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);//添加這一句表示對目標(biāo)應(yīng)用臨時(shí)授權(quán)該Uri所代表的文件
? ? ? ? ? ? ? ? intent.setDataAndType(apkUri, "application/vnd.android.package-archive");
? ? ? ? ? ? }else {
//執(zhí)行的數(shù)據(jù)類型,此處Android應(yīng)為android跳芳,否則造成安裝不了
? ? ? ? ? ? ? ? intent.setDataAndType(Uri.fromFile(file), "application/vnd.android.package-archive");
? ? ? ? ? ? }
context.startActivity(intent);
? ? ? ? ? ? pd.dismiss();
? ? ? ? ? ? ((Activity)context).finish();
? ? ? ? }else {
pd.dismiss();
? ? ? ? ? ? // 下載app失敗
? ? ? ? ? ? AlertDialog dialog =new AlertDialog.Builder(context).setTitle("提示")
.setMessage("應(yīng)用下載失敗")
.setCancelable(false)
.setPositiveButton("確定",
? ? ? ? ? ? ? ? ? ? ? ? ? ? new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? int which) {
dialog.dismiss();
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? }
}).create();
? ? ? ? ? ? dialog.show();
? ? ? ? }
}
}