android > APK更新
16lz
2021-12-04
package t1.com;import java.io.BufferedInputStream;import java.io.ByteArrayOutputStream;import java.io.File;import java.io.FileOutputStream;import java.io.InputStream;import java.net.HttpURLConnection;import java.net.URL;import java.text.SimpleDateFormat;import java.util.ArrayList;import java.util.Calendar;import java.util.Date;import java.util.HashMap;import org.apache.http.client.HttpClient;import android.R.anim;import android.app.Activity;import android.app.AlertDialog;import android.app.AlertDialog.Builder;import android.app.ProgressDialog;import android.content.DialogInterface;import android.content.Intent;import android.content.SharedPreferences;import android.net.Uri;import android.os.Bundle;import android.os.Environment;import android.os.Handler;import android.os.Message;import android.widget.EditText;import android.widget.ListView;import android.widget.Toast;public class T1Activity extends Activity { protected static final int UPDATA_CLIENT = 0;protected static final int CHECK_UPDATE = 1;protected static final int DOWN_ERROR = 0;/** Called when the activity is first created. */ private int globalVersion = 3; private int serverVersion; private String serverVersionUrl = "http://cloud.broadlink.com.cn/2/switch_version.html"; private String downLoadApkUrl = "http://cloud.broadlink.com.cn/2/t1.apk"; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); ////检测更新------------------------- new Thread(new Runnable() { public void run() { try {serverVersion = 33; Message msg = new Message(); msg.what = CHECK_UPDATE; handler.sendMessage(msg); } catch (NumberFormatException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (Exception e) {// TODO Auto-generated catch blocke.printStackTrace();} } }).start(); } public void showUpdateDialog() { @SuppressWarnings("unused") AlertDialog alert = new AlertDialog.Builder(T1Activity.this).setTitle("更新提示") .setIcon(android.R.drawable.ic_dialog_info).setMessage("检测到新的版本,请下载更新").setPositiveButton("下载",new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog,int which) { dialog.dismiss(); //关闭对话框 downLoadApk(); System.out.println("start update"); } }).show(); } protected void downLoadApk() { final ProgressDialog pd; //进度条对话框 pd = new ProgressDialog(this); pd.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); pd.setMessage("正在下载更新"); pd.show(); new Thread(){ @Override public void run() { try { File file = getFileFromServer(downLoadApkUrl, pd); sleep(3000); installApk(file); pd.dismiss(); //结束掉进度条对话框 } catch (Exception e) { pd.dismiss(); Message msg = new Message(); msg.what = DOWN_ERROR; handler.sendMessage(msg); e.printStackTrace(); } }}.start(); } public static File getFileFromServer(String path, ProgressDialog pd) throws Exception{ //如果相等的话表示当前的sdcard挂载在手机上并且是可用的 if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){ URL url = new URL(path); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setConnectTimeout(5000); //获取到文件的大小 pd.setMax(conn.getContentLength()); InputStream is = conn.getInputStream(); File file = new File(Environment.getExternalStorageDirectory(), "updata.apk"); FileOutputStream fos = new FileOutputStream(file); BufferedInputStream bis = new BufferedInputStream(is); byte[] buffer = new byte[1024]; int len ; int total=0; while((len =bis.read(buffer))!=-1){ fos.write(buffer, 0, len); total+= len; //获取当前下载量 pd.setProgress(total); } fos.close(); bis.close(); is.close(); return file; } else{ return null; } } //安装apk protected void installApk(File file) { Intent intent = new Intent(); //执行动作 intent.setAction(Intent.ACTION_VIEW); //执行的数据类型 intent.setDataAndType(Uri.fromFile(file), "application/vnd.android.package-archive"); startActivity(intent); } Handler handler = new Handler(){ @Override public void handleMessage(Message msg) { // TODO Auto-generated method stub super.handleMessage(msg); switch (msg.what) { case DOWN_ERROR: //下载apk失败 Toast.makeText(getApplicationContext(), "下载新版本失败", 1).show(); break; case CHECK_UPDATE: //下载apk失败 if(serverVersion == 0) { serverVersion = globalVersion; } if(serverVersion != globalVersion){ showUpdateDialog(); } break; } } }; }
更多相关文章
- Android更新UI的五种方式
- Android(安卓)Studio - Session 'app': Error launching activit
- android 下载程序
- android进度条对话框
- Android(安卓)studio 多线程网络文件下载
- Android源代码下载
- adb最新版下载地址
- ch07 Android(安卓)日期与时间对话框
- 安装包更新