yanyi 6 лет назад
Родитель
Сommit
eb08ad239e

+ 2 - 1
README.md

@@ -24,9 +24,10 @@ project 下添加
 #### 方法二(最新)
 #### 方法二(最新)
 module 下添加
 module 下添加
 
 
-    compile 'com.yanyi.benyanyi:okhttplib:1.1.0'
+    compile 'com.yanyi.benyanyi:okhttplib:1.1.1'
     
     
 ### 更新记录
 ### 更新记录
+* 2018/11/20(1.1.1) 添加带弹窗下载方法(UpdateUtil类),支持下载弹窗提示,下载进度弹窗,下载进度通知栏,强行下载
 * 2018/10/10(1.1.0) 添加写入超时限定
 * 2018/10/10(1.1.0) 添加写入超时限定
 * 2018/09/28(1.0.9) 添加自定义Request方法
 * 2018/09/28(1.0.9) 添加自定义Request方法
 * 2018/09/14(1.0.8) 优化下载文件方法
 * 2018/09/14(1.0.8) 优化下载文件方法

+ 1 - 1
app/src/main/java/com/mylove/okhttp/MainActivity.java

@@ -52,7 +52,7 @@ public class MainActivity extends AppCompatActivity {
         oMap.put("CodeID", "");
         oMap.put("CodeID", "");
         oMap.put("Status", "");
         oMap.put("Status", "");
         oMap.put("DataSet", "");
         oMap.put("DataSet", "");
-        OkHttpUtil.getInstance(this).post(url).async(oMap, new onOkHttpListener() {
+        OkHttpUtil.getInstance(this).post(url).async(oMap, new OnOkHttpListener() {
             @Override
             @Override
             public void onCompleted() {
             public void onCompleted() {
 
 

+ 1 - 1
okhttplib/bintrayUpload.gradle

@@ -7,7 +7,7 @@ def siteUrl = 'https://github.com/BenYanYi/OkHttp' // 项目主页。
 def gitUrl = 'https://github.com/BenYanYi/OkHttp.git' // Git仓库的url。
 def gitUrl = 'https://github.com/BenYanYi/OkHttp.git' // Git仓库的url。
 
 
 group = "com.yanyi.benyanyi"// 唯一包名,比如compile 'com.ansen.http:okhttpencapsulation:1.0.1'中的com.ansen.http就是这里配置的。
 group = "com.yanyi.benyanyi"// 唯一包名,比如compile 'com.ansen.http:okhttpencapsulation:1.0.1'中的com.ansen.http就是这里配置的。
-version = "1.1.0"//项目引用的版本号,比如compile 'com.ansen.http:okhttpencapsulation:1.0.1'中的1.0.1就是这里配置的。
+version = "1.1.0-1"//项目引用的版本号,比如compile 'com.ansen.http:okhttpencapsulation:1.0.1'中的1.0.1就是这里配置的。
 install {
 install {
     repositories.mavenInstaller {
     repositories.mavenInstaller {
         // This generates POM.xml with proper parameters
         // This generates POM.xml with proper parameters

+ 8 - 8
okhttplib/src/main/java/com/mylove/okhttp/AutoIsRequest.java

@@ -27,23 +27,23 @@ public class AutoIsRequest {
         return instance;
         return instance;
     }
     }
 
 
-    public void sync(onOkHttpCallBack onOkHttpCallBack) {
-        ObservableIsRequest.getInstance(mContext, CallType.SYNC).request(url, onOkHttpCallBack);
+    public void sync(OnOkHttpCallBack OnOkHttpCallBack) {
+        ObservableIsRequest.getInstance(mContext, CallType.SYNC).request(url, OnOkHttpCallBack);
     }
     }
 
 
-    public <T> void sync(Class<T> tClass, onOkHttpCallBack onOkHttpCallBack) {
+    public <T> void sync(Class<T> tClass, OnOkHttpCallBack OnOkHttpCallBack) {
         ObservableIsRequests request = ObservableIsRequests.getInstance(mContext, CallType.SYNC);
         ObservableIsRequests request = ObservableIsRequests.getInstance(mContext, CallType.SYNC);
         request.tClass = tClass;
         request.tClass = tClass;
-        request.request(url, onOkHttpCallBack);
+        request.request(url, OnOkHttpCallBack);
     }
     }
 
 
-    public void async(onOkHttpCallBack onOkHttpCallBack) {
-        ObservableIsRequest.getInstance(mContext, CallType.ASYNC).request(url, onOkHttpCallBack);
+    public void async(OnOkHttpCallBack OnOkHttpCallBack) {
+        ObservableIsRequest.getInstance(mContext, CallType.ASYNC).request(url, OnOkHttpCallBack);
     }
     }
 
 
-    public <T> void async(Class<T> tClass, onOkHttpCallBack onOkHttpCallBack) {
+    public <T> void async(Class<T> tClass, OnOkHttpCallBack OnOkHttpCallBack) {
         ObservableIsRequests request = ObservableIsRequests.getInstance(mContext, CallType.ASYNC);
         ObservableIsRequests request = ObservableIsRequests.getInstance(mContext, CallType.ASYNC);
         request.tClass = tClass;
         request.tClass = tClass;
-        request.request(url, onOkHttpCallBack);
+        request.request(url, OnOkHttpCallBack);
     }
     }
 }
 }

+ 8 - 8
okhttplib/src/main/java/com/mylove/okhttp/AutoRequest.java

@@ -31,23 +31,23 @@ public class AutoRequest {
         return instance;
         return instance;
     }
     }
 
 
-    public void sync(Map<Object, Object> oMap, onOkHttpListener onOkHttpListener) {
-        ObservableRequest.getInstance(mContext, requestType, CallType.SYNC).request(url, oMap, onOkHttpListener);
+    public void sync(Map<Object, Object> oMap, OnOkHttpListener OnOkHttpListener) {
+        ObservableRequest.getInstance(mContext, requestType, CallType.SYNC).request(url, oMap, OnOkHttpListener);
     }
     }
 
 
-    public <T> void sync(Map<Object, Object> oMap, Class<T> tClass, onOkHttpListener onOkHttpListener) {
+    public <T> void sync(Map<Object, Object> oMap, Class<T> tClass, OnOkHttpListener OnOkHttpListener) {
         ObservableRequests request = ObservableRequests.getInstance(mContext, requestType, CallType.SYNC);
         ObservableRequests request = ObservableRequests.getInstance(mContext, requestType, CallType.SYNC);
         request.tClass = tClass;
         request.tClass = tClass;
-        request.request(url, oMap, onOkHttpListener);
+        request.request(url, oMap, OnOkHttpListener);
     }
     }
 
 
-    public void async(Map<Object, Object> oMap, onOkHttpListener onOkHttpListener) {
-        ObservableRequest.getInstance(mContext, requestType, CallType.ASYNC).request(url, oMap, onOkHttpListener);
+    public void async(Map<Object, Object> oMap, OnOkHttpListener OnOkHttpListener) {
+        ObservableRequest.getInstance(mContext, requestType, CallType.ASYNC).request(url, oMap, OnOkHttpListener);
     }
     }
 
 
-    public <T> void async(Map<Object, Object> oMap, Class<T> tClass, onOkHttpListener onOkHttpListener) {
+    public <T> void async(Map<Object, Object> oMap, Class<T> tClass, OnOkHttpListener OnOkHttpListener) {
         ObservableRequests request = ObservableRequests.getInstance(mContext, requestType, CallType.ASYNC);
         ObservableRequests request = ObservableRequests.getInstance(mContext, requestType, CallType.ASYNC);
         request.tClass = tClass;
         request.tClass = tClass;
-        request.request(url, oMap, onOkHttpListener);
+        request.request(url, oMap, OnOkHttpListener);
     }
     }
 }
 }

+ 57 - 0
okhttplib/src/main/java/com/mylove/okhttp/DownloadDialog.java

@@ -0,0 +1,57 @@
+package com.mylove.okhttp;
+
+import android.content.Context;
+import android.content.DialogInterface;
+import android.support.v7.app.AlertDialog;
+
+/**
+ * @author BenYanYi
+ * @date 2018/11/20 09:45
+ * @email ben@yanyi.red
+ * @overview
+ */
+public class DownloadDialog {
+    private String title = "升级";
+    private String message = "发现新的安装包";
+    private boolean isLimit = false;//是否可以关闭
+
+    public DownloadDialog() {
+    }
+
+    public DownloadDialog setTitle(String title) {
+        this.title = title;
+        return this;
+    }
+
+    public DownloadDialog setMessage(String message) {
+        this.message = message;
+        return this;
+    }
+
+    public DownloadDialog setLimit(boolean limit) {
+        isLimit = limit;
+        return this;
+    }
+
+    void init(Context context) {
+        final AlertDialog.Builder builder = new AlertDialog.Builder(context);
+        builder.setTitle(title);
+        builder.setMessage(message);
+        builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
+            @Override
+            public void onClick(DialogInterface dialog, int which) {
+
+            }
+        });
+        if (!isLimit) {
+            builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
+                @Override
+                public void onClick(DialogInterface dialog, int which) {
+                    dialog.dismiss();
+                }
+            });
+        }
+        builder.setCancelable(isLimit);
+        builder.show();
+    }
+}

+ 15 - 0
okhttplib/src/main/java/com/mylove/okhttp/FileUtil.java

@@ -71,4 +71,19 @@ class FileUtil {
         return url.substring(url.lastIndexOf("/") + 1);
         return url.substring(url.lastIndexOf("/") + 1);
     }
     }
 
 
