|
| 1 | +package com.example.teprinciple.updateappdemo.updateapp; |
| 2 | + |
| 3 | +import android.app.DownloadManager; |
| 4 | +import android.content.Context; |
| 5 | +import android.content.Intent; |
| 6 | +import android.net.Uri; |
| 7 | +import android.os.Environment; |
| 8 | +import android.text.TextUtils; |
| 9 | +import android.util.Log; |
| 10 | +import android.widget.Toast; |
| 11 | + |
| 12 | +import java.io.File; |
| 13 | + |
| 14 | + |
| 15 | + |
| 16 | +/** |
| 17 | + *Created by Teprinciple on 2016/12/13. |
| 18 | + */ |
| 19 | +public class DownloadAppUtils { |
| 20 | + private static final String TAG = DownloadAppUtils.class.getSimpleName(); |
| 21 | + public static long downloadUpdateApkId = -1;//下载更新Apk 下载任务对应的Id |
| 22 | + public static String downloadUpdateApkFilePath;//下载更新Apk 文件路径 |
| 23 | + |
| 24 | + /** |
| 25 | + * 通过浏览器下载APK包 |
| 26 | + * @param context |
| 27 | + * @param url |
| 28 | + */ |
| 29 | + public static void downloadForWebView(Context context, String url) { |
| 30 | + Uri uri = Uri.parse(url); |
| 31 | + Intent intent = new Intent(Intent.ACTION_VIEW, uri); |
| 32 | + intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); |
| 33 | + intent.setDataAndType(Uri.fromFile(new File(Environment |
| 34 | + .getExternalStorageDirectory(), "tmp.apk")), |
| 35 | + "application/vnd.android.package-archive"); |
| 36 | + context.startActivity(intent); |
| 37 | + } |
| 38 | + |
| 39 | + |
| 40 | + /** |
| 41 | + * 下载更新apk包 |
| 42 | + * 权限:1,<uses-permission android:name="android.permission.DOWNLOAD_WITHOUT_NOTIFICATION" /> |
| 43 | + * @param context |
| 44 | + * @param url |
| 45 | + */ |
| 46 | + public static void downloadForAutoInstall(Context context, String url, String fileName, String title) { |
| 47 | + //LogUtil.e("App 下载 url="+url+",fileName="+fileName+",title="+title); |
| 48 | + if (TextUtils.isEmpty(url)) { |
| 49 | + return; |
| 50 | + } |
| 51 | + try { |
| 52 | + Uri uri = Uri.parse(url); |
| 53 | + DownloadManager downloadManager = (DownloadManager) context |
| 54 | + .getSystemService(Context.DOWNLOAD_SERVICE); |
| 55 | + DownloadManager.Request request = new DownloadManager.Request(uri); |
| 56 | + //在通知栏中显示 |
| 57 | + request.setVisibleInDownloadsUi(true); |
| 58 | + request.setTitle(title); |
| 59 | + String filePath = null; |
| 60 | + if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {//外部存储卡 |
| 61 | + filePath = Environment.getExternalStorageDirectory().getAbsolutePath(); |
| 62 | + |
| 63 | + } else { |
| 64 | + Log.i(TAG,"没有SD卡"); |
| 65 | + return; |
| 66 | + } |
| 67 | + downloadUpdateApkFilePath = filePath + File.separator + fileName; |
| 68 | + // 若存在,则删除 |
| 69 | + deleteFile(downloadUpdateApkFilePath); |
| 70 | + Uri fileUri = Uri.parse("file://" + downloadUpdateApkFilePath); |
| 71 | + request.setDestinationUri(fileUri); |
| 72 | + downloadUpdateApkId = downloadManager.enqueue(request); |
| 73 | + } catch (Exception e) { |
| 74 | + e.printStackTrace(); |
| 75 | + downloadForWebView(context, url); |
| 76 | + }finally { |
| 77 | +// registerReceiver(receiver, new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE)); |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + |
| 82 | + private static boolean deleteFile(String fileStr) { |
| 83 | + File file = new File(fileStr); |
| 84 | + return file.delete(); |
| 85 | + } |
| 86 | +} |
0 commit comments