菜鸟的博客请多多指教

最近做了一个新功能,更新APK的功能

1、更新APK是一个耗时的任务,我采用了一个服务来做,上次在网上看到服务是在主线程里面,自己也测试了下,数据是真的

所以下载动作还必须在服务里面开一个线程来处理下载任务,在写下载任务的时候,由于经验不够,中间出了很多错,下载要分下载开始,下载中,下载异常,下载结束,

2、在后台下载,对用户来说,是看不见的,无法与用户交流,所以在开启服务的时候我就初始化了一个通知,通知在服务开启是初始化,所以其实通知也是在UI线程中,所以想在下载线程中更新通知进度栏,必须通过机制,来更新通知栏的进度条,但是后来组长又给你我一个需求,在点击通知栏时,在更新界面弹出一个对话框,显示是否要停止更新,这里就涉及到一个新的问题,怎样在Service里面获取Activity呢,找了很多资料,最后找到了一个ActivityManager系统服务,他可以得到指定包的任务栈,得到栈顶的Activity,这样就可以在Service中获取Activity里面了

            String MY_PKG_NAME = "com.example.sunny.text333";
int i=0;
ActivityManager manager = (ActivityManager) MyApplication.getInstance().getSystemService(Context.ACTIVITY_SERVICE);
List<ActivityManager.RunningTaskInfo> list = manager.getRunningTasks(100);
for (ActivityManager.RunningTaskInfo info: list){
if(info.topActivity.getPackageName().equals(MY_PKG_NAME) || info.baseActivity.getPackageName().equals(MY_PKG_NAME)){
break;
}
i++;
}
ActivityManager.RunningTaskInfo info=list.get(i);
String className = info.topActivity.getClassName();//得到栈顶的Activity
Intent intent = null;
try {
intent = new Intent(this,Class.forName(className));
intent.addCategory(Intent.CATEGORY_LAUNCHER);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK| Intent.FLAG_ACTIVITY_SINGLE_TOP);
Bundle bundle = new Bundle();
bundle.putString("flag","取消");
intent.putExtras(bundle);
} catch (ClassNotFoundException e) {
e.printStackTrace();
Log.e("没有找到Activity",""+className);
}
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
|Intent.FLAG_ACTIVITY_NEW_TASK);
pendingIntent = PendingIntent.getActivity(this, 0,intent,PendingIntent.FLAG_UPDATE_CURRENT);
notification.contentIntent =pendingIntent;
Log.e("通知的Activity名称",""+className);

下载线程:

class DownloadThread extends Thread {
String url;
MyHandler handler; public DownloadThread(String url, MyHandler handler) {
this.url = url;
this.handler = handler;
} @Override
public void run() { FileOutputStream fos = null;
InputStream io = null;
try {
handler.sendEmptyMessage(0);
URL u = new URL(url);
Log.e("URL","____________"+u);
HttpURLConnection connection = (HttpURLConnection) u.openConnection();
connection.setConnectTimeout(30000);
connection.setReadTimeout(30000);
io = connection.getInputStream(); if (io != null) {
File file = new File(Environment.getExternalStorageDirectory() + "/" + "mapp.apk");
byte[] buff = new byte[1024];
if (file.exists()) {
file.delete();
} else {
file.createNewFile();
}
fos = new FileOutputStream(file);
int readNum = 0;
int lastPress = 0;//上一次的进度值
int press = 0;//
int fileLength = 0;
int count = 0;//记录文件的读取个数
while (isrunning) {
readNum = io.read(buff);
fileLength = connection.getContentLength();
count += readNum;
press = (int) ((float) count / (float) (fileLength - 1) * 100.0);//下载总大小除以总的大小*100
BigDecimal bd = new BigDecimal(count);
BigDecimal bd1 = new BigDecimal(fileLength - 1);
if (readNum == -1 && bd.toPlainString().equals(bd1.toPlainString())) {// 下载完成
Log.e("sdas", "下载完成");
Message message = new Message();
message.obj = file;
message.what = 2;
handler.sendMessage(message);
break;
} else if (readNum == -1 && !bd.toPlainString().equals(bd1.toPlainString())) {
Log.e("sdas", "下载失败");
handler.sendEmptyMessage(4);
break;
} else if (readNum > 0) {
fos.write(buff, 0, readNum);
// Log.e("sdas", "readNum:" + readNum);
if (press - lastPress >= 1) {
lastPress = press;
// Log.e("sdas", "press:" + press);
Message message = new Message();
message.what = 1;
message.obj = press + "";
handler.sendMessage(message);
}
}
}
fos.flush();
fos.close();
io.close();
}
} catch (MalformedURLException e) {
handler.sendEmptyMessage(4);
e.printStackTrace();
} catch (IOException e) {
handler.sendEmptyMessage(4);
e.printStackTrace();
} finally {
try {
if (fos != null) {
fos.flush();
fos.close();
}
if (io != null) {
io.close();
}
} catch (IOException e) { } }
}
}

