下载apk程序代码
protected file downLoadfile(String httpUrl) {
// Todo auto-generated method stub
final String filename = “updata.apk”;
file tmpfile = new file("/sdcard/update");
if (!tmpfile.exists()) {
tmpfile.mkdir();
}
final file file = new file("/sdcard/update/" + filename);
try { URL url = new URL(httpUrl); try { httpURLConnection conn = (httpURLConnection) url .openConnection(); inputStream is = conn.getinputStream(); fileOutputStream fos = new fileOutputStream(file); byte[] buf = new byte[256]; conn.connect(); double count = 0; if (conn.getResponseCode() >= 400) { Toast.makeText(Main.this, "连接超时", Toast.LENGTH_SHORT) .show(); } else { while (count <= 100) { if (is != null) { int numRead = is.read(buf); if (numRead <= 0) { break; } else { fos.write(buf, 0, numRead); } } else { break; } } } conn.disconnect(); fos.close(); is.close(); } catch (IOException e) { // Todo auto-generated catch block e.printstacktrace(); } } catch (MalformedURLException e) { // Todo auto-generated catch block e.printstacktrace(); } return file; }
//打开APK程序代码
private voID openfile(file file) {
// Todo auto-generated method stub
Log.e(“Openfile”, file.getname());
Intent intent = new Intent();
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.setAction(androID.content.Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromfile(file),
“application/vnd.androID.package-archive”);
startActivity(intent);
}
String str = “/CanavaCancel.apk”;
String filename = Environment.getExternalStorageDirectory() + str;
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromfile(new file(filename)), “application/vnd.androID.package-archive”);
startActivity(intent);
删除
Uri packageURI = Uri.parse(“package:com.demo.CanavaCancel”);
Intent uninstallintent = new Intent(Intent.ACTION_DELETE, packageURI);
startActivity(uninstallintent);
以上是内存溢出为你收集整理的一段Android实现应用下载并自动安装apk包的代码全部内容,希望文章能够帮你解决一段Android实现应用下载并自动安装apk包的代码所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)