我正在生成一个能够在Android应用程序中包含无线更新的Android应用程序。因此,我正在生成一个用于获取版本代码的Web服务,以便我将比较安装的应用程序的版本代码,如果它更小,那么我将触发从服务器安装的更新,为此我使用下面的代码。
String PATH = Environment.getExternalStorageDirectory() + "/download/";
File file = new File(PATH);
file.mkdirs();
File outputFile = new File(file, "yourapp.apk");
downloadFile(file_url, outputFile);
installApk();
//downloadfile function
private static void downloadFile(String url, File outputFile) {
try {
URL u = new URL(url);
URLConnection conn = u.openConnection();
int contentLength = conn.getContentLength();
DataInputStream stream = new DataInputStream(u.openStream());
byte[] buffer = new byte[contentLength];
stream.readFully(buffer);
stream.close();
DataOutputStream fos = new DataOutputStream(new FileOutputStream(outputFile));
fos.write(buffer);
fos.flush();
fos.close();
} catch (FileNotFoundException e) {
Log.e("FileNotFoundException",e+"");
return;
} catch (IOException e) {
Log.e("IOException",e+"");
return;
}
}
//install apk file function
private void installApk(){
Intent installer = new Intent();
installer.setAction(android.content.Intent.ACTION_VIEW);
installer.putExtra(Intent.ACTION_PACKAGE_REPLACED, "org.wannatrak.android");
installer.setDataAndType(Uri.fromFile(new File(Environment.getExternalStorageDirectory() + "/download/" + "yourapp.apk")), "application/vnd.android.package-archive");
installer.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
this.startActivity(installer);
}
I am generating an android application which capable to include over-the-air updation in the android application. So that I am generating a Webservice for getting the versioncode so that I will compare the versioncode of installed application if it is lesser then I will trigger there is an update to install from the server, for this I using below code.
String PATH = Environment.getExternalStorageDirectory() + "/download/";
File file = new File(PATH);
file.mkdirs();
File outputFile = new File(file, "yourapp.apk");
downloadFile(file_url, outputFile);
installApk();
//downloadfile function
private static void downloadFile(String url, File outputFile) {
try {
URL u = new URL(url);
URLConnection conn = u.openConnection();
int contentLength = conn.getContentLength();
DataInputStream stream = new DataInputStream(u.openStream());
byte[] buffer = new byte[contentLength];
stream.readFully(buffer);
stream.close();
DataOutputStream fos = new DataOutputStream(new FileOutputStream(outputFile));
fos.write(buffer);
fos.flush();
fos.close();
} catch (FileNotFoundException e) {
Log.e("FileNotFoundException",e+"");
return;
} catch (IOException e) {
Log.e("IOException",e+"");
return;
}
}
//install apk file function
private void installApk(){
Intent installer = new Intent();
installer.setAction(android.content.Intent.ACTION_VIEW);
installer.putExtra(Intent.ACTION_PACKAGE_REPLACED, "org.wannatrak.android");
installer.setDataAndType(Uri.fromFile(new File(Environment.getExternalStorageDirectory() + "/download/" + "yourapp.apk")), "application/vnd.android.package-archive");
installer.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
this.startActivity(installer);
}
上面的版本可以达到4.0版本。如果我在果冻豆中尝试它,我会收到“包错误中有一个解析错误”请帮我解决这个问题
感谢。
答案 0 :(得分:1)
您需要为apk文件提供读取权限。在安装apk文件中,添加:
File file = new File(Environment.getExternalStorageDirectory() + "/download/" + "yourapp.apk")
file.setReadable(true, false);
installer.setDataAndType(Uri.fromFile(file), "application/vnd.android.package-archive");