代码如下:
/*
* 从服务器中下载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;
}
}安装完第一次执行,检测到版本不一样,下载更新安装,没有问题。
第二次再次登录的时候,提示版本不一样,点击确定,下载,但是失败。
现在是测试阶段,版本还是原来的,程序里写的也是死的,每次登录都让提示下载。为什么第一次安装后,能下载,并能安装,只下载不安装,下次登录还能下载安装(正常)一旦提示下载安装后,再次运行,就下载不下来了?手机
解决方案 »
- android.graphics.Bitmap 的mBuffer不能访问
- 求webview显示HTML代码中文乱码的解决办法
- 【求助】我要在win7下跟ok6410开发版进行ADB调试,我用不用重新编译sdk?
- android 开发关于wifi的问题
- android2.3.4(linux2.6.36)下, menuconfig 如何调出来?
- 快来看看!在android元素周期表布局文件怎么写!!!!!
- 遇到个jni的问题 高手进来帮忙看下
- NDK下用C去调JAVA的类的成员函数,老是出现java.lang.NoSuchMethodError,为什么?
- 求android 资料
- 找工作中,这是我自己做的app,求指点
- android Gallery 的 getView方法不执行
- android官方的api里面有资源管理器吗?
多打点调试log,逐步缩小排查范围
HttpURLConnection conn = (HttpURLConnection) url.openConnection();能正常读取到吗?
bis.read抑或压根就没跑到downLoadApk方法?