直接贴代码:
config类:
[html]
package com.tutor.config;
import org.json.JSONArray;
import org.json.JSONObject;
import com.tutor.update.R;
import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.util.Log;
public class Config
{
public String UPDATE_SERVER = "http://10.81.48.181:8080/update/";
public String UPDATE_APKNAME = "";
public String UPDATE_SAVENAME = "update.apk";
public String UPDATE_VERJSON = "ver.json";
public static int newVerCode;
public static String newVerName;
/**
* 获取当前版本号
*
* @param context
* @return
*/
public int getVerCode(Context context)
{
int verCode = -1;
try
{
// 获取packagemanager的实例
PackageManager packageManager = context.getPackageManager();
// getPackageName()是你当前类的包名,0代表是获取版本信息
PackageInfo packInfo =
packageManager.getPackageInfo(context.getPackageName(), 0);
verCode = packInfo.versionCode;
// verCode =
// context.getPackageManager().getPackageInfo(
// "com.example.update", 0).versionCode;
System.out.println("nowcode---?>>>" + verCode);
}
catch (NameNotFoundException e)
{
Log.e("RG", e.getMessage());
}
return verCode;
}
/**
* 获取版本名称
*
* @param context
* @return
*/
public String getVerName(Context context)
{
String verName = "";
try
{
// 获取packagemanager的实例
PackageManager packageManager = context.getPackageManager();
// getPackageName()是你当前类的包名,0代表是获取版本信息
PackageInfo packInfo =
packageManager.getPackageInfo(context.getPackageName(), 0);
verName = packInfo.versionName;
// verName =
// context.getPackageManager().getPackageInfo(
// "com.example.update", 0).versionName;
}
catch (NameNotFoundException e)
{
Log.e("RG", e.getMessage());
}
return verName;
}
// public static String getVerName(Context context)
// {
// String verName =
// context.getResources().getText(R.string.app_versionName)
// .toString();
// return verName;
// }
public static String getAppName(Context context)
{
String verName =
context.getResources().getText(R.string.app_name).toString();
return verName;
}
/**
* 检查服务器的版本
*
* @return
*/
public boolean getServerVerCode()
{
try
{
String verjson =
NetworkTool.getContent(UPDATE_SERVER + UPDATE_VERJSON);
System.out.println("verjson--->>>" + verjson);
JSONArray array = new JSONArray(verjson);
if (array.length() > 0)
{
JSONObject obj = array.getJSONObject(0);
try
{
newVerCode = Integer.parseInt(obj.getString("verCode"));
newVerName = obj.getString("verName");
System.out.println("newVerCode--->>>" + newVerCode);
System.out.println("new
补充:移动开发 , Android ,