在我的主類我稱之爲 新的自動更新()的checkForUpdate(本)。
這是動作
public void checkForUpdate(Context context) {
// check when last checked. unless overridden check once a day.
// get current version
int resID = context.getResources().getIdentifier("current_version",
"string", context.getPackageName());
currentVersion = context.getResources().getString(resID);
// check for connection
// now compare versions
if (currentVersion.equals(serverVersion) == false) {
ProgressDialog pDialog = new ProgressDialog(context);
pDialog.setMessage("DO NOT ROTATE YOUR DEVICE. /nI am upgrading your version, press OK then INSTALL in a minute. Please wait....");
pDialog.setIndeterminate(false);
pDialog.setCancelable(true);
pDialog.show();
try {
URL url = new URL(prefs.getString("server_address", null)
+ "/updates/eduDroid.apk");
HttpURLConnection c = (HttpURLConnection) url.openConnection();
String PATH = Environment.getExternalStorageDirectory()
+ "/download/";
File file = new File(PATH);
file.mkdirs();
File outputFile = new File(file, "eduDroid.apk");
FileOutputStream fos = new FileOutputStream(outputFile);
InputStream is = c.getInputStream();
byte[] buffer = new byte[1024];
int len1 = 0;
while ((len1 = is.read(buffer)) != -1) {
fos.write(buffer, 0, len1);
}
fos.close();
is.close();
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(new File(Environment
.getExternalStorageDirectory()
+ "/download/"
+ "file.apk")),
"application/vnd.android.package-archive");
context.startActivity(intent);
} catch (IOException e) {
Toast.makeText(context, "Update error!", Toast.LENGTH_LONG)
.show();
}
pDialog.dismiss();
}
}
的問題是,它需要很長時間下載更新的版本。在這段時間內,用戶留下了空白屏幕。我想向他們展示對話框,但它沒有顯示。請幫助
wtf你會得到resID嗎?爲什麼不使用getString(R.string.current_version)? – njzk2
使用asynctask或activity.showDialog()。你正在嘗試在UI線程之外顯示一個對話框,這是禁止的 – njzk2
@ njzk2謝謝指出!它得到了這個名稱的變體,但現在改變了。 – RuAware