+    /**
+     * 判断当前url下载的文件是否为自己所需的
+     *
+     * @param url
+     * @param condition 判断条件
+     * @return
+     */
+    static boolean ifUrl(String url, String condition) {
+        String str = url.substring(url.lastIndexOf("."));
+        if (FormatUtil.isNotEmpty(str)) {
+            return str.equals(condition);
+        } else {
+            return false;
+        }
+    }
 }
 }

+ 143 - 0
okhttplib/src/main/java/com/mylove/okhttp/NotificationUtil.java

@@ -0,0 +1,143 @@
+package com.mylove.okhttp;
+
+import android.app.Notification;
+import android.app.NotificationChannel;
+import android.app.NotificationManager;
+import android.app.PendingIntent;
+import android.content.Context;
+import android.content.Intent;
+import android.graphics.Color;
+import android.widget.RemoteViews;
+
+import java.util.HashMap;
+import java.util.Map;
+
+/**
+ * @author BenYanYi
+ * @date 2018/11/20 15:51
+ * @email ben@yanyi.red
+ * @overview
+ */
+public class NotificationUtil {
+    private Context mContext;
+    // NotificationManager : 是状态栏通知的管理类,负责发通知、清楚通知等。
+    private NotificationManager manager;
+    // 定义Map来保存Notification对象
+    private Map<Integer, Notification> map = null;
+    private int icon;
+    private Class<?> aClass;
+    private boolean isCreateChannel = false;
+    private static final String NOTIFICATION_CHANNEL_NAME = "Update";
+    private NotificationManager notificationManager = null;
+    private String tickerText;
+
+    public NotificationUtil(Context context) {
+        this.mContext = context;
+        // NotificationManager 是一个系统Service,必须通过 getSystemService()方法来获取。
+        manager = (NotificationManager) mContext
+                .getSystemService(Context.NOTIFICATION_SERVICE);
+        map = new HashMap<>();
+    }
+
+    public NotificationUtil setIcon(int icon) {
+        this.icon = icon;
+        return this;
+    }
+
+    public NotificationUtil setClass(Class<?> aClass) {
+        this.aClass = aClass;
+        return this;
+    }
+
+    public NotificationUtil setTickerText(String tickerText) {
+        this.tickerText = tickerText;
+        return this;
+    }
+
+    public void showNotification(int notificationId) {
+        // 判断对应id的Notification是否已经显示, 以免同一个Notification出现多次
+        if (!map.containsKey(notificationId)) {
+            // 创建通知对象
+            Notification notification;
+            Notification.Builder builder;
+            if (android.os.Build.VERSION.SDK_INT >= 26) {
+                //Android O上对Notification进行了修改,如果设置的targetSDKVersion>=26建议使用此种方式创建通知栏
+                if (null == notificationManager) {
+                    notificationManager = (NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE);
+                }
+                String channelId = mContext.getPackageName();
+                if (!isCreateChannel) {
+                    NotificationChannel notificationChannel = new NotificationChannel(channelId,
+                            NOTIFICATION_CHANNEL_NAME, NotificationManager.IMPORTANCE_DEFAULT);
+                    notificationChannel.enableLights(true);//是否在桌面icon右上角展示小圆点
+                    notificationChannel.setLightColor(Color.BLUE); //小圆点颜色
+                    notificationChannel.setShowBadge(true); //是否在久按桌面图标时显示此渠道的通知
+                    notificationManager.createNotificationChannel(notificationChannel);
+                    isCreateChannel = true;
+                }
+                builder = new Notification.Builder(mContext.getApplicationContext(), channelId);
+                notification = builder.build();
+            } else {
+                notification = new Notification();
+            }
+            // 设置通知栏滚动显示文字
+            notification.tickerText = tickerText;
+            // 设置显示时间
+            notification.when = System.currentTimeMillis();
+            // 设置通知显示的图标
+            notification.icon = icon;
+            // 设置通知的特性: 通知被点击后,自动消失
+            notification.flags = Notification.FLAG_AUTO_CANCEL;
+            // 设置点击通知栏操作
+            if (aClass != null) {
+                Intent in = new Intent(mContext, aClass);// 点击跳转到指定页面
+                notification.contentIntent = PendingIntent.getActivity(mContext, 0, in,
+                        0);
+            }
+            // 设置通知的显示视图
+            notification.contentView = new RemoteViews(
+                    mContext.getPackageName(),
+                    R.layout.notification_contentview);
+            // 发出通知
+            manager.notify(notificationId, notification);
+            map.put(notificationId, notification);// 存入Map中
+        }
+    }
+
+    /**
+     * 取消通知操作
+     */
+    public void cancel(int notificationId) {
+        manager.cancel(notificationId);
+        map.remove(notificationId);
+    }
+
+    public void updateProgress(int notificationId, int progress) {
+        Notification notify = map.get(notificationId);
+        if (null != notify) {
+            // 修改进度条
+            notify.contentView.setProgressBar(R.id.pBar, 100, progress, false);
+            manager.notify(notificationId, notify);
+        }
+    }
+
+    public void tickerText(int notificationId, String text) {
+        Notification notify = map.get(notificationId);
+        if (null != notify) {
+            // 修改进度条
+            notify.tickerText = text;
+            manager.notify(notificationId, notify);
+        }
+    }
+
+    public void updateProgress(int notificationId, int progress, String text) {
+        Notification notify = map.get(notificationId);
+        if (null != notify) {
+            notify.tickerText = text;
+            // 修改进度条
+            notify.contentView.setProgressBar(R.id.pBar, 100, progress, false);
+            manager.notify(notificationId, notify);
+        }
+    }
+
+}

