代码如下:
/*
* 从服务器中下载APK
*/ protected void downLoadApk() {
final ProgressDialog pd; //进度条对话框
pd = new ProgressDialog(this);
pd.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
pd.setMessage("正在下载更新");
pd.show();
//adpage.this.AlertMSG(info.getUrl()); new Thread(){
@Override
public void run() {
try {
File file = getFileFromServer1(info.getUrl(), pd);
sleep(3000);
installApk(file);
pd.dismiss(); //结束掉进度条对话框
} catch (Exception e) {
Message msg = new Message();
msg.what = DOWN_ERROR;
handler.sendMessage(msg);
e.printStackTrace();
}
}}.start();
} //服务器端获取APK信息
public File getFileFromServer1(String path, ProgressDialog pd) {
// 如果相等的话表示当前的sdcard挂载在手机上并且是可用的
try {
if (Environment.getExternalStorageState().equals(
Environment.MEDIA_MOUNTED)) {
URL url = new URL(path);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
// 自带获取到文件的大小
int Max = conn.getContentLength();
pd.setMax(Max);
InputStream is = conn.getInputStream();
// 新建一个文件路径以及保存的软件名称
File file = new File(Environment.getExternalStorageDirectory().getPath(),"wmh.apk");
FileOutputStream fos = new FileOutputStream(file);
BufferedInputStream bis = new BufferedInputStream(is);
byte[] buffer = new byte[2048];
int len;
int total = 0;
while ((len = bis.read(buffer)) != -1) {
fos.write(buffer, 0, len);
total += len;
// 获取当前下载量
pd.setProgress(total);
}
fos.close();
bis.close();
is.close();
conn.disconnect();
return file;
}
else {
return null;
}
}
catch (Exception e) {
Message msg = new Message();
msg.what = GET_UNDATAINFO_ERROR;
handler.sendMessage(msg);
return null;
}
}安装完第一次执行,检测到版本不一样,下载更新安装,没有问题。
第二次再次登录的时候,提示版本不一样,点击确定,下载,但是失败。
现在是测试阶段,版本还是原来的,程序里写的也是死的,每次登录都让提示下载。为什么第一次安装后,能下载,并能安装,只下载不安装,下次登录还能下载安装(正常)一旦提示下载安装后,再次运行,就下载不下来了?手机
/*
* 从服务器中下载APK
*/ protected void downLoadApk() {
final ProgressDialog pd; //进度条对话框
pd = new ProgressDialog(this);
pd.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
pd.setMessage("正在下载更新");
pd.show();
//adpage.this.AlertMSG(info.getUrl()); new Thread(){
@Override
public void run() {
try {
File file = getFileFromServer1(info.getUrl(), pd);
sleep(3000);
installApk(file);
pd.dismiss(); //结束掉进度条对话框
} catch (Exception e) {
Message msg = new Message();
msg.what = DOWN_ERROR;
handler.sendMessage(msg);
e.printStackTrace();
}
}}.start();
} //服务器端获取APK信息
public File getFileFromServer1(String path, ProgressDialog pd) {
// 如果相等的话表示当前的sdcard挂载在手机上并且是可用的
try {
if (Environment.getExternalStorageState().equals(
Environment.MEDIA_MOUNTED)) {
URL url = new URL(path);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
// 自带获取到文件的大小
int Max = conn.getContentLength();
pd.setMax(Max);
InputStream is = conn.getInputStream();
// 新建一个文件路径以及保存的软件名称
File file = new File(Environment.getExternalStorageDirectory().getPath(),"wmh.apk");
FileOutputStream fos = new FileOutputStream(file);
BufferedInputStream bis = new BufferedInputStream(is);
byte[] buffer = new byte[2048];
int len;
int total = 0;
while ((len = bis.read(buffer)) != -1) {
fos.write(buffer, 0, len);
total += len;
// 获取当前下载量
pd.setProgress(total);
}
fos.close();
bis.close();
is.close();
conn.disconnect();
return file;
}
else {
return null;
}
}
catch (Exception e) {
Message msg = new Message();
msg.what = GET_UNDATAINFO_ERROR;
handler.sendMessage(msg);
return null;
}
}安装完第一次执行,检测到版本不一样,下载更新安装,没有问题。
第二次再次登录的时候,提示版本不一样,点击确定,下载,但是失败。
现在是测试阶段,版本还是原来的,程序里写的也是死的,每次登录都让提示下载。为什么第一次安装后,能下载,并能安装,只下载不安装,下次登录还能下载安装(正常)一旦提示下载安装后,再次运行,就下载不下来了?手机
解决方案 »
- eclipse创建类 能继承expandablelistadatper无法继承baseexpandablelistadapter 如何解决?
- 求 教!急!!!
- 用麦克风录音时,为什么会把耳机里的声音也录进去?
- 怎样让WindowManager加上的view和周围没被view挡住的控件都获得焦点
- 如何实现图片的滚动切换呢?
- android 无法安装,求大大们帮助
- 关于Android 模拟器 Location Control的问题
- arcgis for android离线矢量应用如何开发?
- android动态注册的广播是不是app关闭就不能接收到了?
- 执行adb shell, 提示: error: no devices/emulators found..., 为什么呢?
- android Gallery 的 getView方法不执行
- android官方的api里面有资源管理器吗?
多打点调试log,逐步缩小排查范围
HttpURLConnection conn = (HttpURLConnection) url.openConnection();能正常读取到吗?
bis.read抑或压根就没跑到downLoadApk方法?