Android实现应用下载并自动安装apk包

安装:

[java] view plain copy
  1. Stringstr="/CanavaCancel.apk";
  2. StringfileName=Environment.getExternalStorageDirectory()+str;
  3. Intentintent=newIntent(Intent.ACTION_VIEW);
  4. intent.setDataAndType(Uri.fromFile(newFile(fileName)),"application/vnd.android.package-archive");
  5. startActivity(intent);

卸载:

[java] view plain copy
  1. UripackageURI=Uri.parse("package:com.demo.CanavaCancel");
  2. IntentuninstallIntent=newIntent(Intent.ACTION_DELETE,packageURI);
  3. startActivity(uninstallIntent);

下载apk程序代码

[java] view plain copy
  1. protectedFiledownLoadFile(StringhttpUrl){
  2. //TODOAuto-generatedmethodstub
  3. finalStringfileName="updata.apk";
  4. FiletmpFile=newFile("/sdcard/update");
  5. if(!tmpFile.exists()){
  6. tmpFile.mkdir();
  7. }
  8. finalFilefile=newFile("/sdcard/update/"+fileName);
  9. try{
  10. URLurl=newURL(httpUrl);
  11. try{
  12. HttpURLConnectionconn=(HttpURLConnection)url
  13. .openConnection();
  14. InputStreamis=conn.getInputStream();
  15. FileOutputStreamfos=newFileOutputStream(file);
  16. byte[]buf=newbyte[256];
  17. conn.connect();
  18. doublecount=0;
  19. if(conn.getResponseCode()>=400){
  20. Toast.makeText(Main.this,"连接超时",Toast.LENGTH_SHORT)
  21. .show();
  22. }else{
  23. while(count<=100){
  24. if(is!=null){
  25. intnumRead=is.read(buf);
  26. if(numRead<=0){
  27. break;
  28. }else{
  29. fos.write(buf,0,numRead);
  30. }
  31. }else{
  32. break;
  33. }
  34. }
  35. }
  36. conn.disconnect();
  37. fos.close();
  38. is.close();
  39. }catch(IOExceptione){
  40. //TODOAuto-generatedcatchblock
  41. e.printStackTrace();
  42. }
  43. }catch(MalformedURLExceptione){
  44. //TODOAuto-generatedcatchblock
  45. e.printStackTrace();
  46. }
  47. returnfile;
  48. }
  49. //打开APK程序代码
  50. privatevoidopenFile(Filefile){
  51. //TODOAuto-generatedmethodstub
  52. Log.e("OpenFile",file.getName());
  53. Intentintent=newIntent();
  54. intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
  55. intent.setAction(android.content.Intent.ACTION_VIEW);
  56. intent.setDataAndType(Uri.fromFile(file),
  57. "application/vnd.android.package-archive");
  58. startActivity(intent);
  59. }

更多相关文章

  1. Pycharm安装PyQt5的详细教程
  2. android 环境变量搭建
  3. Android(安卓)4.0 开发环境离线安装(Linux)
  4. Android——NDK下载提示缺少toolchains问题解决
  5. Android(安卓)Fragment使用和切换 笔记
  6. 安卓模拟器Android(安卓)SDK安装完整图文教程
  7. android实现异步下载过程
  8. Android之断点续传下载
  9. android之网络资源多线程下载

随机推荐

  1. SQL on Hadoop 技术分析(二)
  2. Yarn【label-based scheduling】实战总结
  3. Yarn【label-based scheduling】实战总结
  4. BAT大佬带你了解AB测试
  5. Vue3 系统入门与项目实战
  6. 端午搬砖:聊聊调度云服务
  7. 原创 | 后疫情时代下5G安全的风险防控及
  8. 原创 | 工业网络通讯数据分析之Wireshark
  9. 闲聊乐视
  10. Cosmos DB的5种事物一致性