当前位置:操作系统 > 安卓/Android >>

android自动更新apk版本

截图如下:

\


代码实现如下:
 
package com.update.apk;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONArray;
import org.json.JSONObject;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageManager.NameNotFoundException;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.os.Message;
import android.util.Log;

public class MainActivity extends Activity {
    /** Called when the activity is first created. */
    String newVerName = "";//新版本名称
    int newVerCode = -1;//新版本号
    ProgressDialog pd = null;
    String UPDATE_SERVERAPK = "ApkUpdateAndroid.apk";
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        if(getServerVer()){
            int verCode = this.getVerCode(this);
            if(newVerCode>verCode){
                doNewVersionUpdate();//更新版本
            }else{
                notNewVersionUpdate();//提示已是最新版本
            }
        }
    }
   
    /**
     * 获得版本号
     */
    public int getVerCode(Context context){
        int verCode = -1;
        try {
            verCode = context.getPackageManager().getPackageInfo("com.update.apk", 0).versionCode;
        } catch (NameNotFoundException e) {
            // TODO Auto-generated catch block
            Log.e("版本号获取异常", e.getMessage());
        }
        return verCode;
    }
   
    /**
     * 获得版本名称
     */
    public String getVerName(Context context){
        String verName = "";
        try {
            verName = context.getPackageManager().getPackageInfo("com.update.apk", 0).versionName;
        } catch (NameNotFoundException e) {
            Log.e("版本名称获取异常", e.getMessage());
        }
        return verName;
    }
   
    /**
     * 从服务器端获得版本号与版本名称
     * @return
     */
    public boolean getServerVer(){
        try {
            URL url = new URL("http://10.0.2.2:8080/ApkUpdateService/ver");
            HttpURLConnection httpConnection = (HttpURLConnection) url.openConnection();
            httpConnection.setDoInput(true);
            httpConnection.setDoOutput(true);
            httpConnection.setRequestMethod("GET");
            httpConnection.connect();
            InputStreamReader reader = new InputStreamReader(httpConnection.getInputStream());
            BufferedReader bReader = new BufferedReader(reader);
            String json = bReader.readLine();
            JSONArray array = new JSONArray(json);
            JSONObject jsonObj = array.getJSONObject(0);
            newVerCode = Integer.parseInt(jsonObj.getString("verCode"));       
            newVerName = jsonObj.getString("verName");
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            return false;
        }
        return true;
    }
   
    /**
     * 不更新版本
     */
    public void notNewVersionUpdate(){
        int verCode = this.getVerCode(this);
        String verName = this.getVerName(this);
        StringBuffer sb = new StringBuffer();
        sb.append("当前版本:");
        sb.append(verName);
        sb.append(" Code:");
        sb.append(verCode);
        sb.append("\n已是最新版本,无需更新");
        Dialog dialog = new AlertDialog.Builder(this)
     &n

补充:移动开发 , Android ,
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,