下载app后自动安装程序
其实很简单,只需要几行代码就好了,首先要到服务器下载apk,然后才能安装,当然不是傻子应该都知道,我这里用到的是Httputils去下载,
这里需要一些权限
1 2 3 4 | <uses-permission android:name= "android.permission.INTERNET" /> <uses-permission android:name= "android.permission.WRITE_EXTERNAL_STORAGE" /> <uses-permission android:name= "android.permission.MOUNT_UNMOUNT_FILESYSTEMS" /> <uses-permission android:name= "android.permission.INSTALL_PACKAGES" /> |
直接贴代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 | package com.exmple.httpxutil; import java.io.File; import android.annotation.SuppressLint; import android.app.Activity; import android.content.Intent; import android.content.pm.PackageInfo; import android.content.pm.PackageManager; import android.content.pm.PackageManager.NameNotFoundException; import android.content.pm.Signature; import android.net.Uri; import android.os.Bundle; import android.os.Environment; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.ProgressBar; import com.lidroid.xutils.HttpUtils; import com.lidroid.xutils.exception.HttpException; import com.lidroid.xutils.http.HttpHandler; import com.lidroid.xutils.http.ResponseInfo; import com.lidroid.xutils.http.callback.RequestCallBack; public class MainActivity extends Activity { HttpHandler<File> h; private ProgressBar pa; @SuppressLint( "SdCardPath" ) @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Button button = (Button) findViewById(R.id.button); pa = (ProgressBar) findViewById(R.id.pro); Button more = (Button) findViewById(R.id.more); getCode(); more.setOnClickListener( new OnClickListener() { @Override public void onClick(View v) { Intent ints = new Intent(MainActivity. this , NewActivity. class ); startActivity(ints); } }); button.setOnClickListener( new OnClickListener() { @Override public void onClick(View v) { HttpUtils http = new HttpUtils(); final String path = Environment.getExternalStorageDirectory() .getPath(); System. out .println(path); h = http.download( "http://101.200.142.201:8080/tqyb/baidumap.apk" , path + "/badumap.apk" , true , true , new RequestCallBack<File>() { @Override public void onStart() { System. out .println( "===" ); System. out .println( "开始下载了++++++++++++++" ); } @Override public void onLoading( long total, long current, boolean isUploading) { System. out .println(total + "=====" ); System. out .println(current + "=====" ); System. out .println(isUploading + "=====" ); pa.setMax(( int ) total); pa.setProgress(( int ) current); } @Override public void onFailure(HttpException error, String msg) { System. out .println(error + "+++++++" + msg + "+++++++++" ); } @Override public void onSuccess( ResponseInfo<File> responseInfo) { h.cancel(); installApk(path + "/badumap.apk" ); } }); } }); } //安装的方法 private void installApk(String filename) { File file = new File(filename); Intent intent = new Intent(); intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); intent.setAction(Intent.ACTION_VIEW); String type = "application/vnd.android.package-archive" ; intent.setDataAndType(Uri.fromFile(file), type); startActivity(intent); } //等到版本号的代码 private void getCode() { PackageManager manager; PackageInfo info = null ; manager = this .getPackageManager(); try { info = manager.getPackageInfo( this .getPackageName(), 0); String name = info.versionName; int versionCode = info.versionCode; String packageName = info.packageName; Signature[] signatures = info.signatures; System. out .println(name + "++++++++++" + versionCode + "+++++" + packageName + "========nnn" + signatures + "=============" ); /* * info.versionName; * * info.packageName; * * info.signatures; */ } catch (NameNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } |
【推荐】还在用 ECharts 开发大屏?试试这款永久免费的开源 BI 工具!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 软件产品开发中常见的10个问题及处理方法
· .NET 原生驾驭 AI 新基建实战系列:向量数据库的应用与畅想
· 从问题排查到源码分析:ActiveMQ消费端频繁日志刷屏的秘密
· 一次Java后端服务间歇性响应慢的问题排查记录
· dotnet 源代码生成器分析器入门
· ThreeJs-16智慧城市项目(重磅以及未来发展ai)
· .NET 原生驾驭 AI 新基建实战系列(一):向量数据库的应用与畅想
· Ai满嘴顺口溜,想考研?浪费我几个小时
· Browser-use 详细介绍&使用文档
· 软件产品开发中常见的10个问题及处理方法