+ 3 - 3
okhttplib/src/main/java/com/mylove/okhttp/ObservableIsRequest.java

@@ -34,7 +34,7 @@ class ObservableIsRequest {
     private static OkHttpClient okHttpClient;
     private static OkHttpClient okHttpClient;
     private String mCacheUrl = "";
     private String mCacheUrl = "";
 
 
-    private onOkHttpCallBack okHttpCallBack;
+    private OnOkHttpCallBack okHttpCallBack;
 
 
     static ObservableIsRequest getInstance(Context context, CallType type2) {
     static ObservableIsRequest getInstance(Context context, CallType type2) {
         if (instance == null) {
         if (instance == null) {
@@ -58,8 +58,8 @@ class ObservableIsRequest {
         return instance;
         return instance;
     }
     }
 
 
-    void request(String url, onOkHttpCallBack onOkHttpCallBack) {
-        this.okHttpCallBack = onOkHttpCallBack;
+    void request(String url, OnOkHttpCallBack OnOkHttpCallBack) {
+        this.okHttpCallBack = OnOkHttpCallBack;
         getObservable(url).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread())
         getObservable(url).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread())
                 .serialize()//保证上游下游同一线程 ,防止不同线程下 onError 通知会跳到(并吞掉)原始Observable发射的数据项前面的错误行为
                 .serialize()//保证上游下游同一线程 ,防止不同线程下 onError 通知会跳到(并吞掉)原始Observable发射的数据项前面的错误行为
                 .subscribe(new Observer<String>() {
                 .subscribe(new Observer<String>() {

+ 3 - 3
okhttplib/src/main/java/com/mylove/okhttp/ObservableIsRequests.java

@@ -38,7 +38,7 @@ class ObservableIsRequests<T> {
     private static OkHttpClient okHttpClient;
     private static OkHttpClient okHttpClient;
     private String mCacheUrl = "";
     private String mCacheUrl = "";
 
 
-    private onOkHttpCallBack okHttpCallBack;
+    private OnOkHttpCallBack okHttpCallBack;
 
 
     public Class<T> tClass;
     public Class<T> tClass;
 
 
@@ -64,8 +64,8 @@ class ObservableIsRequests<T> {
         return instance;
         return instance;
     }
     }
 
 
-    void request(String url, onOkHttpCallBack onOkHttpCallBack) {
-        this.okHttpCallBack = onOkHttpCallBack;
+    void request(String url, OnOkHttpCallBack OnOkHttpCallBack) {
+        this.okHttpCallBack = OnOkHttpCallBack;
         getObservable(url).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread())
         getObservable(url).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread())
                 .serialize().subscribe(new Observer<T>() {
                 .serialize().subscribe(new Observer<T>() {
             @Override
             @Override

+ 4 - 4
okhttplib/src/main/java/com/mylove/okhttp/ObservableRequest.java

@@ -65,7 +65,7 @@ class ObservableRequest {
         return instance;
         return instance;
     }
     }
 
 
-    void request(String url, Map<Object, Object> oMap, final onOkHttpListener onOkHttpListener) {
+    void request(String url, Map<Object, Object> oMap, final OnOkHttpListener OnOkHttpListener) {
         getObservable(url, oMap).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread())
         getObservable(url, oMap).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread())
                 .serialize()//保证上游下游同一线程 ,防止不同线程下 onError 通知会跳到(并吞掉)原始Observable发射的数据项前面的错误行为
                 .serialize()//保证上游下游同一线程 ,防止不同线程下 onError 通知会跳到(并吞掉)原始Observable发射的数据项前面的错误行为
                 .subscribe(new Observer<String>() {
                 .subscribe(new Observer<String>() {
@@ -76,17 +76,17 @@ class ObservableRequest {
 
 
                     @Override
                     @Override
                     public void onNext(String str) {
                     public void onNext(String str) {
-                        onOkHttpListener.onSuccess(str);
+                        OnOkHttpListener.onSuccess(str);
                     }
                     }
 
 
                     @Override
                     @Override
                     public void onError(Throwable e) {
                     public void onError(Throwable e) {
-                        onOkHttpListener.onFailure(e);
+                        OnOkHttpListener.onFailure(e);
                     }
                     }
 
 
                     @Override
                     @Override
                     public void onComplete() {
                     public void onComplete() {
-                        onOkHttpListener.onCompleted();
+                        OnOkHttpListener.onCompleted();
                     }
                     }
                 });
                 });
     }
     }

+ 4 - 4
okhttplib/src/main/java/com/mylove/okhttp/ObservableRequests.java

@@ -71,7 +71,7 @@ class ObservableRequests<T> {
         return instance;
         return instance;
     }
     }
 
 
-    void request(String url, Map<Object, Object> oMap, final onOkHttpListener onOkHttpListener) {
+    void request(String url, Map<Object, Object> oMap, final OnOkHttpListener OnOkHttpListener) {
         getObservable(url, oMap).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread())
         getObservable(url, oMap).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread())
                 .serialize().subscribe(new Observer<T>() {
                 .serialize().subscribe(new Observer<T>() {
             @Override
             @Override
@@ -81,17 +81,17 @@ class ObservableRequests<T> {
 
 
             @Override
             @Override
             public void onNext(T t) {
             public void onNext(T t) {
-                onOkHttpListener.onSuccess(t);
+                OnOkHttpListener.onSuccess(t);
             }
             }
 
 
             @Override
             @Override
             public void onError(Throwable e) {
             public void onError(Throwable e) {
-                onOkHttpListener.onFailure(e);
+                OnOkHttpListener.onFailure(e);
             }
             }
 
 
             @Override
             @Override
             public void onComplete() {
             public void onComplete() {
-                onOkHttpListener.onCompleted();
+                OnOkHttpListener.onCompleted();
             }
             }
         });
         });
     }
     }

+ 13 - 0
okhttplib/src/main/java/com/mylove/okhttp/OnDownloadFileCallBack.java

@@ -0,0 +1,13 @@
+package com.mylove.okhttp;
+
+/**
+ * @author BenYanYi
+ * @date 2018/11/20 17:05
+ * @email ben@yanyi.red
+ * @overview
+ */
+public interface OnDownloadFileCallBack {
+    void onDownloading(int progress);
+
+    void onSuccess();
+}

+ 1 - 1
okhttplib/src/main/java/com/mylove/okhttp/OnDownloadListener.java

@@ -6,7 +6,7 @@ package com.mylove.okhttp;
  * @email ben@yanyi.red
  * @email ben@yanyi.red
  * @overview
  * @overview
  */
  */
-public interface OnDownloadListener extends onOkHttpListener {
+public interface OnDownloadListener extends OnOkHttpListener {
     /**
     /**
      * @param progress 下载进度
      * @param progress 下载进度
      */
      */

+ 1 - 1
okhttplib/src/main/java/com/mylove/okhttp/onOkHttpCallBack.java → okhttplib/src/main/java/com/mylove/okhttp/OnOkHttpCallBack.java

@@ -8,6 +8,6 @@ import okhttp3.Request;
  * @email ben@yanyi.red
  * @email ben@yanyi.red
  * @overview
  * @overview
  */
  */
-public interface onOkHttpCallBack extends onOkHttpListener {
+public interface OnOkHttpCallBack extends OnOkHttpListener {
     Request setRequest(String url);
     Request setRequest(String url);
 }
 }

+ 1 - 1
okhttplib/src/main/java/com/mylove/okhttp/onOkHttpListener.java → okhttplib/src/main/java/com/mylove/okhttp/OnOkHttpListener.java

@@ -4,7 +4,7 @@ package com.mylove.okhttp;
  * @author myLove
  * @author myLove
  */
  */
 
 
-public interface onOkHttpListener {
+public interface OnOkHttpListener {
     void onCompleted();
     void onCompleted();
 
 
     <T> void onSuccess(T message);
     <T> void onSuccess(T message);

+ 264 - 0
okhttplib/src/main/java/com/mylove/okhttp/UpdateUtil.java

@@ -0,0 +1,264 @@
+package com.mylove.okhttp;
+
+import android.app.Activity;
+import android.app.ProgressDialog;
+import android.content.DialogInterface;
+import android.content.Intent;
+import android.net.Uri;
+import android.os.Build;
+import android.support.annotation.DrawableRes;
+import android.support.v4.content.FileProvider;
+import android.support.v7.app.AlertDialog;
+import android.widget.Toast;
+
+import java.io.File;
+
+/**
+ * @author BenYanYi
+ * @date 2018/11/20 10:23
+ * @email ben@yanyi.red
+ * @overview
+ */
+public class UpdateUtil {
+    private Activity mActivity;
+
+    private String downloadUrl = "";//app下载地址
+    private String title = "升级";
+    private String message = "发现新的安装包";
+    private boolean isLimit = false;//是否可以关闭
+
+    private boolean isShowNotice = false;//是否显示通知栏
+
+    private int icon;
+
+    private NotificationUtil notificationUtil;
+
+    private Class<?> aClass;
+
+    private ProgressDialog progressDialog;
+
+    private boolean isInstallApk = false;
+    private OnDownloadCallBack downloadCallBack;
+
+    private String filePath;
+
+    public UpdateUtil(Activity mActivity, String downloadUrl) {
+        this.mActivity = mActivity;
+        setDownloadUrl(downloadUrl);
+    }
+
+    public UpdateUtil setDownloadUrl(String downloadUrl) {
+        if (FormatUtil.isEmpty(downloadUrl)) {
+            throw new NullPointerException("");
+        }
+        this.downloadUrl = downloadUrl;
+        return this;
+    }
+
+    /**
+     * 设置弹窗标题
+     */
+    public UpdateUtil setTitle(String title) {
+        if (FormatUtil.isNotEmpty(title)) {
+            this.title = title;
+        }
+        return this;
+    }
+
+    /**
+     * 设置弹窗内容
+     */
+    public UpdateUtil setMessage(String message) {
+        if (FormatUtil.isNotEmpty(message)) {
+            this.message = message;
+        }
+        return this;
+    }
+
+    /**
+     * 是否需要强制下载
+     */
+    public UpdateUtil setLimit(boolean limit) {
+        isLimit = limit;
+        return this;
+    }
+
+    /**
+     * 是否显示通知弹窗
+     */
+    public UpdateUtil setShowNotice(boolean showNotice) {
+        isShowNotice = showNotice;
+        return this;
+    }
+
+    /**
+     * 通知弹窗跳转页面
+     *
+     * @param aClass
+     */
+    public UpdateUtil setStartClass(Class<?> aClass) {
+        this.aClass = aClass;
+        return this;
+    }
+
+    /**
+     * 设置图标
+     */
+    public UpdateUtil setIcon(@DrawableRes int icon) {
+        this.icon = icon;
+        return this;
+    }
+
+    /**
+     * 是否需要安装
+     */
+    public UpdateUtil setInstallApk(boolean installApk) {
+        isInstallApk = installApk;
+        return this;
+    }
+
+    /**
+     * 下载监听
+     */
+    public UpdateUtil setDownloadCallBack(OnDownloadCallBack downloadCallBack) {
+        this.downloadCallBack = downloadCallBack;
+        return this;
+    }
+
+    /**
+     * 存储目录
+     */
+    public UpdateUtil setFilePath(String filePath) {
+        this.filePath = filePath;
+        return this;
+    }
+
+    /**
+     * 升级
+     */
+    public void request() {
+        if (FormatUtil.isEmpty(title)) {
+            title = "提示";
+        }
+        if (FormatUtil.isEmpty(message)) {
+            message = "是否下载";
+        }
+        if (isShowNotice) {
+            notificationUtil = new NotificationUtil(mActivity.getApplicationContext()).setIcon(icon).setTickerText(title);
+            if (aClass != null) {
+                notificationUtil.setClass(aClass);
+            }
+        }
+        AlertDialog.Builder builder = new AlertDialog.Builder(mActivity.getApplicationContext());
+        if (icon != 0) {
+            builder.setIcon(icon);
+        }
+        builder.setTitle(title);
+        builder.setMessage(message);
+        builder.setPositiveButton("立即更新", new DialogInterface.OnClickListener() {
+            @Override
+            public void onClick(DialogInterface dialog, int which) {
+                dialog.dismiss();
+                progressDialog();
+            }
+        });
+        if (!isLimit) {
+            builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
+                @Override
+                public void onClick(DialogInterface dialog, int which) {
+                    dialog.dismiss();
+                }
+            });
+        }
+        builder.setCancelable(!isLimit);
+        builder.show();
+    }
+
+    private void progressDialog() {
+        progressDialog = new ProgressDialog(mActivity.getApplicationContext());
+        progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
+        progressDialog.setTitle(title);
+        progressDialog.setMessage("正在下载");
+        progressDialog.setMax(100);
+        progressDialog.setProgress(0);
+        // 设置ProgressDialog 的进度条是否不明确
+        progressDialog.setIndeterminate(false);
+        progressDialog.setCancelable(false);
+        if (icon != 0)
+            progressDialog.setIcon(icon);
+        progressDialog.show();
+        notificationUtil.showNotification(1020);
+        download();
+    }
+
+    private void download() {
+        if (FormatUtil.isEmpty(filePath)) {
+            filePath = mActivity.getResources().getString(R.string.app_name);
+        }
+        OkHttpUtil.getInstance(mActivity.getApplication()).downloadFile(downloadUrl).downloads(filePath, new OnDownloadCallBack() {
+            @Override
+            public void onDownloading(int progress) {
+                if (notificationUtil != null && isShowNotice) {
+                    notificationUtil.updateProgress(1020, progress, "已下载" + progress + "%");
+                }
+                progressDialog.setProgress(progress);
+                if (downloadCallBack != null) {
+                    downloadCallBack.onDownloading(progress);
+                }
+            }
+
+            @Override
+            public void onSuccess(String filePath) {
+                progressDialog.dismiss();
+                notificationUtil.cancel(1020);
+                if (isInstallApk && FileUtil.ifUrl(filePath, ".apk")) {
+                    installApk(new File(filePath));
+                }
+                if (downloadCallBack != null) {
+                    downloadCallBack.onSuccess(filePath);
+                }
+            }
+
+            @Override
+            public void onFailure(Throwable throwable) {
+                Toast.makeText(mActivity.getApplicationContext(), "下载失败", Toast.LENGTH_SHORT).show();
+                LogHelper.e(throwable.getMessage());
+                if (downloadCallBack != null) {
+                    downloadCallBack.onFailure(throwable);
+                }
+            }
+        });
+    }
+
+    /**
+     * 安装下载完成的安装包
+     *
+     * @param file
+     */
+    public void installApk(final File file) {
+//        if (Build.VERSION.SDK_INT >= 26) {
+//            boolean b = getPackageManager().canRequestPackageInstalls();
+//            if (b) {
+//                installApk();//安装应用的逻辑(写自己的就可以)
+//            } else {
+//                //请求安装未知应用来源的权限
+//                ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.REQUEST_INSTALL_PACKAGES}, INSTALL_PACKAGES_REQUESTCODE);
+//            }
+//        }
+        //判读版本是否在7.0以上
+        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
+            //在AndroidManifest中的android:authorities值
+            Uri apkUri = FileProvider.getUriForFile(mActivity.getApplication(), mActivity.getPackageName() + ".fileProvider", file);
+            Intent install = new Intent(Intent.ACTION_VIEW);
+            install.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
+            install.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
+            install.setDataAndType(apkUri, "application/vnd.android.package-archive");
+            mActivity.startActivity(install);
+        } else {
+            Intent install = new Intent(Intent.ACTION_VIEW);
+            install.setDataAndType(Uri.fromFile(file), "application/vnd.android.package-archive");
+            install.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
+            mActivity.startActivity(install);
+        }
+    }
+}

+ 4 - 4
okhttplib/src/main/java/com/mylove/okhttp/XMLRequest.java

@@ -34,11 +34,11 @@ public class XMLRequest {
         return instance;
         return instance;
     }
     }
 
 
-    public void sync(Map<Object, Object> map, onOkHttpListener onOkHttpListener) {
-        ObservableRequest.getInstance(mContext, requestType, CallType.SYNC).request(url, map, onOkHttpListener);
+    public void sync(Map<Object, Object> map, OnOkHttpListener OnOkHttpListener) {
+        ObservableRequest.getInstance(mContext, requestType, CallType.SYNC).request(url, map, OnOkHttpListener);
     }
     }
 
 
-    public void async(Map<Object, Object> map, onOkHttpListener onOkHttpListener) {
-        ObservableRequest.getInstance(mContext, requestType, CallType.ASYNC).request(url, map, onOkHttpListener);
+    public void async(Map<Object, Object> map, OnOkHttpListener OnOkHttpListener) {
+        ObservableRequest.getInstance(mContext, requestType, CallType.ASYNC).request(url, map, OnOkHttpListener);
     }
     }
 }
 }

+ 29 - 0
okhttplib/src/main/res/layout/notification_contentview.xml

@@ -0,0 +1,29 @@
+<?xml version="1.0" encoding="utf-8"?>
+<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
+    xmlns:tools="http://schemas.android.com/tools"
+    android:layout_width="match_parent"
+    android:layout_height="match_parent"
+    android:orientation="vertical">
+
+    <TextView
+        android:id="@+id/title"
+        android:layout_width="wrap_content"
+        android:layout_height="wrap_content"
+        android:text="通知栏下载测试" />
+
+    <LinearLayout
+        android:layout_width="match_parent"
+        android:layout_height="wrap_content"
+        android:gravity="center"
+        android:orientation="horizontal">
+
+        <ProgressBar
+            android:id="@+id/pBar"
+            style="@android:style/Widget.ProgressBar.Horizontal"
+            android:layout_width="match_parent"
+            android:layout_height="4dp"
+            android:layout_weight="1" />
+
+    </LinearLayout>
+
+</LinearLayout>