博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
版本升级demo(thread+service+Notification)
阅读量:7099 次
发布时间:2019-06-28

本文共 4262 字,大约阅读时间需要 14 分钟。

11.png  33.png 66.png 

第一步:获取本机app版本:

public int getVerCode(Context _context,String _package) {int verCode = -1;try {verCode = _context.getPackageManager().getPackageInfo(_package, 0).versionCode;} catch (NameNotFoundException e) {}return verCode;}

第二步:线程从服务端下载当前版本以及升级提示:

public JSONObject getJsonObject(String Url) {HttpClient client = new DefaultHttpClient();StringBuilder sb = new StringBuilder();String js = null;JSONObject son=null;HttpGet myget = new HttpGet(Url); try {HttpParams params = new BasicHttpParams();HttpConnectionParams.setConnectionTimeout(params, 8000);HttpResponse response = client.execute(myget);BufferedReader reader = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));for (String s = reader.readLine(); s != null; s = reader.readLine()) {sb.append(s);}js = sb.toString();son = new JSONObject(js);} catch (Exception e) {// TODO Auto-generated catch blockSystem.out.println("异常-》下载转化JSON");return null;}return son;}

第三步:进行版本号对比,若有新版本,进行版本升级提示,builder使用自定义view。

 

LinearLayout ll = (LinearLayout) LayoutInflater.from(TestVersionUpdateActivity.this).inflate(R.layout.layout_loadapk, null);pb = (ProgressBar) ll.findViewById(R.id.down_pb);tv = (TextView) ll.findViewById(R.id.tv);Builder builder = new Builder(TestVersionUpdateActivity.this);builder.setView(ll);builder.setTitle("版本更新进度提示");builder.setNegativeButton("后台下载",new DialogInterface.OnClickListener() {@Overridepublic void onClick(DialogInterface dialog, int which) {Intent intent=new Intent(TestVersionUpdateActivity.this, VersionService.class); startService(intent);dialog.dismiss();}});builder.show();new Thread() {public void run() {loadFile("http://1.nightman.sinaapp.com/test/good.zip");}}.start();

第四步,进行更新

public void loadFile(String url) {HttpClient client = new DefaultHttpClient();HttpGet get = new HttpGet(url);HttpResponse response;try {response = client.execute(get);HttpEntity entity = response.getEntity();float length = entity.getContentLength();InputStream is = entity.getContent();FileOutputStream fileOutputStream = null;if (is != null) {File file = new File(Environment.getExternalStorageDirectory(),"NightMan.apk");fileOutputStream = new FileOutputStream(file);byte[] buf = new byte[1024];int ch = -1;float count = 0;while ((ch = is.read(buf)) != -1) {fileOutputStream.write(buf, 0, ch);count += ch;sendMsg(1,(int) (count*100/length));}}sendMsg(2,0);fileOutputStream.flush();if (fileOutputStream != null) {fileOutputStream.close();}} catch (Exception e) {sendMsg(-1,0);}}private void sendMsg(int flag,int c) {Message msg = new Message();msg.what = flag;msg.arg1=c;handler.sendMessage(msg);}private Handler handler = new Handler() {@Overridepublic void handleMessage(Message msg) {
// 定义一个Handler,用于处理下载线程与UI间通讯if (!Thread.currentThread().isInterrupted()) {switch (msg.what) {case 1:pb.setProgress(msg.arg1);loading_process = msg.arg1;tv.setText("已为您加载了:" + loading_process + "%");break;case 2:Intent intent = new Intent(Intent.ACTION_VIEW);intent.setDataAndType(Uri.fromFile(new File(Environment.getExternalStorageDirectory(), "NightMan.apk")),"application/vnd.android.package-archive");startActivity(intent);break;case -1:String error = msg.getData().getString("error");Toast.makeText(TestVersionUpdateActivity.this, error, 1).show();break;}}super.handleMessage(msg);}};

后台下载service循环handler

 

private Handler mHandler = new Handler() {@Overridepublic void handleMessage(Message msg) {// 1为出现,2为隐藏if(TestVersionUpdateActivity.loading_process>99){notificationMrg.cancel(0);stopSelf();return;}if(TestVersionUpdateActivity.loading_process>old_process){displayNotificationMessage(TestVersionUpdateActivity.loading_process);}new Thread() {public void run() {isFirstStart=false;Message msg = mHandler.obtainMessage();mHandler.sendMessage(msg);}}.start();old_process =TestVersionUpdateActivity.loading_process;}};

有需要改进的地方望大哥们多踢踢BUG

应部分朋友的需求,这边上一下服务端的代码

服务器端以PHP为例,输出一个JSON格式的字符串
<?php
echo '{        "version":2,
        "content":[{"id":0,"text":"增加了摇一摇自动排列频道的功能"},
{"id":1,"text":"优化了拖拽缓冲的效果"},
{"id":2,"text":"改善了PATH菜单用户体验"},
{"id":3,"text":"添加了更多名人趣事"}
]}
        ';

源码:http://files.cnblogs.com/shanzei/versionUpdate%E7%89%88%E6%9C%AC%E5%8D%87%E7%BA%A7demo%EF%BC%88thread_service_Notification%EF%BC%89.zip

原文:

转载于:https://www.cnblogs.com/shanzei/archive/2012/04/19/2456655.html

你可能感兴趣的文章
抓交通肇事犯
查看>>
ABAP程序系统字段中英文详解
查看>>
ruby 可枚举模块Enumerable
查看>>
线阵CCD和面阵CCD
查看>>
[BZOJ 3211]花神游历各国(并查集+树状数组)
查看>>
Python中给文件加锁
查看>>
4.9Python数据类型(5)列表(新版)
查看>>
软件构造期末复习考点总结
查看>>
7 Django的模板层
查看>>
EF中Json序列化对象时检测到循环引用的解决办法
查看>>
词向量概况
查看>>
css3 画圆记录
查看>>
javascript中级
查看>>
《CLR Via C# 第3版》笔记之(十五) - 接口
查看>>
golang实现ios推送
查看>>
【Linux】linux常用基本命令
查看>>
libsvm使用说明
查看>>
CodeForces 595A Vitaly and Night
查看>>
秒杀读后感2
查看>>
插入排序
查看>>