Bladeren bron

<适配rk3368 8.1升级程序>

weizhengliang 4 jaren geleden
bovenliggende
commit
d323d3b657

+ 15 - 4
app/src/main/java/com/wdkl/callingmainnurse/ui/activity/APPUpdateActivity.java

@@ -1,6 +1,7 @@
 package com.wdkl.callingmainnurse.ui.activity;
 package com.wdkl.callingmainnurse.ui.activity;
 
 
 import android.annotation.SuppressLint;
 import android.annotation.SuppressLint;
+import android.os.Build;
 import android.os.Handler;
 import android.os.Handler;
 import android.os.Message;
 import android.os.Message;
 import android.widget.TextView;
 import android.widget.TextView;
@@ -96,9 +97,19 @@ public class APPUpdateActivity extends BaseActivity implements SilentUpdateUtil.
                 case APP_UPDATE_SUCSSED:
                 case APP_UPDATE_SUCSSED:
                     text_download_infomation.setText("程序正在安装中请勿断电!");
                     text_download_infomation.setText("程序正在安装中请勿断电!");
                     //升级
                     //升级
-                    SilentUpdateUtil silentUpdateUtil = new SilentUpdateUtil();
-                    silentUpdateUtil.setInstallCallBack(APPUpdateActivity.this);
-                    silentUpdateUtil.updateApk(APPUpdateActivity.this);//静默升级
+                    if (Build.VERSION.SDK_INT >= 26) {
+                        //rk3368 8.1
+                        DownloadUtils downloadUtils = new DownloadUtils(APPUpdateActivity.this);
+                        if (downloadUtils.silentInstall(DownloadUtil.FILE_APK_PATH + "/" + DownloadUtil.FILE_APK_NAME)) {
+                            ToastUtil.showToast("升级成功,即将重启!");
+                        }
+                        APPUpdateActivity.this.finish();
+                    } else {
+                        //rk3128 7.1
+                        SilentUpdateUtil silentUpdateUtil = new SilentUpdateUtil();
+                        silentUpdateUtil.setInstallCallBack(APPUpdateActivity.this);
+                        silentUpdateUtil.updateApk(APPUpdateActivity.this);//静默升级
+                    }
                     //new SilentUpdateUtil().updateApk(APPUpdateActivity.this).setInstallCallBack(APPUpdateActivity.this);//静默升级
                     //new SilentUpdateUtil().updateApk(APPUpdateActivity.this).setInstallCallBack(APPUpdateActivity.this);//静默升级
                     //new DownloadUtils(APPUpdateActivity.this).installAPK(DownloadUtil.FILE_APK_PATH + "/" + DownloadUtil.FILE_APK_NAME); //智能升级
                     //new DownloadUtils(APPUpdateActivity.this).installAPK(DownloadUtil.FILE_APK_PATH + "/" + DownloadUtil.FILE_APK_NAME); //智能升级
                     break;
                     break;
@@ -123,7 +134,7 @@ public class APPUpdateActivity extends BaseActivity implements SilentUpdateUtil.
     public void installInfomation(int type, String infomation) {
     public void installInfomation(int type, String infomation) {
         text_download_infomation.setText(infomation);
         text_download_infomation.setText(infomation);
         if (type == SilentUpdateUtil.INSTALL_SUCSSED) {
         if (type == SilentUpdateUtil.INSTALL_SUCSSED) {
-
+            ToastUtil.showToast("升级成功!");
         } else if (type == SilentUpdateUtil.INSTALL_FILED) {
         } else if (type == SilentUpdateUtil.INSTALL_FILED) {
             ToastUtil.showToast("升级失败!");
             ToastUtil.showToast("升级失败!");
             APPUpdateActivity.this.finish();
             APPUpdateActivity.this.finish();

+ 27 - 0
app/src/main/java/com/wdkl/callingmainnurse/util/DownloadUtils.java

@@ -5,6 +5,7 @@ import android.content.BroadcastReceiver;
 import android.content.Context;
 import android.content.Context;
 import android.content.Intent;
 import android.content.Intent;
 import android.content.IntentFilter;
 import android.content.IntentFilter;
+import android.content.pm.PackageManager;
 import android.database.Cursor;
 import android.database.Cursor;
 import android.net.Uri;
 import android.net.Uri;
 import android.os.Build;
 import android.os.Build;
@@ -15,6 +16,8 @@ import android.widget.Toast;
 
 
 import java.io.File;
 import java.io.File;
 import java.io.IOException;
 import java.io.IOException;
+import java.lang.reflect.Constructor;
+import java.lang.reflect.Method;
 
 
 /**
 /**
  * 类名称:DownloadUtil <br>
  * 类名称:DownloadUtil <br>
@@ -133,6 +136,30 @@ public class DownloadUtils {
 
 
     }
     }
 
 
+    public boolean silentInstall(String apkPath) {
+        PackageManager packageManager = mContext.getPackageManager();
+        Class pmClz = packageManager.getClass();
+        try {
+            if (Build.VERSION.SDK_INT >= 21) {
+                Class aClass = Class.forName("android.app.PackageInstallObserver");
+                Constructor constructor = aClass.getDeclaredConstructor();
+                constructor.setAccessible(true);
+                Object installObserver = constructor.newInstance();
+                Method method = pmClz.getDeclaredMethod("installPackage", Uri.class, aClass, int.class, String.class);
+                method.setAccessible(true);
+                method.invoke(packageManager, Uri.fromFile(new File(apkPath)), installObserver, 2, null);
+            } else {
+                Method method = pmClz.getDeclaredMethod("installPackage", Uri.class, Class.forName("android.content.pm.IPackageInstallObserver"), int.class, String.class);
+                method.setAccessible(true);
+                method.invoke(packageManager, Uri.fromFile(new File(apkPath)), null, 2, null);
+            }
+            return true;
+        } catch (Exception e) {
+            LogUtil.e("install", e.toString());
+        }
+        return false;
+    }
+
     //修改文件权限
     //修改文件权限
     public void setPermission(String absolutePath) {
     public void setPermission(String absolutePath) {
         String command = "chmod " + "777" + " " + absolutePath;
         String command = "chmod " + "777" + " " + absolutePath;