android 更新下载apk

更新类保存

目前采用的是后台下载apk包,通知栏弹出提示并实时更新进度条的方式。最近似乎流行在首页直接下载安装,后台下载方式本意是让用户能在下载的时候去做其他操作的,但是如果我在使用的app马上要更新一个新包了,我实际上也不会去执行什么操作,比如微信更新包的时候,我不会在下载新包的间隙去聊天,因为马上就会弹出那个安装界面了,所以后台下载通知栏通知的更新方式其实并不是用户最希望的。相反,在点击下载更新按钮后,直接在当前页面呈现出下载进度,让用户能精确的知道更新进度,同时提供一个后台下载按钮,让用户在网络情况不佳的情况下有可以在后台下载的选择,这种体验应该是比较好的。

 

代码有些部分使用了自定义工具库,看代码应该就知道作用.

/**
 * 更新类
 * Created by sy on 2015/8/5.
 */
public class UpdataUtil {

    public static int lastProgress = 0;
    public final static int updateId = 88888;

    public static void updateApk(final Context context, String url) {

        StaticMethod.debugEMSG(StaticMethod.getApkPath());
        //检测当前是否在下载更新,防止多次重复下载
        if (!(Boolean) SPUtil.get(context, "isUpdate", false)) {
            //锁定下载更新
            SPUtil.put(context, "isUpdate", true);
            //发送通知
            final NotificationManager notificationManager = (NotificationManager) context.getSystemService(context.NOTIFICATION_SERVICE);
            final Notification notification = new Notification(R.drawable.push, "正在下载", System.currentTimeMillis());
            notification.contentView = new RemoteViews(context.getPackageName(), R.layout.update_notification_layout);
            notification.flags = Notification.FLAG_ONGOING_EVENT;
            notification.contentView.setTextViewText(R.id.update_title, context.getResources().getString(R.string.app_name) + "正在更新");
            notificationManager.notify(updateId, notification);

            //开始下载
            HttpUtil.mHttpClient.get(url, new BinaryHttpResponseHandler() {

                @Override
                public void onProgress(long bytesWritten, long totalSize) {
                    super.onProgress(bytesWritten, totalSize);
                    int nowProgress = (int) ((bytesWritten / (double) totalSize) * 100);
                    if (nowProgress != lastProgress) {
                        notification.contentView.setTextViewText(R.id.update_tv, nowProgress + "%");
                        notification.contentView.setProgressBar(R.id.update_pb, 100, nowProgress, false);
                        notificationManager.notify(updateId, notification);
                    }
                    lastProgress = nowProgress;
                }

                @Override
                public void onSuccess(int statusCode, cz.msebera.android.httpclient.Header[] headers, byte[] binaryData) {
                    StaticMethod.debugEMSG("下载成功");
                    notificationManager.cancel(updateId);
                    //解锁下载更新
                    SPUtil.put(context, "isUpdate", false);

                    //把数据存到sd上
                    File file = new File(StaticMethod.getApkPath());
                    FileOutputStream fos = null;
                    try {
                        fos = new FileOutputStream(file);
                        fos.write(binaryData, 0, binaryData.length);
                        fos.close();
                    } catch (FileNotFoundException e) {
                        e.printStackTrace();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }

                    //开始安装
                    Intent intent = new Intent(Intent.ACTION_VIEW);
                    intent.setDataAndType(Uri.fromFile(file),
                            "application/vnd.android.package-archive");
                    context.startActivity(intent);
                }

                @Override
                public void onFailure(int statusCode, cz.msebera.android.httpclient.Header[] headers, byte[] binaryData, Throwable error) {
                    StaticMethod.showMSG(context, "更新失败,错误代码:" + statusCode);
                    SPUtil.put(context, "isUpdate", false);
                }
            });
        } else {
            StaticMethod.showMSG(context, "正在下载更新包,请稍后");
        }
    }
}

  

posted on 2016-01-28 14:34  Sun_Yang_  阅读(1679)  评论(0编辑  收藏  举报

导航