MyHandler的代码:

class MyHandler extends Handler {
@Override
public void handleMessage(Message msg) { super.handleMessage(msg);
if (msg != null) {
switch (msg.what) {
case 0:// 初始化通知栏和进度条
views.setTextViewText(R.id.tvProcess, "开始下载……");
views.setTextColor(R.id.tvProcess, Color.BLACK);
views.setProgressBar(R.id.app_down_progressBar, 100, 0, false);
notification.defaults = Notification.DEFAULT_LIGHTS;
notification.contentView = views;
nm.notify(NOTIFICATIO_UPDATE_SOFT, notification);
MyApplication.getInstance().setIsupdateing(true);
break;
case 1:
int download_precent = Integer.parseInt(msg.obj.toString());// 获取下载进度
// 更新状态栏上的下载进度信息
views.setTextViewText(R.id.tvProcess, "已下载" + download_precent + "%");
views.setTextColor(R.id.tvProcess, Color.BLACK);
views.setProgressBar(R.id.app_down_progressBar, 100, download_precent, false);
notification.defaults = Notification.DEFAULT_LIGHTS;
notification.contentView = views;
nm.notify(NOTIFICATIO_UPDATE_SOFT, notification);
MyApplication.getInstance().setIsupdateing(true); break;
case 2:
MyApplication.getInstance().setIsupdateing(false);
/* if( unIntallapk()){
Log.e("文件卸载",""+unIntallapk()); }*/
Instanll((File) msg.obj); nm.cancel(NOTIFICATIO_UPDATE_SOFT);
// 停止掉当前的服务
stopSelf();
break;
case 3:// 停止下载
MyApplication.getInstance().setIsupdateing(false);
// 停止掉当前的服务
nm.cancel(NOTIFICATIO_UPDATE_SOFT);
stopSelf();
break;
case 4:// 下载异常
MyApplication.getInstance().setIsupdateing(false);
views.setTextViewText(R.id.tvProcess, "" + "已停止下载");
views.setTextColor(R.id.tvProcess, Color.BLUE);
// 在任务栏不被用户手动删除
notification.flags = Notification.FLAG_AUTO_CANCEL;
// 调用系统默认的声音提示以及震动
notification.defaults = Notification.DEFAULT_ALL;
notification.contentView = views;
nm.cancel(NOTIFICATIO_UPDATE_SOFT);
nm.notify(NOTIFICATIO_UPDATE_SOFT, notification);
break;
default:
break;
}
}
}
}

