服务器端设计:
设计方法应该有很多,下面介绍我的一种方法:
a.首先在服务器项目下建立一个文件夹来存放APK安装文件:
b.其次在src下建立一个资源文件,apkVersion.properties,属性定义如下:
view plaincopy to clipboardprint?
apkVersion=1 存版本号
apkSize=550kb 大小
apkPath=http://xx8080/srv/apk/Demo.apk 升级文件
apkVersion=1 存版本号
apkSize=550kb 大小
apkPath=http://xx8080/srv/apk/Demo.apk 升级文件
c.定义一个servlet来获取资源中的信息:
定义类:UpdateApkServlet.java
view plaincopy to clipboardprint?
//获取资源文件信息
static {
Properties ppt = new Properties();
try {
ppt.load(UpdateApkServlet.class .getResourceAsStream("/apkVersion.properties"));
apkVersion = ppt.getProperty("apkVersion");
apkSize = ppt.getProperty("apkSize");
apkPath = ppt.getProperty("apkPath"); }
catch (Exception e) {
e.printStackTrace();
}
}
//获取资源文件信息
static {
Properties ppt = new Properties();
try {
ppt.load(UpdateApkServlet.class .getResourceAsStream("/apkVersion.properties"));
apkVersion = ppt.getProperty("apkVersion");
apkSize = ppt.getProperty("apkSize");
apkPath = ppt.getProperty("apkPath"); }
catch (Exception e) {
e.printStackTrace();
}
}
获取资源,然后生成JSON字串返回客户端处理。 注:当客户端版本有更新,服务器端只要把APK文件拷贝到APK目录,然后更新apkVersion.properties文件中的信息就可以了,切记。
客户端设计:
1、 客户端首先获取服务器的版本信息(http方式获取)。
2、 如何获取本地客户端的版本信息 如下参考代码:
view plaincopy to clipboardprint?
/**
* 得到本地应用的版本信息
* @return
*/
private int getAPKVersion(){
//APK版本判断
int sdcardVersion = 0;
String apkFilePath="sdcard/demo.apk";//安装包路径
PackageManager pm = getPackageManager();

PackageInfo info = pm.getPackageArchiveInfo(apkFilePath, PackageManager.GET_ACTIVITIES);
if(info != null){
sdcardVersion=info.versionCode; //得到版本信息
Log.v(TAG, "Version="+sdcardVersion);
}
return sdcardVersion;
}
/**
* 得到本地应用的版本信息
* @return
*/
private int getAPKVersion(){
//APK版本判断
int sdcardVersion = 0;
String apkFilePath="sdcard/demo.apk";//安装包路径
PackageManager pm = getPackageManager();

PackageInfo info = pm.getPackageArchiveInfo(apkFilePath, PackageManager.GET_ACTIVITIES);
if(info != null){
sdcardVersion=info.versionCode; //得到版本信息
Log.v(TAG, "Version="+sdcardVersion);
}
return sdcardVersion;
}
3、 版本比较,如果版本相同,则不执行更新,不同才进行更新操作。 这里插入客户端版本设置介绍: 客户端版本设置在AndroidManifest.xml文件中,里面有两个属性可进行版本信息设置, android:versionCode="1" 版本号 android:versionName="1.1" 版本名称 这个版本号需要和服务器端对应。
4、 需要的权限设置
view plaincopy to clipboardprint?
Sdcard访问权限: uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"
访问网络权限: uses-permission android:name="android.permission.INTERNET"
Sdcard访问权限: uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"
访问网络权限: uses-permission android:name="android.permission.INTERNET"
5、 更新安装 当用户点击应用时执行检查更新。相关代码参考:
//弹出框提示
view plaincopy to clipboardprint?
public Handler handler = new Handler() {
public void handleMessage(Message msg) {
super.handleMessage(msg);
Dialog dialog = new AlertDialog.Builder(MainActivity.this).setTitle("系统更新").setMessage("发现新版本,请更新!")
// 设置内容
.setPositiveButton("确定",// 设置确定按钮
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
pBar = new ProgressDialog(MainActivity.this);
pBar.setTitle("正在下载");
pBar.setMessage("请稍候...");
pBar.setProgressStyle(ProgressDialog.STYLE_SPINNER);
downFile(apkPath);
}
}).setNegativeButton("取消", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
// 点击"取消"按钮操作
}
}).create();// 创建
// 显示对话框
dialog.show();
}
};
public Handler handler = new Handler() {
public void handleMessage(Message msg) {
super.handleMessage(msg);
Dialog dialog = new AlertDialog.Builder(MainActivity.this).setTitle("系统更新").setMessage("发现新版本,请更新!")
// 设置内容
.setPositiveButton("确定",// 设置确定按钮
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
pBar = new ProgressDialog(MainActivity.this);
pBar.setTitle("正在下载");
pBar.setMessage("请稍候...");
pBar.setProgressStyle(ProgressDialog.STYLE_SPINNER);
downFile(apkPath);
}
}).setNegativeButton("取消", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
// 点击"取消"按钮操作
}
}).create();// 创建
// 显示对话框
dialog.show();
}
};
//下载
view plaincopy to clipboardprint?
/**
* DOWNLOAD APK FILE BY URL
* @param url
*/
public void downFile(final String url) {
pBar.show();
new Thread() {
public void run() {
HttpClient client = new DefaultHttpClient();
// params[0]代表连接的url
HttpGet get = new HttpGet(url);
HttpResponse response;
try {
response = client.execute(get);
HttpEntity entity = response.getEntity();
long length = entity.getContentLength();
InputStream is = entity.getContent();
FileOutputStream fileOutputStream = null;
if (is != null) {
File file = new File(Environment.getExternalStorageDirectory(),"demo.apk");
fileOutputStream = new FileOutputStream(file);
byte[] buf = new byte[1024];
int ch = -1;
int count = 0;
while ((ch = is.read(buf)) != -1) {
// baos.write(buf, 0, ch);
fileOutputStream.write(buf, 0, ch);
count += ch;
if (length > 0) {
}
}
}
fileOutputStream.flush();
if (fileOutputStream != null) {
fileOutputStream.close();
}
down();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}.start();
}
public void down() {
handler.post(new Runnable() {
public void run() {
pBar.cancel();
update();
}
});
}
/**
* DOWNLOAD APK FILE BY URL
* @param url
*/
public void downFile(final String url) {
pBar.show();
new Thread() {
public void run() {
HttpClient client = new DefaultHttpClient();
// params[0]代表连接的url
HttpGet get = new HttpGet(url);
HttpResponse response;
try {
response = client.execute(get);
HttpEntity entity = response.getEntity();
long length = entity.getContentLength();
InputStream is = entity.getContent();
FileOutputStream fileOutputStream = null;
if (is != null) {
File file = new File(Environment.getExternalStorageDirectory(),"demo.apk");
fileOutputStream = new FileOutputStream(file);
byte[] buf = new byte[1024];
int ch = -1;
int count = 0;
while ((ch = is.read(buf)) != -1) {
// baos.write(buf, 0, ch);
fileOutputStream.write(buf, 0, ch);
count += ch;
if (length > 0) {
}
}
}
fileOutputStream.flush();
if (fileOutputStream != null) {
fileOutputStream.close();
}
down();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}.start();
}
public void down() {
handler.post(new Runnable() {
public void run() {
pBar.cancel();
update();
}
});
}
//更新升级
view plaincopy to clipboardprint?
public void update() {
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(new File("/sdcard/demo.apk")),"application/vnd.android.package-archive");
startActivity(intent);
}
public void update() {
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(new File("/sdcard/demo.apk")),"application/vnd.android.package-archive");
startActivity(intent);
}

更多相关文章

  1. 【Android】解决FloatMath类中方法在API 23以后不存在问题
  2. Android(安卓)手机 Google Play 商店“从服务器检索信息时出错 [
  3. Android(安卓)Widget点击事件
  4. Android如何获取当前应用版本号?
  5. Android(安卓)SDK Manager软件包下载安装相关问题的解决办法
  6. android 自定义注解处理器
  7. 不同版本的TLS在Android中的支持情况
  8. app包下的fragment 和 v4 包下的fragment的区别 以及兼容包suppo
  9. Mac下Android(安卓)Studio 使用git版本控制个人心得

随机推荐

  1. android点击本地缩略图,弹出显示服务器原
  2. Android中的进程与多线程的讲解(Handler和
  3. Android(安卓)API Guides---Data Storage
  4. android 自定义圆形进度条(一)
  5. iOS中的抽屉菜单
  6. Android如何快速实现打渠道打包
  7. Android客户端与服务器端数据同步
  8. Android(安卓)显示GIF图片实例详解
  9. Android中弹窗中带有Edittext,软键盘遮挡
  10. 记一次Build.gradle引发的ClassNotFound