|
@@ -8,6 +8,7 @@ import android.content.pm.PackageManager;
|
|
import android.net.Uri;
|
|
import android.net.Uri;
|
|
import android.os.Build;
|
|
import android.os.Build;
|
|
import android.os.Environment;
|
|
import android.os.Environment;
|
|
|
|
+import android.support.v4.content.FileProvider;
|
|
import android.util.Log;
|
|
import android.util.Log;
|
|
|
|
|
|
import java.io.BufferedReader;
|
|
import java.io.BufferedReader;
|
|
@@ -29,7 +30,10 @@ public class AppUpdateHelper {
|
|
*/
|
|
*/
|
|
public static final String FILE_APK_NAME = "CallingBed2APK.apk";
|
|
public static final String FILE_APK_NAME = "CallingBed2APK.apk";
|
|
|
|
|
|
|
|
+ public static Context mContext;
|
|
|
|
+
|
|
public static void updateApp(Context context, UpdateCallBack callBack) {
|
|
public static void updateApp(Context context, UpdateCallBack callBack) {
|
|
|
|
+ mContext = context;
|
|
if (checkApkExit(context)) {
|
|
if (checkApkExit(context)) {
|
|
Log.d(TAG, "文件存在");
|
|
Log.d(TAG, "文件存在");
|
|
} else {
|
|
} else {
|
|
@@ -49,17 +53,17 @@ public class AppUpdateHelper {
|
|
|
|
|
|
|
|
|
|
//if (installApp(context.getPackageName(), path)) {
|
|
//if (installApp(context.getPackageName(), path)) {
|
|
- if (rootSilenceInstall(path)) {
|
|
|
|
- Log.d(TAG, "安装成功");
|
|
|
|
- if (callBack != null) {
|
|
|
|
- callBack.onSuccess();
|
|
|
|
- }
|
|
|
|
- } else {
|
|
|
|
- Log.d(TAG, "安装失败");
|
|
|
|
- if (callBack != null) {
|
|
|
|
- callBack.onFailed();
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
|
|
+// if (rootSilenceInstall(path)) {
|
|
|
|
+// Log.d(TAG, "安装成功");
|
|
|
|
+// if (callBack != null) {
|
|
|
|
+// callBack.onSuccess();
|
|
|
|
+// }
|
|
|
|
+// } else {
|
|
|
|
+// Log.d(TAG, "安装失败");
|
|
|
|
+// if (callBack != null) {
|
|
|
|
+// callBack.onFailed();
|
|
|
|
+// }
|
|
|
|
+// }
|
|
|
|
|
|
// if (silentInstall(context, path)) {
|
|
// if (silentInstall(context, path)) {
|
|
// Log.d(TAG, "app 安装成功");
|
|
// Log.d(TAG, "app 安装成功");
|
|
@@ -67,8 +71,38 @@ public class AppUpdateHelper {
|
|
// callBack.onSuccess();
|
|
// callBack.onSuccess();
|
|
// }
|
|
// }
|
|
// }
|
|
// }
|
|
|
|
+
|
|
|
|
+ if (installAPP(path)) {
|
|
|
|
+ Log.d(TAG, "安装成功");
|
|
|
|
+ if (callBack != null) {
|
|
|
|
+ callBack.onSuccess();
|
|
|
|
+ }
|
|
|
|
+ }
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+
|
|
|
|
+ public static boolean installAPP(String path) {
|
|
|
|
+
|
|
|
|
+ Intent intent = new Intent(Intent.ACTION_VIEW);
|
|
|
|
+ File apkFile = new File(path);
|
|
|
|
+ // 由于没有在Activity环境下启动Activity,设置下面的标签
|
|
|
|
+ intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
|
|
|
|
+ //Android 7.0以上要使用FileProvider
|
|
|
|
+ if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
|
|
|
|
+ intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
|
|
|
|
+ //参数1 上下文, 参数2 Provider主机地址 和配置文件中保持一致 参数3 共享的文件
|
|
|
|
+ Uri uri = FileProvider.getUriForFile(mContext, mContext.getPackageName() + ".fileprovider", apkFile);
|
|
|
|
+ //添加这一句表示对目标应用临时授权该Uri所代表的文件
|
|
|
|
+// intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
|
|
|
|
+ intent.setDataAndType(uri, "application/vnd.android.package-archive");
|
|
|
|
+ } else {
|
|
|
|
+ intent.setDataAndType(Uri.fromFile(apkFile), "application/vnd.android.package-archive");
|
|
|
|
+ }
|
|
|
|
+ mContext.startActivity(intent);
|
|
|
|
+ return true;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+
|
|
private static boolean checkApkExit(Context context) {
|
|
private static boolean checkApkExit(Context context) {
|
|
File file = new File(FILE_APK_PATH + "/" + FILE_APK_NAME);
|
|
File file = new File(FILE_APK_PATH + "/" + FILE_APK_NAME);
|
|
return file.exists();
|
|
return file.exists();
|
|
@@ -189,6 +223,7 @@ public class AppUpdateHelper {
|
|
|
|
|
|
public interface UpdateCallBack {
|
|
public interface UpdateCallBack {
|
|
void onFailed();
|
|
void onFailed();
|
|
|
|
+
|
|
void onSuccess();
|
|
void onSuccess();
|
|
}
|
|
}
|
|
}
|
|
}
|