您好,登錄后才能下訂單哦!
本篇文章是直接下載最新的APK安裝的方法,并不是增量下載該APk。
想要實現(xiàn)一個android應用,自動更新下載APK軟件的方法,我采取的是以下幾步方法:
1.每次進入主界面時,獲取服務器的數(shù)據(jù),看是否是最新版本,是,則無操作,否,則進行以下步驟;
2.彈出是否更新軟件的對話框,點擊下載后
3.彈出下載的進度條的對話框,開始下載,可以上隨時點擊按鈕,停止下載
4.下載完成后,調(diào)用系統(tǒng)安裝軟件的服務,安裝軟件
效果圖:
實現(xiàn)過程:
新建一個UpdateManager方法,具體內(nèi)容我已經(jīng)有詳細的注釋
package lgx.acc.updatedemo; import java.io.File; import java.io.FileOutputStream; import java.io.InputStream; import java.net.HttpURLConnection; import java.net.URL; import android.app.AlertDialog; import android.content.Context; import android.content.DialogInterface; import android.content.Intent; import android.net.Uri; import android.os.Handler; import android.view.LayoutInflater; import android.view.View; import android.widget.ProgressBar; import android.widget.TextView; public class UpdateManager { // 應用程序Context private Context mContext; // 是否是最新的應用,默認為false private boolean isNew = false; private boolean intercept = false; // 下載安裝包的網(wǎng)絡路徑 private String apkUrl = "http://shouji.360tpcdn.com/360sj/tpi/20130201/" + "com.flikie.wallpapers.gallery_4.apk"; // 保存APK的文件夾 private static final String savePath = "/sdcard/updatedemo/"; private static final String saveFileName = savePath + "UpdateDemoRelease.apk"; // 下載線程 private Thread downLoadThread; private int progress;// 當前進度 TextView text; // 進度條與通知UI刷新的handler和msg常量 private ProgressBar mProgress; private static final int DOWN_UPDATE = 1; private static final int DOWN_OVER = 2; public UpdateManager(Context context) { mContext = context; } /** * 檢查是否更新的內(nèi)容 */ public void checkUpdateInfo() { //這里的isNew本來是要從服務器獲取的,我在這里先假設他需要更新 if (isNew) { return; } else { showUpdateDialog(); } } /** * 顯示更新程序?qū)υ捒?,供主程序調(diào)用 */ private void showUpdateDialog() { AlertDialog.Builder builder = new AlertDialog.Builder(mContext); builder.setTitle("軟件版本更新"); builder.setMessage("有最新的軟件包,請下載!"); builder.setPositiveButton("下載", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { showDownloadDialog(); } }); builder.setNegativeButton("以后再說", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { dialog.dismiss(); } }); builder.create().show(); } /** * 顯示下載進度的對話框 */ private void showDownloadDialog() { AlertDialog.Builder builder = new AlertDialog.Builder(mContext); builder.setTitle("軟件版本更新"); LayoutInflater inflater = LayoutInflater.from(mContext); View v = inflater.inflate(R.layout.progress, null); mProgress = (ProgressBar) v.findViewById(R.id.progress); builder.setView(v); builder.setNegativeButton("取消", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { intercept = true; } }); builder.show(); downloadApk(); } /** * 從服務器下載APK安裝包 */ private void downloadApk() { downLoadThread = new Thread(mdownApkRunnable); downLoadThread.start(); } private Runnable mdownApkRunnable = new Runnable() { @Override public void run() { URL url; try { url = new URL(apkUrl); HttpURLConnection conn = (HttpURLConnection) url .openConnection(); conn.connect(); int length = conn.getContentLength(); InputStream ins = conn.getInputStream(); File file = new File(savePath); if (!file.exists()) { file.mkdir(); } File apkFile = new File(saveFileName); FileOutputStream fos = new FileOutputStream(apkFile); int count = 0; byte[] buf = new byte[1024]; while (!intercept) { int numread = ins.read(buf); count += numread; progress = (int) (((float) count / length) * 100); // 下載進度 mHandler.sendEmptyMessage(DOWN_UPDATE); if (numread <= 0) { // 下載完成通知安裝 mHandler.sendEmptyMessage(DOWN_OVER); break; } fos.write(buf, 0, numread); } fos.close(); ins.close(); } catch (Exception e) { e.printStackTrace(); } } }; /** * 安裝APK內(nèi)容 */ private void installAPK() { File apkFile = new File(saveFileName); if (!apkFile.exists()) { return; } Intent intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(Uri.parse("file://" + apkFile.toString()), "application/vnd.android.package-archive"); mContext.startActivity(intent); }; private Handler mHandler = new Handler() { public void handleMessage(android.os.Message msg) { switch (msg.what) { case DOWN_UPDATE: mProgress.setProgress(progress); break; case DOWN_OVER: installAPK(); break; default: break; } } }; }
還有progressBar.xml的具體代碼
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <ProgressBar android:id="@+id/progress" android:layout_width="fill_parent" android:layout_height="wrap_content" /> </LinearLayout>
之后在MainActivity的onCreate方法中,調(diào)用一下代碼即可
UpdateManager manager=new UpdateManager(MainActivity.this); manager.checkUpdateInfo();
一定要記得在manifest里面加權(quán)限哈,
<uses-permission android:name="android.permission.INTERNET"/> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
最后效果就出來了。
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持億速云。
免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。