| xqd
@@ -1,8 +1,11 @@
|
|
|
package com.zhilin.patrol.ui.activity;
|
|
|
|
|
|
+import android.content.Context;
|
|
|
import android.content.Intent;
|
|
|
+import android.net.Uri;
|
|
|
import android.os.Bundle;
|
|
|
import android.os.Environment;
|
|
|
+import android.support.v4.content.FileProvider;
|
|
|
import android.support.v7.app.AppCompatActivity;
|
|
|
import android.text.TextUtils;
|
|
|
import android.util.Log;
|
| xqd
@@ -12,6 +15,10 @@ import android.widget.TextView;
|
|
|
|
|
|
import com.cjj.MaterialRefreshLayout;
|
|
|
import com.cjj.MaterialRefreshListener;
|
|
|
+import com.pgyersdk.update.DownloadFileListener;
|
|
|
+import com.pgyersdk.update.PgyUpdateManager;
|
|
|
+import com.pgyersdk.update.UpdateManagerListener;
|
|
|
+import com.pgyersdk.update.javabean.AppBean;
|
|
|
import com.zhilin.patrol.R;
|
|
|
import com.zhilin.patrol.bean.HomeEntity;
|
|
|
import com.zhilin.patrol.bean.LoginEntity;
|
| xqd
@@ -63,8 +70,100 @@ public class RectifyMainActivity extends AppCompatActivity {
|
|
|
mInfoListBean = (LoginEntity.InfoListBean) getIntent().getSerializableExtra("infoList");
|
|
|
mUserInfo = mInfoListBean.getUserInfo();
|
|
|
init();
|
|
|
+ checkUpdate();
|
|
|
}
|
|
|
|
|
|
+ public void checkUpdate(){
|
|
|
+ /** 新版本 **/
|
|
|
+ new PgyUpdateManager.Builder()
|
|
|
+ .setForced(true) //设置是否强制提示更新,非自定义回调更新接口此方法有用
|
|
|
+ .setUserCanRetry(false) //失败后是否提示重新下载,非自定义下载 apk 回调此方法有用
|
|
|
+ .setDeleteHistroyApk(false) // 检查更新前是否删除本地历史 Apk, 默认为true
|
|
|
+ .setUpdateManagerListener(new UpdateManagerListener() {
|
|
|
+ @Override
|
|
|
+ public void onNoUpdateAvailable() {
|
|
|
+ //没有更新是回调此方法
|
|
|
+ Log.d("pgyer", "there is no new version");
|
|
|
+ }
|
|
|
+ @Override
|
|
|
+ public void onUpdateAvailable(final AppBean appBean) {
|
|
|
+ //有更新回调此方法
|
|
|
+ Log.d("pgyer", "there is new version can update"
|
|
|
+ + "new versionCode is " + appBean.getVersionCode());
|
|
|
+ //调用以下方法,DownloadFileListener 才有效;
|
|
|
+ //如果完全使用自己的下载方法,不需要设置DownloadFileListener
|
|
|
+ int a=2;
|
|
|
+ new AlertDialog(RectifyMainActivity.this).builder()
|
|
|
+ .setTitle(getString(R.string.tv_tishi))
|
|
|
+ .setMsg("有新版本,是否更新?")
|
|
|
+ .setPositiveButton("确定", new View.OnClickListener() {
|
|
|
+ @Override
|
|
|
+ public void onClick(View v) {
|
|
|
+ PgyUpdateManager.downLoadApk(appBean.getDownloadURL());
|
|
|
+ }
|
|
|
+ }).setNegativeButton("取消", new View.OnClickListener() {
|
|
|
+ @Override
|
|
|
+ public void onClick(View view) {
|
|
|
+
|
|
|
+ }
|
|
|
+ }).show();
|
|
|
+
|
|
|
+
|
|
|
+ }
|
|
|
+ @Override
|
|
|
+ public void checkUpdateFailed(Exception e) {
|
|
|
+ //更新检测失败回调
|
|
|
+ //更新拒绝(应用被下架,过期,不在安装有效期,下载次数用尽)以及无网络情况会调用此接口
|
|
|
+ Log.e("pgyer", "check update failed ", e);
|
|
|
+ }
|
|
|
+ })
|
|
|
+ //注意 :
|
|
|
+ //下载方法调用 PgyUpdateManager.downLoadApk(appBean.getDownloadURL()); 此回调才有效
|
|
|
+ //此方法是方便用户自己实现下载进度和状态的 UI 提供的回调
|
|
|
+ //想要使用蒲公英的默认下载进度的UI则不设置此方法
|
|
|
+ .setDownloadFileListener(new DownloadFileListener() {
|
|
|
+ @Override
|
|
|
+ public void downloadFailed() {
|
|
|
+ //下载失败
|
|
|
+ Log.e("pgyer", "download apk failed");
|
|
|
+ }
|
|
|
+ @Override
|
|
|
+ public void downloadSuccessful(File file) {
|
|
|
+ // ***/storage/emulated/0/Android/data/com.zhilin.patrol/files/pgySdk/downloadApk/apk-3628010012122729412.apk
|
|
|
+ Log.e("pgyer", "download apk success");
|
|
|
+ // 使用蒲公英提供的安装方法提示用户 安装apk
|
|
|
+ Log.i("installfile=","***"+file);
|
|
|
+// PgyUpdateManager.installApk(file);
|
|
|
+// installAPK(file);
|
|
|
+ startInstall(RectifyMainActivity.this,file);
|
|
|
+// installAPKtow(MainActivity.this,file);
|
|
|
+ }
|
|
|
+ @Override
|
|
|
+ public void onProgressUpdate(Integer... integers) {
|
|
|
+ Log.e("pgyer", "update download apk progress" + integers);
|
|
|
+ }})
|
|
|
+ .register();
|
|
|
+ }
|
|
|
+ public void startInstall(Context context, File file) {
|
|
|
+// if (Build.VERSION.SDK_INT >= 29) {
|
|
|
+ Intent intent = new Intent(Intent.ACTION_VIEW);
|
|
|
+ intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
|
|
|
+ Uri apkUri = FileProvider.getUriForFile(context, "com.zhilin.patrol.fileProvider", file);
|
|
|
+ intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
|
|
|
+ intent.setDataAndType(apkUri, "application/vnd.android.package-archive");
|
|
|
+ try {
|
|
|
+ context.startActivity(intent);
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+// } else {
|
|
|
+// Uri uri = Uri.fromFile(file);
|
|
|
+// Intent intent = new Intent(Intent.ACTION_VIEW);
|
|
|
+// intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
|
|
|
+// intent.setDataAndType(uri, "application/vnd.android.package-archive");
|
|
|
+// startActivity(intent);
|
|
|
+// }
|
|
|
+ }
|
|
|
private void init() {
|
|
|
if (mUserInfo != null) {
|
|
|
mUserTv.setText(mUserInfo.getUserName());
|