安卓实现在线更新

////////////////////2016/04/25//////////////////////

///////////////////by XBW///////////////////////////

//////////////////环境 eclipse api22///////////

做点贡献,发几个小demo,每次启动某个app都会遇到app升级,这是怎么实现的呢,先上图吧,

有图有真相,

这截图真大,没睡了,

结构。

Config.java

package com.example.update;

public class Config {
public static String mysql_url_update="http://127.0.0.1/androidapp/appupdate/update.txt";//服务器端更新url;
}

FileUtil.java

package com.example.update;
import java.io.File;
import java.io.IOException;
import android.os.Environment;

public class FileUtil {

	public static File updateDir = null;
	public static File updateFile = null;
	public static final String Application = "konkaUpdateApplication";

	public static boolean isCreateFileSucess;
	public static void createFile(String app_name) {

		if (android.os.Environment.MEDIA_MOUNTED.equals(android.os.Environment.getExternalStorageState())) {
			isCreateFileSucess = true;

			updateDir = new File(Environment.getExternalStorageDirectory()+ "/" +Application +"/");
			updateFile = new File(updateDir + "/" + app_name + ".apk");

			if (!updateDir.exists()) {
				updateDir.mkdirs();
			}
			if (!updateFile.exists()) {
				try {
					updateFile.createNewFile();
				} catch (IOException e) {
					isCreateFileSucess = false;
					e.printStackTrace();
				}
			}

		}else{
			isCreateFileSucess = false;
		}
	}
}

GetVersion.java

package com.example.update;

import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;

public class GetVersion {

	// 获取当前版本的版本号
		public String getVersion(Context context) {
			try {
				PackageManager packageManager = context.getPackageManager();
				PackageInfo packageInfo = packageManager.getPackageInfo(
						context.getPackageName(), 0);
				return packageInfo.versionName;
			} catch (NameNotFoundException e) {
				e.printStackTrace();
				return "版本号未知";
			}
		}
}

IsNeedUpdate.java

package com.example.update;

import android.content.Context;

public class IsNeedUpdate {

	private UpdateInfo info;
	public IsNeedUpdate(Context context){
		UpdateInfoService updateInfoService = new UpdateInfoService(
				context);
		try {
			info = updateInfoService.getUpDateInfo();
		} catch (Exception e) {
			// TODO 自动生成?? catch ??
			e.printStackTrace();
		}
	}
	public boolean isNeedUpdate(Context context) {
		 GetVersion version=new GetVersion();
		 //版本更新检测使用浮点型存在小版本问题,浮点型不能识别v 1.2.1这种小版本
		 //double webVersion=Double.parseDouble(info.getVersion());
		// double localVersion=Double.parseDouble(version.getVersion(context));
		 //采用比较字典序大小检测版本更新
		if (info.getVersion().compareTo(version.getVersion(context))>0) {
			return true;
		} else {
			return false;
		}
	}
	public String getDescribe(){

		return info.getDescription();
	}
	public String getUrl(){

		return info.getUrl();
	}
}

ShowUpdateDialog.java

package com.example.update;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.os.Environment;
import android.text.method.ScrollingMovementMethod;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

