Android-下载更新方法记录

今天重新更改了公司项目下载更新的办法。感觉功能和界面还算可以,特此记录下来。

第一步:画更新的界面

无图说屌,先上效果图,我是从androidstudioUI截图过来了,效果如下。中间留白写更新特新或者固定内容,两个按钮留白为取消和更新。请自己添加文本内容。

弹框样式.png

布局代码如下:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_gravity="center_horizontal"
    android:orientation="vertical" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_marginLeft="20dp"
        android:layout_marginRight="20dp"
        android:background="@drawable/dialog_bg"
        android:orientation="vertical"
        android:paddingLeft="10dp"
        android:paddingRight="10dp" >

        <LinearLayout
            android:id="@+id/ll_title"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:visibility="gone" >

            <TextView
                android:id="@+id/tv_title"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:background="#FFFFFF"
                android:gravity="center"
                android:padding="20dp"
                android:textColor="#3cac17"
                android:textSize="18sp" />

            <View
                android:layout_width="match_parent"
                android:layout_height="1dp"
                android:background="#773cac17" />
        </LinearLayout>

        <TextView
            android:id="@+id/tv_content"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="#FFFFFF"
            android:paddingBottom="20dp"
            android:paddingLeft="10dp"
            android:paddingRight="10dp"
            android:paddingTop="20dp"
            android:textColor="#353535"
            android:textSize="16sp" 
            android:maxLines="6"
            android:scrollbars="none"/>
             <LinearLayout
                android:id="@+id/ll_content"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="horizontal" >

                <TextView
                    android:id="@+id/tv_cancle"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center"
                    android:layout_margin="10dp"
                    android:layout_weight="1"
                    android:background="@drawable/btn_gem_green"
                    android:gravity="center"
                    android:textColor="#FFFFFF"
                    android:padding="10dp"
                    android:textSize="16sp" />

                <TextView
                    android:id="@+id/tv_sure"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center"
                    android:layout_margin="10dp"
                    android:layout_weight="1"
                    android:background="@drawable/btn_gem_green"
                    android:gravity="center"
                    android:textColor="#FFFFFF"
                     android:padding="10dp"
                    android:textSize="16sp" />
            </LinearLayout>
    </LinearLayout>
</LinearLayout>

再布局中编写了两个drawable属性btn_gem_green和dialog_bg
dialog_bg代码如下:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
   <corners android:radius="8dip"/>
   <solid android:color="#FFFFFF"/>
   <stroke android:color="#00C5A3" android:width="1dip"/>
</shape>

btn_gem_green代码如下:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <!-- 点击之后 -->
    <item android:state_pressed="true"><layer-list>
            <item android:left="0dip" android:top="0dip"><shape>
                    <solid android:color="#2BBE6A" />

                    <corners android:radius="4dip" />
                </shape></item>
        </layer-list></item>
    <!-- 正常状态 -->
    <item><layer-list>

            <!-- 第一层 -->
            <item android:left="0dip" android:top="0dip"><shape>
                    <solid android:color="#00C5A3" />

                    <corners android:radius="4dip" />
                    <!-- 描边 -->
                    <stroke android:width="1dip" android:color="#ffffffff" />
                </shape></item>
        </layer-list></item>

</selector>

第二步:开始写弹窗代码

