版本更新工具类

public class VersionUpdatingUtil {

    private static final int TIMEOUT = 10 * 1000;
    public static File updateDir = null;
    public static File updateFile = null;

    /***
     * 下载文件
     *
     * @return
     * @throws MalformedURLException
     */
    public void downloadUpdateFile(String down_url, String file)
            throws Exception {
        int down_step = 5;// 提示step
        int totalSize;
        int downloadCount = 0;// 已经下载好的大小
        InputStream inputStream;
        OutputStream outputStream;

        URL url = new URL(down_url);
        HttpURLConnection httpURLConnection = (HttpURLConnection) url
                .openConnection();
        httpURLConnection.setConnectTimeout(TIMEOUT);
        httpURLConnection.setReadTimeout(TIMEOUT);
        // 获取下载文件的size
        totalSize = httpURLConnection.getContentLength();
        if (httpURLConnection.getResponseCode() == 404) {
            throw new Exception("fail!");
        }
        inputStream = httpURLConnection.getInputStream();
        outputStream = new FileOutputStream(file, false);// 文件存在则覆盖掉
        byte buffer[] = new byte[1024];
        int readsize = 0;
        while ((readsize = inputStream.read(buffer)) != -1) {
            outputStream.write(buffer, 0, readsize);
            totalSize += readsize;// 时时获取下载到的大小

            /**
             * 没
             */
            if (downloadCount == 0
                    || (totalSize * 100 / totalSize - down_step) > downloadCount) {

            }
        }
    }

    /***
     * 创建文件
     */
    public static void createFile(String name) {
        if (android.os.Environment.MEDIA_MOUNTED.equals(android.os.Environment
                .getExternalStorageState())) {
            updateDir = new File(Environment.getExternalStorageDirectory()
                    + "/" + App.downloadDir);
            updateFile = new File(updateDir + "/" + name + ".apk");

            if (!updateDir.exists()) {
                updateDir.mkdirs();
            }
            if (!updateFile.exists()) {
                try {
                    updateFile.createNewFile();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

        }
    }

    /***
     * 检查是否更新版本
     */
    public static void checkVersion(final Context context) {
        if (!App.versionName.equals(MainActivity.version_Name)) {
            // 发现新版本,提示用户更新
            AlertDialog.Builder alert = new AlertDialog.Builder(context);
            alert.setTitle("软件升级")
                    .setMessage(MainActivity.description)
                    .setPositiveButton("更新",
                            new DialogInterface.OnClickListener() {
                                public void onClick(DialogInterface dialog,
                                        int which) {
                                    // 开启更新服务UpdateService
                                    // 这里为了把update更好模块化,可以传一些updateService依赖的值
                                    // 如布局ID,资源ID,动态获取的标题,这里以app_name为例
                                    Intent updateIntent = new Intent(context,
                                            UpdateService.class);
                                    updateIntent.putExtra(
                                            "app_name",
                                            context.getResources().getString(
                                                    R.string.app_name));
                                    context.startService(updateIntent);
                                }
                            })
                    .setNegativeButton("取消",
                            new DialogInterface.OnClickListener() {
                                public void onClick(DialogInterface dialog,
                                        int which) {
                                    dialog.dismiss();
                                }
                            });
            alert.create().show();

        } else {
            Toast.makeText(context, "已是最新版本", Toast.LENGTH_LONG).show();
        }
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值