public class ShowUpdateDialog{
	private UpdateInfo info;
	//更新窗口
	public Dialog showUpdateDialog(final Context context,String msg,final String url) {
		LayoutInflater layoutInflater =LayoutInflater.from(context);
		RelativeLayout layout = (RelativeLayout)layoutInflater.inflate(R.layout.dialog, null );
		final Dialog dialog = new AlertDialog.Builder(context).create();
	    dialog.show();
	    dialog.getWindow().setContentView(layout);
	    TextView tex=(TextView)layout.findViewById(R.id.dialog_text);
	    TextView tex1=(TextView)layout.findViewById(R.id.textView_title);
	    tex.setMovementMethod(ScrollingMovementMethod.getInstance());
	    tex.setText(msg);
	    tex1.setText("更新提示");
         //确定按钮
         Button btnOK = (Button) layout.findViewById(R.id.dialog_ok);
         btnOK.setOnClickListener(new OnClickListener() {
           @Override
           public void onClick(View v) {
        	   if (Environment.getExternalStorageState().equals(
						Environment.MEDIA_MOUNTED)) {
        		   		//services.service();
        		   Intent intent = new Intent(context,UpdateServices.class);
        		   intent.putExtra("Key_App_Name",context.getString(R.string.app_name));
        		   intent.putExtra("Key_Down_Url",url);
        		   context.startService(intent);
				} else {
					Toast.makeText(context, "SD卡不可用,请插入SD卡",
							Toast.LENGTH_SHORT).show();
				}
        	   dialog.dismiss();
           }
         });
         //关闭按钮
         ImageButton btnClose = (ImageButton) layout.findViewById(R.id.dialog_close);
         btnClose.setOnClickListener(new OnClickListener() {
           @Override
           public void onClick(View v) {
              dialog.dismiss();
           }
         });
         return dialog;
     }
	//不更新窗口
	public Dialog showDialog(final Context context) {
		LayoutInflater layoutInflater =LayoutInflater.from(context);
		RelativeLayout layout = (RelativeLayout)layoutInflater.inflate(R.layout.dialog, null );
		final Dialog dialog = new AlertDialog.Builder(context).create();
	    dialog.show();
	    dialog.getWindow().setContentView(layout);
	    TextView tex=(TextView)layout.findViewById(R.id.dialog_text);
	    TextView tex1=(TextView)layout.findViewById(R.id.textView_title);
	    GetVersion version=new GetVersion();
	    tex.setText("您使用的是最新版:"+version.getVersion(context)+"版本");
	    tex1.setText("更新提示");
	    Button btnOK = (Button) layout.findViewById(R.id.dialog_ok);
	    btnOK.setVisibility(View.INVISIBLE);
	  //关闭按钮
         ImageButton btnClose = (ImageButton) layout.findViewById(R.id.dialog_close);
         btnClose.setOnClickListener(new OnClickListener() {
           @Override
           public void onClick(View v) {
              dialog.dismiss();
           }
         });
         return dialog;
	}

}

UpdateInfo.java

package com.example.update;

public class UpdateInfo
{
        private String version;
        private String description;
        private String url;
        public String getVersion()
        {
                return version;
        }
        public void setVersion(String version)
        {
                this.version = version;
        }
        public String getDescription()
        {
                return description;
        }
        public void setDescription(String description)
        {
                this.description = description;
        }
        public String getUrl()
        {
                return url;
        }
        public void setUrl(String url)
        {
                this.url = url;
        }
}

UpdateInfoService.java

package com.example.update;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import android.content.Context;

public class UpdateInfoService {
	public UpdateInfoService(Context context) {
	}

