Android版本更新下载apk自动安装的方法

程序中XXXX部分和//TODO部分根据具体项目进行修改和完善。

public class UpdateHelper {
    private static final String TAG = "UpdateHelper";
    private static final String FILE_PROVIDER_AUTHORITY = "XXXX";
    private static final String DATA_AND_TYPE = "application/vnd.android.package-archive";
    private static final String UPDATE_URL = "XXXX";

    public static boolean checkForUpdated(Context context) {
        boolean updated = false;
        try {
            int currentVersion = context.getPackageManager().getPackageInfo(context.getPackageName(), 0).versionCode;
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
       //TODO 获取服务端最新版本号,与currentVersion做比较
        return updated;
    }

    public static void autoInstallUpdate(final Context context) {
        OkGo.<File>get(UPDATE_URL).tag(context).execute(new FileCallback() {
            @Override
            public void onSuccess(Response<File> response) {
                File file = response.body();
                setPermission(file.getPath());
                installApk(context, file.getPath());
            }

            @Override
            public void onError(Response<File> response) {
                super.onError(response);
                //TODO 网络请求出错
            }

            @Override
            public void onFinish() {
                super.onFinish();
                //TODO 网络请求结束
            }

            @Override
            public void downloadProgress(Progress progress) {
                super.downloadProgress(progress);
                int fraction = (int) (progress.fraction * 100);
                //TODO 下载中,显示下载进度
            }
        });
    }

    private static void installApk(Context context, String apkPath) {
        Intent intent = new Intent(Intent.ACTION_VIEW);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        File file = new File(apkPath);
        if (Build.VERSION.SDK_INT > Build.VERSION_CODES.N) {
            Uri apkUri = FileProvider.getUriForFile(context, FILE_PROVIDER_AUTHORITY, file);
            intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
            intent.setDataAndType(apkUri, DATA_AND_TYPE);
        } else {
            Uri apkUri = Uri.fromFile(file);
            intent.setDataAndType(apkUri, DATA_AND_TYPE);
        }
        context.startActivity(intent);
    }

    private static void setPermission(String filePath) {
        String command = "chmod " + "777" + " " + filePath;
        Runtime runtime = Runtime.getRuntime();
        try {
            runtime.exec(command);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 173,264评论 25 708
  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 134,923评论 18 139
  • 幸福路人春风20180222第263天 大千世界,不会有两片纹理相同的树叶,也不会有两段完全一样的人生。你的人生自...
    春风7861阅读 190评论 0 0
  • 文/麻老将 “刚子,回来吃饭,”听见奶奶一如往常平和的嗓音,我抖抖在石墩上蹲久了有些发麻的腿,走进了这座无时无刻不...
    朝外阅读 335评论 0 1
  • 很多人都会在几年后重逢的时候说上一句“你的变化真大啊!”。但是谁又能说自己没有变过呢?那么人为什么要改变呢,从瓜瓜...
    我的梦想是吃花生阅读 149评论 0 1