Android APK下载与安装流程
Android 应用的自动更新,多时候都绕不开直接下载 APK 触发安装流程这一步。挺多项目里用这套方案来应对不走商店的更新需求,比较灵活。
APK 其实就是 Android 的安装包,类似 Windows 的 exe。你得先从网络上把它拉下来,用HttpURLConnection就能搞定,用OkHttp会更舒服点。下载完后一般会存到/sdcard/download这种目录里。
下面这段代码,用 HttpURLConnection 撸了一个简版下载器:
URL url = new URL("http://example.com/your.apk");
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
connection.setConnectTimeout(5000);
connection.setReadTimeout(5000);
FileOutputStream fos = new FileOutputStream("/sdcard/download/your.apk");
BufferedInputStream bis = new BufferedInputStream(connection.getInputStream());
byte[] buffer = new byte[1024];
int length;
while ((length = bis.read(buffer)) != -1) {
  fos.write(buffer, 0, length);
}
fos.flush();
fos.close();
bis.close();
connection.disconnect();别忘了加上WRITE_EXTERNAL_STORAGE权限,否则一跑就崩。
APK 下好了不能直接装,得用Intent拉起系统安装页面。用Intent.ACTION_VIEW,指定 MIME 类型为application/vnd.android.package-archive,系统就知道你要干啥了:
Intent installIntent = new Intent(Intent.ACTION_VIEW);
installIntent.setDataAndType(Uri.fromFile(new File("/sdcard/download/your.apk")),
    "application/vnd.android.package-archive");
installIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(installIntent);Android 6.0 以后要动态申权限,用ActivityCompat.requestPermissions()搞定就行。
,这一套流程挺实用,尤其适合做自定义更新的场景。如果你项目里有不通过市场发版的需求,这种方式就派上用场了。
                            
                            Android中下载apk文件并安装
                            预估大小:1023个文件
                        
                        
                        
                                
                                last-build.bin
                                1B
                            
                                                        
                                
                                fileSnapshots.bin
                                1.1MB
                            
                                                        
                                
                                taskHistory.bin
                                52KB
                            
                                                        
                                
                                classAnalysis.bin
                                1.41MB
                            
                                                        
                                
                                jarAnalysis.bin
                                626KB
                            
                                                        
                                
                                taskJars.bin
                                20KB
                            
                                                        
                                
                                taskHistory.bin
                                58KB
                            
                                                        
                                
                                fileHashes.bin
                                239KB
                            
                                                        
                                
                                resourceHashesCache.bin
                                20KB
                            
                                                        
                                
                                gradlew.bat
                                2KB
                            
                                                        
                                                        
                                                        
                                                        
                                                        
                                                        
                                                        
                                                        
                                                        
                                                        
                            
                            
                                                        
                                        
                                    文件大小:10.59MB
                                
                                
                                
                            
评论区