该处代码业务是检测后弹出窗口,三个参数分别为上下文,弹窗标题,和弹窗更新内容,因里面有别的代码内容,没有删除掉,复制后如果有找不到的,请删除那些找不到的没用代码

 public static void openDownDialog(final Context context, String title, String msg) {
        dialog = new AlertDialog.Builder(context).create();
        dialog.setCancelable(false);
        dialog.show();

        Window window = dialog.getWindow();
        window.setContentView(R.layout.prompt_alertdialog);
        LinearLayout ll_title = (LinearLayout) window
                .findViewById(R.id.ll_title);
        ll_title.setVisibility(View.VISIBLE);
        TextView tv_title = (TextView) window.findViewById(R.id.tv_title);
        getInstance().pb_download = (NumberProgressBar) window
                .findViewById(R.id.pb_splash_download);
        getInstance().pb_download.setOnProgressBarListener(getInstance());
        tv_title.setText(title);
        TextView tv_content = (TextView) window.findViewById(R.id.tv_content);
        tv_content.setMovementMethod(new ScrollingMovementMethod());
        tv_content
                .setText(msg);
        final TextView tv_sure = (TextView) window.findViewById(R.id.tv_sure);
        final TextView tv_cancle = (TextView) window
                .findViewById(R.id.tv_cancle);
        tv_cancle.setText("取消");
        tv_cancle.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View arg0) {
                dialog.cancel();
                //关闭程序
                System.exit(0);

            }
        });
        tv_sure.setText("更新");
        tv_sure.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                tv_cancle.setEnabled(false);
                tv_sure.setEnabled(false);
                dialog.cancel();
                Intent intent = new Intent(context, DownloadActivity.class);
                intent.putExtra("url", apkurl);
                intent.putExtra("apkversion", apkversion);
                context.startActivity(intent);
            }
        });
    }
因为公司项目更新页面代码原因,无法在本页面进行下载操作,所以当点击更新的时候,我是进行了一个新的activity页面跳转做的这个操作。

第三步:开始写下载的代码

下载使用的是xutlis的下载方法。如果我这个写的个人看的不清不楚的,可自行查看xutlis的文档,官方的总比个人更加详细嘛。。。
代码没必要完全复制,主要还是提供一个思路。实现代码都已经注释了,希望能帮助到你们。

 /**
     * 下载apk
     *
     * @param url
     * @param strFile
     */
    public void downLoadNewApk(final String url, final String strFile) {


        if (!fileIsExists(strPath + strFile)) {

            File file = new File(strPath);
             utils.download(getIntent().getStringExtra("url"), file.getPath() + "/XXXXX" + getIntent().getStringExtra("apkversion") + ".apk", new RequestCallBack<File>() {

                @Override
                public void onStart() {
                    super.onStart();
                    ToastUtil.show(DownloadActivity.this, "正在下载...");
                }

                @Override
                public void onSuccess(ResponseInfo<File> responseInfo) {
                    ToastUtil.show(DownloadActivity.this, "下载成功");
                    //打开文件
                    Intent intent = DownloadActivity.getApkFileIntent(strPath + strFile);
                    DownloadAPK.mActivity.startActivity(intent);
                    finish();
                }

                @Override
                public void onFailure(HttpException e, String s) {
                    ToastUtil.show(DownloadActivity.this, "下载失败");
                  
                }

                @Override
                public void onLoading(long total, long current, boolean isUploading) {
                    pb_download.setVisibility(View.VISIBLE);// 设置进度的显示
                    int max = (int) total;
                    int progress = (int) current;
                    pb_download.setMax(max);// 设置进度条的最大值
                    pb_download.setProgress(progress);// 设置当前进度
                    super.onLoading(total, current, isUploading);
                }
            });
        } else {
            ToastUtil.show(DownloadActivity.this, "与服务器连接失败!");
        }

    }
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 175,631评论 25 709
  • ¥开启¥ 【iAPP实现进入界面执行逐一显】 〖2017-08-25 15:22:14〗 《//首先开一个线程,因...
    小菜c阅读 11,763评论 0 17
  • 发现 关注 消息 iOS 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 14,222评论 4 61
  • 在Linux下,通过一个命令就可以把整个站相关的文件全部下载下来。 参数说明: -r : 递归下载 -p ...
    devilyaos阅读 12,071评论 0 1
  • 今天老师点评了我的“数枣”练习,指出我的问题所在:声音只从口腔里发出,腹腔没有共鸣。我恍然大悟,谢谢老师指点迷津。
    修木成音阅读 1,241评论 0 0