android 在线版本更新源码,全注释,4.0能跑!第一次写,请多指教。
2013-09-05 12:06
435 查看
//主线程 Activity public class Test_UpdateActivity extends Activity { private static final String TAG = "Update"; private Config config; private String jsonuri=""; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); NetworkTool myasyn=new NetworkTool(); myasyn.execute(Config.UPDATE_SERVER + Config.UPDATE_VERJSON); try { jsonuri=myasyn.get(); config=new Config(this); config.updateVer(jsonuri); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (ExecutionException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
* 检查版本更新帮助类 * @author Administrator * */
public class Config { private static final String TAG = "Config"; //下载地址(JSP服务器) public static final String UPDATE_SERVER = "http://192.168.1.107:8080/text_android/"; //下载文件名(在服务器上放置待下载的 apk 文件) public static final String UPDATE_APKNAME = "Hp2_2.0.apk"; //读取 JSON 文件(在服务器上放置待读取的 json 版本信息文件) public static final String UPDATE_VERJSON = "ver.json"; //下载后保存的文件名 public static final String UPDATE_SAVENAME = "Hp2_2.0.apk"; //下载进度条 public ProgressDialog pBar; private Handler handler = new Handler(); //新版本号 private int newVerCode = 0; //新版本名 private String newVerName = ""; private Context context; public Config(Context context){ this.context=context; } /** * 开始版本检查更新 */ public void updateVer(String jsonuri){ boolean json=getServerVerCode(jsonuri); if (json) { //获取当前版本号 int vercode = getVerCode(context); //判断新版本号是否大于当前版本 if (newVerCode > vercode) { doNewVersionUpdate(); } else { notNewVersionShow(); } } } /** * 获取当前版本号 * @param context 传入上下文 * @return 版本号 */ public static int getVerCode(Context context) { int verCode = -1; try { //"com.haiyue.test" 为系统内已存在的应用包名 verCode = context.getPackageManager().getPackageInfo( "com.haiyue.test", 0).versionCode; } catch (NameNotFoundException e) { Log.e(TAG, e.getMessage()); } return verCode; } /** * 获取当前版本名 * @param context 传入上下文 * @return 版本名 */ public static String getVerName(Context context) { String verName = ""; try { //"com.haiyue.test" 为系统内已存在的应用包名 verName = context.getPackageManager().getPackageInfo( "com.haiyue.test", 0).versionName; } catch (NameNotFoundException e) { Log.e(TAG, e.getMessage()); } return verName; } /** * 获取当前 app 名 * @param context 传入上下文 * @return app 名 */ public static String getAppName(Context context) { String verName = context.getResources().getText(R.string.app_name) .toString(); return verName; } /** * 是否能获取版本号 * @return */ private boolean getServerVerCode(String jsonuri) { try { //解析 JSON 数组 JSONArray array = new JSONArray(jsonuri); if (array.length() > 0) { JSONObject obj = array.getJSONObject(0); //获取 JSON 对象 try { //获取 JSON 对象内容:新版本号 newVerCode = Integer.parseInt(obj.getString("verCode")); //获取 JSON 对象内容:新版本名 newVerName = obj.getString("verName"); } catch (Exception e) { newVerCode = -1; newVerName = ""; return false; } } } catch (Exception e) { Log.e(TAG, e.getMessage()); return false; } return true; } /** * 没有新版本显示信息 */ private void notNewVersionShow() { int verCode = getVerCode(context); String verName = getVerName(context); StringBuffer sb = new StringBuffer(); sb.append("当前版本:"); sb.append(verName); sb.append(" Code:"); sb.append(verCode); sb.append(",\n已是最新版,无需更新!"); Dialog dialog = new AlertDialog.Builder(context) .setTitle("软件更新").setMessage(sb.toString())// 设置内容 .setPositiveButton("确定",// 设置确定按钮 new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { dialog.dismiss(); } }).create();// 创建 // 显示对话框 dialog.show(); } /** * 当新版本号大于当前版本执行方法 */ private void doNewVersionUpdate() { //获取当前版本号s int verCode = getVerCode(context); //获取当前版本名 String verName = getVerName(context); //编辑字符串:"当前版本:** Code:**,发现新版本:** Code:**,是否更新?" StringBuffer sb = new StringBuffer(); sb.append("当前版本:"); sb.append(verName); sb.append(" Code:"); sb.append(verCode); sb.append(", 发现新版本:"); sb.append(newVerName); sb.append(" Code:"); sb.append(newVerCode); sb.append(", 是否更新?"); //弹出 Dialog Dialog dialog = new AlertDialog.Builder(context) .setTitle("软件更新") .setMessage(sb.toString()) // 设置内容 .setPositiveButton("更新",// 设置确定按钮 new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { //新建下载进度条 pBar = new ProgressDialog( context); pBar.setTitle("正在下载"); pBar.setMessage("请稍候..."); pBar.setProgressStyle(ProgressDialog.STYLE_SPINNER); downFile(Config.UPDATE_SERVER + Config.UPDATE_APKNAME); } }) .setNegativeButton("暂不更新", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int whichButton) { // 点击"取消"按钮之后退出程序 dialog.dismiss(); } }).create();// 创建 // 显示对话框 dialog.show(); } /** * 下载 apk 文件 * @param url 网址 */ void downFile(final String url) { pBar.show(); //开启新线程 new Thread() { public void run() { //新建网络连接 HttpClient client = new DefaultHttpClient(); //发送请求 HttpGet get = new HttpGet(url); HttpResponse response; try { response = client.execute(get); //代表接收的http消息,服务器返回的消息都在httpEntity HttpEntity entity = response.getEntity(); //long length = entity.getContentLength(); //新建输入流 InputStream is = entity.getContent(); //输出流 FileOutputStream fileOutputStream = null; //判断输入流是否为空 if (is != null) { //新建文件 //Environment.getExternalStorageDirectory() SD卡路径 File file = new File( Environment.getExternalStorageDirectory(), Config.UPDATE_SAVENAME); //新建输出流 fileOutputStream = new FileOutputStream(file); //新建 byte 数组 byte[] buf = new byte[1024]; int ch = -1; //int count = 0; //循环写入文件 while ((ch = is.read(buf)) != -1) { fileOutputStream.write(buf, 0, ch); /*count += ch; if (length > 0) { }*/ } } //把缓冲区的数据强行输出, 主要用在IO中,即清空缓冲区数据 //更新写出的结果 fileOutputStream.flush(); //关闭输出流 if (fileOutputStream != null) { fileOutputStream.close(); } down(); } catch (ClientProtocolException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } }.start(); } /** * 下载文件 */ void down() { //消息 handler.post(new Runnable() { public void run() { pBar.cancel(); update(); } }); } /** * 安装文件 */ void update() { Intent intent = new Intent(Intent.ACTION_VIEW); //setDataAndType 打开各种文件 intent.setDataAndType(Uri.fromFile(new File(Environment .getExternalStorageDirectory(), Config.UPDATE_SAVENAME)), "application/vnd.android.package-archive"); context.startActivity(intent); } }/** * 异步任务类 * @author Administrator * */
public class NetworkTool extends AsyncTask<String, Void, String> { @Override protected String doInBackground(String... params) { StringBuilder sb = new StringBuilder(); // TODO Auto-generated method stub HttpClient client = new DefaultHttpClient(); // 创建 HttpParams 以用来设置 HTTP 参数(这一部分不是必需的) HttpParams httpParams = client.getParams(); // 设置网络超时参数 // 连接超时 HttpConnectionParams.setConnectionTimeout(httpParams, 3000); // Socket 超时 HttpConnectionParams.setSoTimeout(httpParams, 5000); // Get 请求 HttpGet get = new HttpGet(params[0]); HttpResponse response; try { response = client.execute(get); // 代表接收的http消息,服务器返回的消息都在httpEntity HttpEntity entity = response.getEntity(); if (entity != null) { // 设置字符集 BufferedReader reader = new BufferedReader( new InputStreamReader(entity.getContent(), "UTF-8"), 8192); String line = null; // 循环读取行 while ((line = reader.readLine()) != null) { sb.append(line + "\n"); } // 关闭读取 reader.close(); } } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } return sb.toString(); } }
相关文章推荐
- 关于android客户端在线版本更新的总结(json源码)
- (android实战)应用在线版本更新
- Android不同版本统计数据更新:Android 4.0份额为0.6%
- (android实战)应用在线版本更新
- 一个提供Android各历史版本在线源码的网址
- 使用清华大学开源软件镜像AOSP的“每月更新初始化包”更新指定版本的Android源码
- 一个在线看android系统各个版本源码的链接
- 【Android】两步搞定AndroidSDKManager在线更新SDK版本失败问题
- Android 各个版本源码在线看
- android 软件在线版本更新功能的实现
- (android实战)应用在线版本更新
- [androd] android的在线源码网站,各个版本都有(目前已到俺android 4.2,但不包含kernel部分的代码)
- android 版本在线更新
- Android各个版本源码——在线阅读
- android:versionCode、android:versionName之实现第一次登陆、版本更新
- Android应用检查版本与更新源码
- Android版本更新教程和源码
- 一个提供Android各历史版本在线源码的网址
- [androd] android的在线源码网站,各个版本都有(目前已到俺android 4.2,但不包含kernel部分的代码)
- [androd] android的在线源码网站,各个版本都有(目前已到俺android 4.2,但不包含kernel部分的代码)