5202
- 收藏
- 点赞
- 分享
- 举报
简单的在线升级功能封装类
本帖最后由 Heguming 于 2015-8-23 16:45 编辑
该类封装了在线升级功能,对外提供一个升级完成接口,通过实例化对象,调用其中的update方法,可实现对应用的升级维护。
参数:
context:上下文
download_url:升级应用下载地址
app_name:应用名称
file_path:文件存放路径
好了,这是代码:
[code]package com.example.update;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
public class Update {
private Context context;
private String download_url;
private String app_name;
private String file_path;
private UpdateDone updateDone;
public Update(Context context, String download_url, String app_name,
String file_path) {
this.context = context; // this
this.download_url = download_url; // "http://192.168.1.103"
this.app_name = app_name; // "Reader"
this.file_path = file_path; // Environment.getExternalStorageDirectory()
// + ...
updateDone = (UpdateDone) context;
}
public void update() {
Intent intent = new Intent(context, UpdateService.class);
context.startService(intent);
}
public interface UpdateDone {
public void updateOK();
public void updateERROR();
}
public class UpdateService extends Service {
private static final int DOWN_OK = 1;
private static final int DOWN_ERROR = 0;
private static final int TIMEOUT = 10 * 1000;
@Override
public IBinder onBind(Intent arg0) {
// TODO Auto-generated method stub
return null;
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
createThread();
return super.onStartCommand(intent, flags, startId);
}
/***
* 开线程下载
*/
public void createThread() {
final Handler handler = new Handler() {
@Override
public void handleMessage(Message msg) {
switch (msg.what) {
case DOWN_OK:
updateDone.updateOK();
break;
case DOWN_ERROR:
updateDone.updateERROR();
break;
default:
break;
}
}
};
final Message message = new Message();
new Thread(new Runnable() {
@Override
public void run() {
try {
long downloadSize = downloadUpdateFile(new URL(
download_url), createFile(file_path, app_name));
if (downloadSize > 0) {
// 下载成功
message.what = DOWN_OK;
handler.sendMessage(message);
}
} catch (Exception e) {
e.printStackTrace();
message.what = DOWN_ERROR;
handler.sendMessage(message);
}
}
}).start();
}
public long downloadUpdateFile(URL url, String file) throws Exception {
int down_step = 5;// 提示step
int totalSize;// 文件总大小
int downloadCount = 0;// 已经下载好的大小
int updateCount = 0;// 已经上传的文件大小
InputStream inputStream;
OutputStream outputStream;
HttpURLConnection httpURLConnection = (HttpURLConnection) url
.openConnection();
httpURLConnection.setConnectTimeout(TIMEOUT);
httpURLConnection.setReadTimeout(TIMEOUT);
// 获取下载文件的size
totalSize = httpURLConnection.getContentLength();
if (httpURLConnection.getResponseCode() == 404) {
throw new Exception("fail!");
}
inputStream = httpURLConnection.getInputStream();
outputStream = new FileOutputStream(file, false);// 文件存在则覆盖掉
byte buffer[] = new byte[1024];
int readsize = 0;
while ((readsize = inputStream.read(buffer)) != -1) {
outputStream.write(buffer, 0, readsize);
downloadCount += readsize;// 时时获取下载到的大小
if (updateCount == 0
|| (downloadCount * 100 / totalSize - down_step) >= updateCount) {
updateCount += down_step;
}
}
if (httpURLConnection != null) {
httpURLConnection.disconnect();
}
inputStream.close();
outputStream.close();
return downloadCount;
}
public String createFile(String path, String name) {
if (android.os.Environment.MEDIA_MOUNTED
.equals(android.os.Environment.getExternalStorageState())) {
File dir = new File(path);
if (!dir.exists()) {
dir.mkdirs();
}
File file = new File(dir + "/" + name + ".apk");
if (!file.exists()) {
try {
file.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return path + "/" + name + ".apk";
}
}
}[/code]
原创,转载请注明出处 :o
该类封装了在线升级功能,对外提供一个升级完成接口,通过实例化对象,调用其中的update方法,可实现对应用的升级维护。
参数:
context:上下文
download_url:升级应用下载地址
app_name:应用名称
file_path:文件存放路径
好了,这是代码:
[code]package com.example.update;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
public class Update {
private Context context;
private String download_url;
private String app_name;
private String file_path;
private UpdateDone updateDone;
public Update(Context context, String download_url, String app_name,
String file_path) {
this.context = context; // this
this.download_url = download_url; // "http://192.168.1.103"
this.app_name = app_name; // "Reader"
this.file_path = file_path; // Environment.getExternalStorageDirectory()
// + ...
updateDone = (UpdateDone) context;
}
public void update() {
Intent intent = new Intent(context, UpdateService.class);
context.startService(intent);
}
public interface UpdateDone {
public void updateOK();
public void updateERROR();
}
public class UpdateService extends Service {
private static final int DOWN_OK = 1;
private static final int DOWN_ERROR = 0;
private static final int TIMEOUT = 10 * 1000;
@Override
public IBinder onBind(Intent arg0) {
// TODO Auto-generated method stub
return null;
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
createThread();
return super.onStartCommand(intent, flags, startId);
}
/***
* 开线程下载
*/
public void createThread() {
final Handler handler = new Handler() {
@Override
public void handleMessage(Message msg) {
switch (msg.what) {
case DOWN_OK:
updateDone.updateOK();
break;
case DOWN_ERROR:
updateDone.updateERROR();
break;
default:
break;
}
}
};
final Message message = new Message();
new Thread(new Runnable() {
@Override
public void run() {
try {
long downloadSize = downloadUpdateFile(new URL(
download_url), createFile(file_path, app_name));
if (downloadSize > 0) {
// 下载成功
message.what = DOWN_OK;
handler.sendMessage(message);
}
} catch (Exception e) {
e.printStackTrace();
message.what = DOWN_ERROR;
handler.sendMessage(message);
}
}
}).start();
}
public long downloadUpdateFile(URL url, String file) throws Exception {
int down_step = 5;// 提示step
int totalSize;// 文件总大小
int downloadCount = 0;// 已经下载好的大小
int updateCount = 0;// 已经上传的文件大小
InputStream inputStream;
OutputStream outputStream;
HttpURLConnection httpURLConnection = (HttpURLConnection) url
.openConnection();
httpURLConnection.setConnectTimeout(TIMEOUT);
httpURLConnection.setReadTimeout(TIMEOUT);
// 获取下载文件的size
totalSize = httpURLConnection.getContentLength();
if (httpURLConnection.getResponseCode() == 404) {
throw new Exception("fail!");
}
inputStream = httpURLConnection.getInputStream();
outputStream = new FileOutputStream(file, false);// 文件存在则覆盖掉
byte buffer[] = new byte[1024];
int readsize = 0;
while ((readsize = inputStream.read(buffer)) != -1) {
outputStream.write(buffer, 0, readsize);
downloadCount += readsize;// 时时获取下载到的大小
if (updateCount == 0
|| (downloadCount * 100 / totalSize - down_step) >= updateCount) {
updateCount += down_step;
}
}
if (httpURLConnection != null) {
httpURLConnection.disconnect();
}
inputStream.close();
outputStream.close();
return downloadCount;
}
public String createFile(String path, String name) {
if (android.os.Environment.MEDIA_MOUNTED
.equals(android.os.Environment.getExternalStorageState())) {
File dir = new File(path);
if (!dir.exists()) {
dir.mkdirs();
}
File file = new File(dir + "/" + name + ".apk");
if (!file.exists()) {
try {
file.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return path + "/" + name + ".apk";
}
}
}[/code]
原创,转载请注明出处 :o
我来回答
回答2个
时间排序
认可量排序
认可0
认可0
或将文件直接拖到这里
悬赏:
E币
网盘
* 网盘链接:
* 提取码:
悬赏:
E币
Markdown 语法
- 加粗**内容**
- 斜体*内容*
- 删除线~~内容~~
- 引用> 引用内容
- 代码`代码`
- 代码块```编程语言↵代码```
- 链接[链接标题](url)
- 无序列表- 内容
- 有序列表1. 内容
- 缩进内容
- 图片![alt](url)
相关问答
-
2013-11-16 18:04:22
-
2020-01-21 10:12:33
-
2019-09-02 14:59:49
-
2019-09-20 18:12:07
-
2019-01-29 14:54:13
-
2016-06-21 18:01:31
-
02008-08-02 00:21:25
-
2020-11-28 13:36:36
-
2020-08-24 19:25:23
-
2018-05-16 14:08:02
-
2018-10-10 18:06:44
-
2020-03-24 14:51:36
-
2015-02-27 15:23:41
-
2010-01-21 11:12:15
-
2020-03-27 15:45:57
-
42019-08-31 19:56:49
-
2019-02-15 15:17:54
-
22015-09-01 20:25:30
-
2018-12-06 10:26:42
无更多相似问答 去提问
点击登录
-- 积分
-- E币
提问
—
收益
—
被采纳
—
我要提问
切换马甲
上一页
下一页
悬赏问答
-
5Hi3516CV610 如何使用SD卡升级固件
-
5cat /dev/logmpp 报错 <3>[ vi] [func]:vi_send_frame_node [line]:99 [info]:vi pic queue is full!
-
50如何获取vpss chn的图像修改后发送至vo
-
5FPGA通过Bt1120传YUV422数据过来,vi接收不到数据——3516dv500
-
50SS928 运行PQtools 拼接 推到设备里有一半画面会异常
-
53536AV100的sample_vdec输出到CVBS显示
-
10海思板子mpp怎么在vi阶段改变视频数据尺寸
-
10HI3559AV100 多摄像头同步模式
-
9海思ss928单路摄像头vio中加入opencv处理并显示
-
10EB-RV1126-BC-191板子运行自己编码的程序
举报反馈
举报类型
- 内容涉黄/赌/毒
- 内容侵权/抄袭
- 政治相关
- 涉嫌广告
- 侮辱谩骂
- 其他
详细说明
提醒
你的问题还没有最佳答案,是否结题,结题后将扣除20%的悬赏金
取消
确认
提醒
你的问题还没有最佳答案,是否结题,结题后将根据回答情况扣除相应悬赏金(1回答=1E币)
取消
确认