一段Android实现应用下载并自动安装apk包的代码

一段Android实现应用下载并自动安装apk包的代码,第1张

概述下载apk程序代码protectedFiledownLoadFile(StringhttpUrl){//TODOAuto-generatedmethodstubfinalStringfileName=“updata.apk”;FiletmpFile=newFile("/sdcard/update");if(!tmpFile.exists()){tmpFile.mkdir();}finalFilefile=newFile(&q

下载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包的代码所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1117445.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-29
下一篇 2022-05-29

发表评论

登录后才能评论

评论列表(0条)

保存