1、使用HttpURLConnection下載下傳并更新
1、下載下傳APK檔案
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
private File download() {
try {
URL url = new URL("要下載下傳apk檔案的路徑");
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setConnectTimeout(5000);
connection.setRequestMethod("GET");
InputStream inputStream = null;
Log.e("------->","connection.getResponseCode()"+connection.getResponseCode());
if (connection.getResponseCode() == 200) {
int fileSize = connection.getContentLength() / 1024;
Log.e("------->","fileSize"+fileSize);
progressDialog.setMax(fileSize);
int totla = 0;
inputStream = connection.getInputStream();
File file = new File(SAVE_PATH);
FileOutputStream fos = new FileOutputStream(file);
byte[] bytes = new byte[1024];
int len = 0;
while ((len = inputStream.read(bytes)) != -1) {
fos.write(bytes, 0, len);
totla += (len / 1024);
progressDialog.setProgress(totla);
}
//别忘了重新整理哈
fos.flush();
fos.close();
inputStream.close();
progressDialog.dismiss();
return file;
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
2、安裝apk檔案
Intent intent = new Intent();
intent.setAction("android.intent.action.VIEW");
intent.addCategory("android.intent.category.DEFAULT");
//下載下傳的apk檔案(download方法傳回的File檔案)
intent.setDataAndType(Uri.fromFile(file),"application/vnd.android.package-archive");
startActivity(intent);
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
2、使用DownLoadManager下載下傳并更新
利用這個DownLoadManager方法下載下傳,會以通知的方式顯示,是以應用到廣播來通知下載下傳完畢。
1、使用DownLoadManager下載下傳檔案,并将downLoadID存到本地
DownloadManager downloadManager = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);
DownloadManager.Request request = new DownloadManager.Request(Uri.parse(" 要更新的檔案的.apk的路徑"));
//.apk的存儲路徑 request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS,"haha.apk");
//通知欄是否顯示 request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
request.setTitle("通知欄上顯示的标題");
request.setDescription("通知欄的描述");
request.setMimeType("application/vnd.android.package-archive");
long downLoadId = downloadManager.enqueue(request);
//将downloadid存到本地
SharedPreferences sharedPreferences = getSharedPreferences("download_id.txt",MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putLong("downLoadId",downLoadId);
editor.commit();
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
2、定義廣播
public class ApkInstallReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
SharedPreferences sharedPreferences =context.getSharedPreferences("download_id.txt",MODE_PRIVATE);
long downloadApkId = sharedPreferences.getLong("downLoadId",0);
if (intent.getAction().equals(DownloadManager.ACTION_DOWNLOAD_COMPLETE)) {
installApk(context, downloadApkId);
}
}
private static void installApk(Context context, long downloadApkId) {
DownloadManager dManager = (DownloadManager) context.getSystemService(Context.DOWNLOAD_SERVICE);
Intent install = new Intent(Intent.ACTION_VIEW);
Uri downloadFileUri = dManager.getUriForDownloadedFile(downloadApkId);
if (downloadFileUri != null) {
install.setDataAndType(downloadFileUri, "application/vnd.android.package-archive");
//從廣播啟動一個Activity要加這句話
install.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(install);
} else {
}
}
}
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
3、在AndroidManifest中注冊廣播(兩種方式)
方式一:
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
方式二:
ApkInstallReceiver receiver = new ApkInstallReceiver();
IntentFilter intent = new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE);
registerReceiver(receiver, intent);
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
小奮鬥文章
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~