android APK更新的更多相关文章

  1. Android Apk增量更新

    前言 有关APK更新的技术比较多,例如:增量更新.插件式开发.热修复.RN.静默安装. 下面简单介绍一下: 什么是增量更新?   增量更新就是原有app的基础上只更新发生变化的地方,其余保持原样. 与 ...

  2. 【原】Android热更新开源项目Tinker源码解析系列之一:Dex热更新

    [原]Android热更新开源项目Tinker源码解析系列之一:Dex热更新 Tinker是微信的第一个开源项目,主要用于安卓应用bug的热修复和功能的迭代. Tinker github地址:http ...

  3. 【原】Android热更新开源项目Tinker源码解析系列之二:资源文件热更新

    上一篇文章介绍了Dex文件的热更新流程,本文将会分析Tinker中对资源文件的热更新流程. 同Dex,资源文件的热更新同样包括三个部分:资源补丁生成,资源补丁合成及资源补丁加载. 本系列将从以下三个方 ...

  4. Android 数据库管理— — —更新数据

    <?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android=" ...

  5. Android 增量更新(BSDiff / bspatch)

    Android 增量更新 BSDiff / bspatchhttp://www.daemonology.net/bsdiff/android的代码目录下 \external\bsdiff bsdiff ...

  6. android apk 自我保护技术-加密apk

    经过了忙碌的一周终于有时间静下来写点东西了,我们继续介绍android apk防止反编译技术的另一种方法.前两篇我们讲了加壳技术(http://my.oschina.net/u/2323218/blo ...

  7. 如何加密android apk

    经过了忙碌的一周终于有时间静下来写点东西了,我们继续介绍android apk防止反编译技术的另一种方法.前两篇我们讲了加壳技术(http://my.oschina.net/u/2323218/blo ...

  8. android开发 更新升级安装到一半自动闪退

    如题:android开发 更新升级安装到一半自动闪退,,,解决办法,如下(红色为我新增的代码) /**     * 安装APK文件     */    private void installApk( ...

  9. Android热更新开源项目Tinker集成实践总结

    前言 最近项目集成了Tinker,开始认为集成会比较简单,但是在实际操作的过程中还是遇到了一些问题,本文就会介绍在集成过程大家基本会遇到的主要问题. 考虑一:后台的选取 目前后台功能可以通过三种方式实 ...

随机推荐

  1. SQL 2008 R2下载 升级R2 SP1或者SQL 2008从10.50.1600升级10.5.2500

    SQL Server 2008 R2 中英文 开发版/企业版/标准版 链接地址 一. 简体中文 1. SQL Server 2008 R2 Developer (x86, x64, ia64) - D ...

  2. dev GridControl实现拖拽

    一.示例说明 以gridControl1和gridControl2为例,从gridControl1拖拽行到gridControl2中去. 二.属性设置 gridControl2.AllowDrop = ...

  3. linux下TCP/IP及内核参数优化调优(转)

    Linux下TCP/IP及内核参数优化有多种方式,参数配置得当可以大大提高系统的性能,也可以根据特定场景进行专门的优化,如TIME_WAIT过高,DDOS攻击等等. 如下配置是写在sysctl.con ...

  4. arm 2440 linux 应用程序 nes 红白机模拟器 第1篇

    对比了很多种,开源的 NES 模拟器 VirtuaNES , nestopia , FakeNES , FCEUX , InfoNES , LiteNES 最后决定使用 LiteNES 进行移值,它是 ...

  5. python 内置函数!

    chr 数字转换字母 r = chr(65)print(r) ord字母转换数字n = ord("A")print(n) random 函数 import random li = ...

  6. [课程设计]Sprint Three 回顾与总结&发表评论&团队贡献分

    Sprint Three 回顾与总结&发表评论&团队贡献分 ● 一.回顾与总结 (1)回顾 燃尽图: Sprint计划-流程图: milestones完成情况如下: (2)总结 本次冲 ...

  7. 95、Jenkins部署.net持续集成自动化测试环境

    ##目录 1. 安装Jenkins 1. 配置Jenkins 1. 自动编译 1. 自动部署 1. 自动测试 环境介绍: web服务器机器:192.168.1.7 svn服务器:192.168.1.5 ...

  8. android 命名 数组 所有国家 String[] COUNTRIES

    static final String[] COUNTRIES = new String[] { "Afghanistan", "Albania", " ...

  9. OpenLayers2.13.1知识整理

    对于地图的默认加载等,网上很多资料,就不细说了,主要整理解决一些api不明显或不全的内容 因玩webgis只有一周左右,肯定很水~~ 我资料中的目录结构如下: html map js(自己封装的js目 ...

  10. JavaScript的chapterII

    程序流程控制: 1.条件语句——if     if(condition) {statement1}     else {statement2} 例子:     if(i<60 && ...