Android基础#22:Android handler用法详解2,例2

本文介绍了如何在Android应用中实现应用升级提示,并通过对话框引导用户进行升级操作。包括创建对话框、处理用户选择、下载升级包以及更新应用的过程。重点展示了使用Handler和线程进行异步下载的方法。

内容简介:

上一篇讲解了Handler的基本原理和用法,本篇再给出一个实例。

 

直接看代码:

 

public class MyAboutActivity extends Activity {

    public ProgressDialog mProgressDialog;
    private Handler mHandler = new Handler();

    @Override


    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.my_about);
        Dialog dialog = new AlertDialog.Builder(MyAboutActivity.this)
                .setTitle("应用升级").
                        setMessage("发现新版本,请升级!")
                .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog,
                                        int which) {
                        mProgressDialog = new ProgressDialog(MyAboutActivity.this);
                        mProgressDialog.setTitle("正在升级中");
                        mProgressDialog.setMessage("请稍候...");
                        mProgressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
                        downFile("http://localhost:8080/test/apk/test1.apk");
                    }
                }).setNegativeButton("取消", 
                        new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog,
                                                int whichButton) {
                                //do your something
                            }
                        }).create();
        dialog.show();
    }

    private void downFile(final String url) {
        mProgressDialog.show();
        new Thread() {
            public void run() {
                HttpClient client = new DefaultHttpClient();
                //代表连接的url
                HttpGet get = new HttpGet(url);
                HttpResponse response;
                try {
                    response = client.execute(get);
                    HttpEntity entity = response.getEntity();
                    long length = entity.getContentLength();
                    InputStream is = entity.getContent();
                    FileOutputStream fileOutputStream = null;
                    if (is != null) {
                        File file = new File(Environment.getExternalStorageDirectory(), "test1.apk");
                        fileOutputStream = new FileOutputStream(file);
                        byte[] buf = new byte[1024];
                        int ch = -1;
                        int count = 0;
                        while ((ch = is.read(buf)) != -1) {
                            fileOutputStream.write(buf, 0, ch);
                            count += ch;
                            if (length > 0) {
                            }
                        }
                    }
                    fileOutputStream.flush();
                    if (fileOutputStream != null) {
                        fileOutputStream.close();
                    }
                    downLoadApk();
                } catch (ClientProtocolException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }.start();
    }

    private void downLoadApk() {
        mHandler.post(new Runnable() {
            public void run() {
                mProgressDialog.cancel();
                updateApp();
            }
        });
    }

    private void updateApp() {
        Intent intent = new Intent(Intent.ACTION_VIEW);
        intent.setDataAndType(Uri.fromFile(new File("/sdcard/test1.apk")), "application/vnd.android.package-archive");
        startActivity(intent);
    }

}

说明:

调用流程: 在onCreate, 创建dialog,如果用户点击“确定”,就调用到downFile,然后,downFile ->downLoadApk ->mHandler.post。


 

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

liranke

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值