Android客户端升级(整理)

本文为个人整理而来,原地址排版有点乱(应该是网站从别的地方用工具抓来的),所以不打算贴上原地址;版权归原作者所有。

=========开始==========

服务器端设计:
设计方法应该有很多,下面介绍我的一种方法:
a.首先在服务器项目下建立一个文件夹来存放APK安装文件:
b.其次在src下建立一个资源文件,apkVersion.properties,属性定义如下:

<span style="font-family:Courier New;">apkVersion=1 存版本号   
apkSize=550kb 大小   
apkPath=http://xx8080/srv/apk/Demo.apk 升级文件  </span>

c.定义一个servlet来获取资源中的信息:
定义类:UpdateApkServlet.java
<span style="font-family:Courier New;">//获取资源文件信息   
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();
    }
}</span>

获取资源,然后生成JSON字串返回客户端处理。 注:当客户端版本有更新,服务器端只要把APK文件拷贝到APK目录,然后更新apkVersion.properties文件中的信息就可以了,切记。
客户端设计:
1、客户端首先获取服务器的版本信息(http方式获取)。

2、如何获取本地客户端的版本信息 如下参考代码:

<span style="font-family:Courier New;">/**
  * 得到本地应用的版本信息
  * @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;
}</span>

3、版本比较,如果版本相同,则不执行更新,不同才进行更新操作。
这里插入客户端版本设置介绍:
客户端版本设置在AndroidManifest.xml文件中,里面有两个属性可进行版本信息设置,
<span style="font-family:Courier New;">android:versionCode="1" 版本号
android:versionName="1.1" 版本名称</span>

这个版本号需要和服务器端对应。

4、需要的权限设置

Sdcard访问权限: uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"  
访问网络权限: uses-permission android:name="android.permission.INTERNET"

5、 更新安装 当用户点击应用时执行检查更新。相关代码参考:
<span style="font-family:Courier New;">//弹出框提示
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();
        }
    });
}  </span>



//更新升级
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);
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值