-1
我已經在檢查版本更新的應用程序的oncreate中放置了條件檢查。如果我的應用程序的新版本可用,我將調用onDestroy。安裝新的apk文件編程方式顯示錯誤
public void onCreate(Bundle savedInstanceState) {
if(「true」.equal(CheckVersion))
{
alertbox.setMessage("Do you want to update Aplication with Latest version?");
alertbox.setPositiveButton("Yes",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface arg0, int arg1) {
try {
onDestroy();
} catch (Exception exception) {
exception.toString();
}
}
});
alertbox.setNegativeButton("No",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface arg0, int arg1) {
LaunchManifest();
}
});
alertbox.show();
}
}
/*
* In the onDestroy method I have Placed the code for downloading the New
* apk file and installation of the apk file methods as given below
*/
@Override
public void onDestroy() {
DownloadOnSDcard();
InstallApplication();
}
public void DownloadOnSDcard() {
try {
urlpath = "http://192.168.1.158/VisionEPODWebService/VisionEPOD.apk";
String ApkName = "VisionEPOD.apk";
URL url = new URL(urlpath.toString());
// Your given URL.
HttpURLConnection c = (HttpURLConnection)url.openConnection();
c.setRequestMethod("GET");
c.setDoOutput(true);
c.connect();
// Connection Complete here.!
// Toast.makeText(getApplicationContext(),
// "HttpURLConnection complete.", Toast.LENGTH_SHORT).show();
String PATH = Environment.getExternalStorageDirectory() + "/download/";
File file = new File(PATH); // PATH = /mnt/sdcard/download/
if (!file.exists()) {
file.mkdirs();
}
File outputFile = new File(file, ApkName.toString());
FileOutputStream fos = new FileOutputStream(outputFile);
// Toast.makeText(getApplicationContext(), "SD Card Path: " +
// outputFile.toString(), Toast.LENGTH_SHORT).show();
InputStream is = c.getInputStream();
// Get from Server and Catch In Input Stream Object.
byte[] buffer = new byte[1024];
int len1 = 0;
while ((len1 = is.read(buffer)) != -1) {
fos.write(buffer, 0, len1); // Write In FileOutputStream.
}
fos.close();
is.close();
// till here, it works fine - .apk is download to my sdcard in
// download file.
// So plz Check in DDMS tab and Select your Emualtor.
// Toast.makeText(getApplicationContext(),
// "Download Complete on SD Card.!", Toast.LENGTH_SHORT).show();
// download the APK to sdcard then fire the Intent.
} catch (IOException e) {
Toast.makeText(getApplicationContext(), "Error! " + e.toString(), Toast.LENGTH_LONG)
.show();
}
}
public void InstallApplication() {
String ApkName = "VisionEPOD.apk";
String PackageName = "com.Vision.EPOD";
Uri packageURI = Uri.parse(PackageName.toString());
Intent intent = new Intent(android.content.Intent.ACTION_VIEW, packageURI);
intent.setDataAndType(
Uri.fromFile(new File(Environment.getExternalStorageDirectory() + "/download/"
+ ApkName.toString())), "application/vnd.android.package-archive");
startActivity(intent);
}
的問題是,當得到執行我的安裝方法它顯示一個警告框,
替換應用 您正在安裝會替換其他應用程序中的應用。所有以前的用戶數據將被保存。 並有確定和取消按鈕
當我點擊確定按鈕,它顯示安裝的應用程序
的另一個按鈕,但是當我點擊安裝按鈕,應用程序顯示出,然後進行安裝
一個進度條我將得到一個未安裝完成按鈕的消息應用程序。
即我的新更新沒有安裝。
這是我實施版本更新程序的正確方法。請問任何人請查看。請留下冗長的代碼。
請看看logcat的輸出。那裏也應該有一個錯誤信息。如果您發佈此消息,我們可能會幫助您。 – Janusz