|
@@ -5,6 +5,7 @@ import android.content.BroadcastReceiver;
|
|
|
import android.content.Context;
|
|
|
import android.content.Intent;
|
|
|
import android.content.IntentFilter;
|
|
|
+import android.content.pm.PackageManager;
|
|
|
import android.database.Cursor;
|
|
|
import android.net.Uri;
|
|
|
import android.os.Build;
|
|
@@ -15,6 +16,8 @@ import android.widget.Toast;
|
|
|
|
|
|
import java.io.File;
|
|
|
import java.io.IOException;
|
|
|
+import java.lang.reflect.Constructor;
|
|
|
+import java.lang.reflect.Method;
|
|
|
|
|
|
/**
|
|
|
* 类名称:DownloadUtil <br>
|
|
@@ -186,4 +189,29 @@ public class DownloadUtils {
|
|
|
*/
|
|
|
void onDownloadFailed();
|
|
|
}
|
|
|
+
|
|
|
+
|
|
|
+ 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;
|
|
|
+ }
|
|
|
}
|