	public UpdateInfo getUpDateInfo() throws Exception {
		String path = Config.mysql_url_update;
		StringBuffer sb = new StringBuffer();
		String line = null;
		BufferedReader reader = null;
		UpdateInfo updateInfo = new UpdateInfo();
		try {
			// 创建??个url对象
			URL url = new URL(path);
			// 通過url对象,创建一个HttpURLConnection对象(连接)
			HttpURLConnection urlConnection = (HttpURLConnection) url
					.openConnection();
			// 通过HttpURLConnection对象,得到InputStream
			reader = new BufferedReader(new InputStreamReader(
					urlConnection.getInputStream()));
			// 使用io流读取文??
			while ((line = reader.readLine()) != null) {
				sb.append(line);

			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				if (reader != null) {
					reader.close();
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
		}

		String info = sb.toString();
		updateInfo.setVersion(info.split("&")[1]);
		updateInfo.setDescription(info.split("&")[2]);
		updateInfo.setUrl(info.split("&")[3]);
		return updateInfo;
	}

}

才发现csdn博客还有长度限制,发了好几次都被截断了,这样吧,

直接传送demo,或者去我小站看一下吧

demo传送门

小站

小站demo下载不需要积分哦

				
时间: 2024-08-02 19:15:48

安卓实现在线更新的相关文章

安卓 SDK升级后模拟器闪退

我们使用的是intel加速模拟器,但sdk更新后却不能使用,因为intel的HAXM虚拟程序没有更新,虽然在线更新包说已经Installed,但自己要亲自到根目录找到安装包并安装. 安卓sdk在线更新,由于谷歌的那边连不上,就链接国内的镜像,具体教程可看这网站http://www.androiddevtools.cn/,修改sdk Mannager,如图下 接着修复配置文件,重新打开 因为我们使用的是intel加速模拟器,虽然 在Extra下载了,但未安装,因为要进入sdk目录下的sdk\ext

安卓下cocos2dx实现cpp部分在线更新

目前cocos2dx + lua的方式可以动态更新所有的脚本文件,但是却无法动态更新cpp部分的代码(这部分东西一般我们称之为底包),事实上采用同样的方案在android下也是可以实现动态更新的,只需要将java代码里加载libgame.so的代码稍微做些修改即可 static { System.loadLibrary("game"); } 将上面的代码移到onCreate里大概如下 protected void onCreate(Bundle savedInstanceState)

Eclipse+ADT+Android SDK 搭建安卓开发环境

要求 必备知识 windows 7 基本操作. 运行环境 windows 7 下载地址 环境下载 最近开接触Android(安卓)嵌入式开发,首要问题是搭建Andoid开发环境,由于本人用的是windows7的笔记本,也就只能到Windows中搭建Android 开发环境了! 就搭建环境都花了比较长的时间, 在各种版本之间折腾了比较久的时间, 装好后SDK包更新又是一个比较大的麻烦(天朝的网络大家懂的--).下面把我的安装过程和经验分享个大家!! 安装JDK 这里可以参考我之前写的一篇关于安装J

Android环境搭建-安卓开发系列教程(原创)

[面向人员]:立志于安卓开发的所有人员,做到真正的从零基础起步: [教程说明]:本教程在内容上保证原创.简洁.详细,在附件链接上保证可用.最新.完整: [回帖说明]:教程有不清晰的地方,请及时回帖互动,楼主会第一时间更新帖子,谨以此开源.分享.共进. 1.下载最新版JDK(JDK是运行环境) 官方下载地址:    http://www.oracle.com/technetwor ... nloads-2133151.html 小提示:甲骨文官方下载速度比较慢,请耐心等待 网盘下载地址:http:

Android SDK:Android standard develop kits 安卓开发的工具集

目前主流的安卓开发工具: 1.Adnroid-Adt-bundle SDK Manager.exe: Tools(安卓的开发小工具) 各种安卓版本 Extras 额外的开发包 在线更新/安装的安卓版本的更新工具 不提倡在SDK Manager在线更新安卓版本.sdk: 主要用来存储安卓开发的环境 .android:虚拟机创建的文件 add-ons 存储google的一些API 平时很少 基本没使用 存储google地图 google paly 代码 build-tools 编译工具 docs 安

android源码大放送(实战开发必备),免费安卓demo源码,例子大全文件详细列表

免费安卓demo源码,例子大全文件详细列表 本列表源码永久免费下载地址:http://www.jiandaima.com/blog/android-demo 卷 yunpan 的文件夹 PATH 列表 卷序列号为 0000-73EC E:. │ jiandaima.com文件列表生成.bat │ 例子大全说明.txt │ 本例子永久更新地址~.url │ 目录列表2016.03.10更新.txt │ ├─前台界面 │ ├─3D标签云卡片热门 │ │ Android TagCloudView云标签

安卓开发黄金搭档:android-studio+Genymotion模拟器

最近换了电脑,重新配置安卓开发环境,记录一下: 早期研究安卓开发,用eclipse觉得挺臃肿庞大,后来出了android-studio,等版本渐渐稳定下来用起来还不错,但是还是比较难忍受AVD模拟器的缓慢启动速度,又不喜欢一直用真机联调,后来发现Genymotion,立马有种拨开云雾见月明的感觉,启动快,即有真机速度,又适配多种机型.配置步骤如下: 一.安装android-studio android-studio不用说了,出了多个版本,现在最新的是0.8.9,可以在下面网址下载: https:

【原创】开发第一个安卓程序(教程系列贴)

[面向人员]:立志于安卓开发的所有人员,做到真正的从零基础起步: [教程说明]:本教程在内容上保证原创.简洁.详细,在附件链接上保证可用.最新.完整: [回帖说明]:教程有不清晰的地方,请及时回帖互动,楼主会第一时间更新帖子,谨以此开源.分享.共进. 1.通过代理在线更新SDK 依次打开安卓001教程建立的开发文件夹,android/develope,找到并双击SDK Manager.exe可执行文件 稍等片刻,自动弹出Android SDK Manager Log窗口,窗口内未配置代理钱会显示

安卓安装提示:Android SDK requires Android Developer Toolkit version 21.1.0 or above. (错误解决方法)

安卓安装提示:Android SDK requires Android Developer Toolkit version 21.1.0 or above.  (错误解决方法) 主要是由于版本不对,sdk和adt的版本. 解决思路: 打开Eclipse--帮助(help) -- Install Now Software -- Add按钮 Name:随便输入 下面的文本框输入 :http://dl-ssl.google.com/android/eclipse/ 在线更新就行了,超慢呀! =====