Monday, April 15, 2013

[android help] Something wrong happened when updating app online


Recently I just meet a problem when I want to update my app online. My codes are shown below:



try {
if (Environment.getExternalStorageState().equals(
Environment.MEDIA_MOUNTED)) {
String sdpath = Environment.getExternalStorageDirectory()+ "/";
mSavePath = sdpath + "download/";
URL url = new URL(downUrl);
conn = (HttpURLConnection) url.openConnection();
conn.connect();
// get the size of target file
int length = conn.getContentLength();
is = conn.getInputStream();

File file = new File(mSavePath);
if (!file.exists()) {
file.mkdir();
}
File apkFile = new File(mSavePath, FILE_NAME);
fos = new FileOutputStream(apkFile);
int count = 0;
byte buf[] = new byte[1024];
do {
int numread = is.read(buf);
count += numread;
progress = (int) (((float) count / length) * 100);
mHandler.sendEmptyMessage(DOWNLOAD);
if (numread <= 0) {
fos.flush();
isDownloadFinished = true;
break;
}
fos.write(buf, 0, numread);
fos.flush();
} while (true); // just for test
} else {
System.out.println("no external SDcard");
}
} catch (Exception e) {
mHandler.sendEmptyMessage(DOWNLOADFAIL);
e.printStackTrace();
} finally {
try {
conn.disconnect();
fos.close();
is.close();
} catch (Exception e2) {
// TODO: handle exception
e2.printStackTrace();
}
mDownloadDialog.dismiss();
if (isDownloadFinished == true) {
mHandler.sendEmptyMessage(DOWNLOAD_FINISH);
}
}


And in handleMessage(), there is a function: installApk(). Of course it's in case DOWNLOAD_FINISH.



private void installApk() {
File apkfile = new File(mSavePath, FILE_NAME);
if (!apkfile.exists()) {
return;
}
Intent i = new Intent(Intent.ACTION_VIEW);
i.setDataAndType(Uri.fromFile(new File(mSavePath + FILE_NAME)),
"application/vnd.android.package-archive");
FileDownloadActivity.this.startActivity(i);
}


OK, this is my main code. Simply what I want to do is to replace(or we can say "update") current app with new version which just been downloaded. And next step is to click "Yes" when the dialog which alert us with something like "you're goning to replace the app" shown. Then a progress shown,which means the new verion app is been installing. BUT, now the problem appear: the install surface just exit without any result, alert or infotmation! This is a very simple test app, without anything such as:database, package sign and so on. So anybody met this problem? I need your help. Oh, by the way, forgive my damn poor English... :)



.

stackoverflow.comm

No comments:

Post a Comment

Google Voice on T-Mobile? [General]

Google Voice on T-Mobile? So I recently switched from a GNex on Verizon to a Moto X DE on T-Mobile. I had always used Google Voice for my v...