android软件自动更新的实现步骤

本篇文章是直接下载最新的APK安装的方法,并不是增量下载该APk。

转载请注明出处:,谢谢

想要实现一个android应用,自动更新下载APK软件的方法,我采取的是以下几步方法:

1.每次进入主界面时,获取服务器的数据,,看是否是最新版本,是,则无操作,否,则进行以下步骤;

2.弹出是否更新软件的对话框,点击下载后

3.弹出下载的进度条的对话框,开始下载,可以上随时点击按钮,停止下载

4.下载完成后,调用系统安装软件的服务,安装软件

效果图:

实现过程:

新建一个UpdateManager方法,具体内容我已经有详细的注释

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 {// 应用程序Contextprivate Context mContext;// 是否是最新的应用,默认为falseprivate boolean isNew = false;private boolean intercept = false;// 下载安装包的网络路径private String apkUrl = ""+ "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;}/** * 检查是否更新的内容 */public void checkUpdateInfo() {//这里的isNew本来是要从服务器获取的,我在这里先假设他需要更新if (isNew) {return;} else {showUpdateDialog();}}/** * 显示更新程序对话框,供主程序调用 */private void showUpdateDialog() {AlertDialog.Builder builder = new AlertDialog.Builder(mContext);builder.setTitle("软件版本更新");builder.setMessage("有最新的软件包,请下载!");builder.setPositiveButton("下载", new DialogInterface.OnClickListener() {@Overridepublic void onClick(DialogInterface dialog, int which) {showDownloadDialog();}});builder.setNegativeButton("以后再说",new DialogInterface.OnClickListener() {@Overridepublic 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() {@Overridepublic 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() {@Overridepublic 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内容 */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的具体代码

以后我会去到很多很繁华或苍凉,

android软件自动更新的实现步骤

相关文章:

你感兴趣的文章:

标签云: