AKever

导航

Android(2) Update 软件版本更新

Update 软件版本更新

Android是通过检测本地的“AndroidManifest.xml”文件中的android:versionCode来判断,也仅仅用来判断而已

贴码:

AppUpdater.java

package com.exi.utils;

import org.json.JSONException;
import org.json.JSONObject;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageManager.NameNotFoundException;
import android.net.Uri;

import com.exi.exr.ExRMannger;

public class AppUpdater {

    private static final String TAG = AppUpdater.class.getSimpleName();
    private static AppUpdater instance = new AppUpdater();

    private String versionName;
    private String downUrl;

    private AppUpdater() {
    }

    public static AppUpdater getInstance() {
        return instance;
    }
    
    //检测版本
    public boolean check(Context ctx) {
//        if (hasNewVersion(ctx)) {
//            Logger.e(TAG, "App has new version");
//            此处调用产生UI(弹出窗口)提示是否更新,需在主线程启动
//        但由于版本检查是另开子线程访问网络,使UI无法启动,故不在此处执行,开handler调用
//            showUpdateConfirmDialog(ctx);
//        }
        return hasNewVersion(ctx);
    }

    //提示版本更新
    public void showUpdateConfirmDialog(final Context ctx) {
        new AlertDialog.Builder(ctx).setMessage("应用助理发布了新版本,请及时更新。")
                .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(downUrl)); 
                        ctx.startActivity(intent);
                    }
                })
                .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                    }
                }).show();
    }

    public boolean hasNewVersion(Context ctx) {
        int versionCode = 0;
        try {
            versionCode = ctx.getPackageManager().getPackageInfo(
                    ctx.getPackageName(), 0).versionCode;
        } catch (NameNotFoundException e) {
            e.printStackTrace();
        }

        ExRMannger exr = ExRMannger.getInstance();
        //调用服务,确定服务的软件安装包版本
        String content = exr.get("/tcsm/versionUpdate.jsp?versionCode=" + versionCode);
        try {
            JSONObject jsonObj = new JSONObject(content);
            int ver = jsonObj.getInt("versionCode");
            versionName = jsonObj.getString("versionName");
            downUrl = jsonObj.getString("downUrl");

            if (ver > versionCode) {
                return true;
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }

        return false;
    }
}

Activity活动中检测

Handler + Thread

// 版本加测常量
private final int MSG_UPDATE_YES = 10;
private final int MSG_UPDATE_NO = 11;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_orderlist);
    
    //...
    
    // 版本检测
    versionUpdate();
}

// 安装包版本加测
private void versionUpdate() {
    // Handler
    final Handler handler = new Handler(Looper.getMainLooper()) {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
            case MSG_UPDATE_YES:
                // / here show alert dialog
                AppUpdater.getInstance().showUpdateConfirmDialog(
                        CaseListActivity.this);
                break;
            case MSG_UPDATE_NO:
                break;
            default:
                super.handleMessage(msg);
            }
        }
    };

    new Thread(new Runnable() {
        @Override
        public void run() {
            // TODO Auto-generated method stub
            Looper.prepare();//
            boolean flag = AppUpdater.getInstance().check(CaseListActivity.this);
            if(flag) {
                Message msg = Message.obtain(handler, MSG_UPDATE_YES,
                    CaseListActivity.this);
                msg.sendToTarget();
            } else {
                Message msg = Message.obtain(handler, MSG_UPDATE_NO,
                        CaseListActivity.this);
                    msg.sendToTarget();
            }
            
        }
    }).start();
}

 

 服务器返回数据:

{"versionCode":1(版本, 为整形),"downUrl":"安装包访问路径","versionName":"versionName"}

例:

{"versionCode":3,"downUrl":"http://192.168.100.100:8090/oa/apkUpdate/2014\\2\\26\\6bda3679-0144-1000-e000-001ac0a86464.apk","versionName":"3.0"}

 

---end!!!

posted on 2014-02-27 15:45  AKever  阅读(412)  评论(0)    收藏  举报