我正在从服务器下载一个 Android 应用程序并将其保存到我的应用程序的内部空间,使用:
URL url = new URL(Urls[0]);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
File outputFile = new File(getFilesDir(), "Apps");
outputFile.mkdirs();
File file = makeFilename(outputFile, _appName + ".apk");
file.createNewFile();
file.setExecutable(true);
file.setReadable(true, false);
file.setWritable(true, false);
FileOutputStream fout = new FileOutputStream(zipFile);
InputStream in = conn.getInputStream();
byte[] buffer = new byte[1024];
int length = 0;
while ((length = in.read(buffer)) > 0) {
fout.write(buffer, 0, length);
}
fout.close();
in.close();
outputFile.setExecutable(true, false);
outputFile.setReadable(true, false);
outputFile.setWritable(true, false);
private File makeFilename(File base, String name) {
if (name.indexOf(File.separatorChar) < 0) {
return new File(base, name);
}
throw new IllegalArgumentException("File " + name
+ " contains a path separator");
}
下载完成后,会出现通知。当我单击此通知时,应安装该应用程序。但实际上,当我点击通知时,会出现“解析包时出错”。我做错了什么?
Intent i = new Intent(Intent.ACTION_VIEW);
i.setDataAndType(Uri.fromFile(new File(getFilesDir()+ "/Apps/_appName.apk")),"application/vnd.android.package-archive");
PendingIntent act = PendingIntent.getActivity(MainActivity._activity,0, i, 0);
notification.setLatestEventInfo("TEXT", "TEXT, act);