版本更新
2016-07-27 15:38
260 查看
<span style="background-color: rgb(255, 0, 0);"> ///MainActivity中</span> package test.bwie.com.gaoxuge20160701; import android.app.AlertDialog; import android.app.DownloadManager; import android.app.ProgressDialog; import android.content.DialogInterface; import android.content.Intent; import android.net.Uri; import android.os.Environment; import android.os.Handler; import android.os.Message; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.util.Log; import android.view.View; import android.widget.Button; import android.widget.ListView; import android.widget.Toast; import org.apache.http.HttpEntity; import org.apache.http.HttpResponse; import org.apache.http.client.ClientProtocolException; import org.apache.http.client.HttpClient; import org.apache.http.client.methods.HttpGet; import org.apache.http.impl.client.DefaultHttpClient; import java.io.ByteArrayOutputStream; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.net.HttpURLConnection; import java.net.MalformedURLException; import java.net.URL; import java.util.List; import test.bwie.com.gaoxuge20160701.adapter.MyAdapter; import test.bwie.com.gaoxuge20160701.bean.NewsBean; import test.bwie.com.gaoxuge20160701.bean.VersionBean; import test.bwie.com.gaoxuge20160701.utils.NetWorkUtils; import test.bwie.com.gaoxuge20160701.utils.PullUtils; import test.bwie.com.gaoxuge20160701.utils.VerSionUtils; public class MainActivity extends AppCompatActivity { private Button newcode; private ListView lv; private Handler handler = new Handler() { @Override public void handleMessage(Message msg) { super.handleMessage(msg); // Toast.makeText(MainActivity.this, "====="+list.toString(), Toast.LENGTH_SHORT).show(); switch (msg.what) { case 0: //给listview设置适配器 MyAdapter adapter = new MyAdapter(MainActivity.this, list); lv.setAdapter(adapter); break; case 1: try { //判断版本号并弹出是否更新的提示框 updataInfo = VerSionUtils.getUpdataInfo(inputstreams); int verCode = VerSionUtils.getVerCode(MainActivity.this); //打印当前的版本号 System.out.println("当前版本号====="+verCode); if (verCode < Integer.parseInt(updataInfo.getVersionCode())) { //弹出是否更新的提示框 showUpdataDialog(); } else { Toast.makeText(MainActivity.this, "已经是最新版本,无须更新", Toast.LENGTH_SHORT).show(); } } catch (Exception e) { e.printStackTrace(); } break; } } }; private String path = "http://www.oschina.net/action/api/news_list?catalog=1&pageIndex=0&pageSize=20"; private PullUtils utils = new PullUtils(); private List<NewsBean> list; private InputStream inputstreams; private VersionBean updataInfo; private DownloadManager downloadManager; private ProgressDialog pd; //进度条对话框 private Handler m_mainHandler =new Handler(); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //创建ProgressDialog对象 pd = new ProgressDialog(this); pd.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); pd.setMessage("正在下载更新"); //找控件 initView(); //请求数据 initData(); //控件的点击事件 setlistener(); } private void setlistener() { //版本更新的点击事件 newcode.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { updatasapk(); } }); } //从网络请求数据返回流 并给handler发送消息,进行解析判断 private void updatasapk() { new Thread() { @Override public void run() { super.run(); inputstreams = NetWorkUtils.getInputstreams("http://www.oschina.net/MobileAppVersion.xml"); handler.sendEmptyMessage(1); } }.start(); } /** * 从网络请求数据 */ private void initData() { new Thread() { @Override public void run() { super.run(); String string = NetWorkUtils.getString(path); // System.out.println("======"+string); //解析xml list = utils.getlist(string); //给handler发送消息进行适配器的设置 handler.sendEmptyMessage(0); } }.start(); } /** * 找控件 */ private void initView() { newcode = (Button) findViewById(R.id.newcode); lv = (ListView) findViewById(R.id.lv); } /* * * 弹出对话框通知用户更新程序 * * 弹出对话框的步骤: * 1.创建alertDialog的builder. * 2.要给builder设置属性, 对话框的内容,样式,按钮 * 3.通过builder 创建一个对话框 * 4.对话框show()出来 */ protected void showUpdataDialog() { AlertDialog.Builder builer = new AlertDialog.Builder(this); builer.setTitle("版本升级"); builer.setMessage(updataInfo.getUpdateLog()); //当点确定按钮时从服务器上下载 新的apk 然后安装 builer.setPositiveButton("确定", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { Log.i("sss", "下载apk,更新"); // downLoadApk(); downFile(updataInfo.getDownloadUrl()); } }); //当点取消按钮时进行登录 builer.setNegativeButton("取消", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { } }); AlertDialog dialog = builer.create(); dialog.show(); } private void downFile(final String url) { //提示框显示 pd.show(); //开启子线程下载apk new Thread() { public void run() { //创建HttpClient HttpClient client = new DefaultHttpClient(); //请求方式 HttpGet get = new HttpGet(url); HttpResponse response; try { //请求网络 response = client.execute(get); HttpEntity entity = response.getEntity(); long length = entity.getContentLength(); //设置进度条的最大值 pd.setMax((int)length); //获取流 InputStream is = entity.getContent(); FileOutputStream fileOutputStream = null; //将流写到sd卡中 if (is != null) { File file = new File( Environment.getExternalStorageDirectory(), "updata.spk"); fileOutputStream = new FileOutputStream(file); byte[] buf = new byte[1024]; int ch = -1; int count = 0; //循环将请求的数据写到sd卡 while ((ch = is.read(buf)) != -1) { fileOutputStream.write(buf, 0, ch); count += ch; if (length > 0) { pd.setProgress(count);//设置当前进度 } } } fileOutputStream.flush(); if (fileOutputStream != null) { fileOutputStream.close(); } //告诉HANDER已经下载完成了,可以安装了 down(); } catch (ClientProtocolException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } }.start(); } //提示框消失,进行安装 private void down() { m_mainHandler.post(new Runnable() { 4000 public void run() { pd.cancel(); update(); } }); } /** * 安装程序 */ void update() { Intent intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(Uri.fromFile(new File(Environment .getExternalStorageDirectory(),"updata.spk")),"application/vnd.android.package-archive"); startActivity(intent); } }
相关文章推荐
- AsyncTask的使用
- Netty源码解读(一)概述
- android 文件存储
- .net网站开发中关于gridview控件“弹窗”设置的解决办法
- context.obtainStyledAttributes 研究
- @-ms-viewport
- 如何选择合适的PoE交换机?
- 02-swift微博自定义log
- installing MPTCP
- IntelliJ IDEA 快捷键和设置
- c++调用动态链接库DLL
- 64位windows系统的PatchGuard
- re2c安装
- 故障案例--mysql如何定位执行完但未提交的事务内容
- FFMPEG学习----打印视频信息
- Redis学习笔记(十三)redis配置文件redis.conf中文版(基于2.4)
- linux set 命令及linux设置环境变量命令
- 每天为自己充点电还是好的虽然前期的知识有点乏味,但走一步是一步的心态不能变。嗯,加油!If you find a path with no obstacles, it probably doesn’t
- 第18周 C语言实战105例 实例2:递增递减操作符
- IOS(swift语言)第3天、类,继承