Android应用更新

开发好的Android应用,在上线之后,往往会有更新,很多Android应用往往是这样做的,手机上打开Android应用,然后应用就会联网,获取远程服务器提供的Android应用的最新版本号,然后再和当前已安装的Android应用的版本号进行对比,如果远程服务器提供的Android应用的版本比已在手机上安装的应用的版本新,便提示用户下载更新。

AndroidManifest.xml这个文件的versionCode表示版本号,versionName表示版本名称 ,versionCode是整型数字,versionName是字符串类型,一般我们会用versionCode作为软件是否更新的参考。

下面是一个工具类,专门获取版本号和版本名称的,如下:

[java]public class VersionUtil {
private static final String TAG="VersionUtil";

/**
* getVersionNo(获取应用的版本号)
* @param context
* @return
*/
public static int getVersionNo(Context context){
int versionNo=-1;
try{
versionNo=context.getPackageManager()
.getPackageInfo("com.hxxy.androidupdateproject", PackageManager.PERMISSION_GRANTED)
.versionCode;
}catch(NameNotFoundException e){
Log.e(TAG, e.getMessage());
}
return versionNo;
}

/**
* getVersionName(获取应用的版本名称)
* @param context
* @return
*/
public static String getVersionName(Context context){
String versionName="";
try{
versionName=context.getPackageManager()
.getPackageInfo("com.hxxy.androidupdateproject", PackageManager.PERMISSION_GRANTED)
.versionName;
}catch(NameNotFoundException e){
Log.e(TAG, e.getMessage());
}
return versionName;
}
}[/java]

下面是MainActivity.java

[java]public class MainActivity extends Activity {
/**
* @Fields TAG : 用于日志输出
*/
private static final String TAG = "MainActivity";

/**
* @Fields handler : 主要处理非main线程与main线程通信
*/
private Handler handler = new Handler() {
public void handleMessage(Message msg) {
final SwInfo swInfo = (SwInfo) msg.obj;
if (swInfo != null) {
// 创建提示AlertDialog
AlertDialog alertDialog = new AlertDialog.Builder(MainActivity.this).create();
// 如果软件版本为最新的
if (swInfo.isNew()) {
alertDialog.setTitle("您的版本为最新的!");
alertDialog.setMessage(getResources().getString(R.string.update_dialog_hint, swInfo.pre_versionNo, swInfo.pre_versionName,
swInfo.versionNo, swInfo.versionName));
alertDialog.setButton(getResources().getString(R.string.update_dialog_btn_ok), new OnClickListener() {

@Override
public void onClick(DialogInterface dialog, int which) {
// 安装最新apk
installApk(swInfo.apkLocation);

}
});

alertDialog.setButton2(getResources().getString(R.string.update_dialog_btn_no), new OnClickListener() {

@Override
public void onClick(DialogInterface dialog, int which) {
// 不作任何处理

}
});
// 显示对话框
alertDialog.show();
}
}
}
};

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

// 启动检查更新线程
new UpdateThread().start();
}

/**
* 根据位置安装apk
* @param location
*/
private void installApk(String location) {
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.setDataAndType(Uri.fromFile(new File(Environment.getExternalStorageDirectory(), location)), "application/vnd.android.package-archive");
startActivity(intent);
}

// 检查软件版本信息线程
class UpdateThread extends Thread {

@Override
public void run() {
Properties props = new Properties();
InputStream in = null;
try {
// 本地properties文件中读取的版本号代替网络上获取的版本号
in = getClassLoader().getResourceAsStream("apk.properties");
props.load(in);
int lastest_versionCode = Integer.parseInt(props.getProperty("lastest_versionCode"));
String lastest_versionName = props.getProperty("lastest_versionName");
String apkLocation = props.getProperty("lastest_apk_location");
// 当前应用的版本号
int cur_versionCode = VersionUtil.getVersionNo(MainActivity.this);
String cur_versionName = VersionUtil.getVersionName(MainActivity.this);
SwInfo swInfo = new SwInfo(cur_versionCode, lastest_versionCode, cur_versionName, lastest_versionName, apkLocation);
//从Android系统已经存在的全局消息池获取一个消息实例
Message msg = handler.obtainMessage();
msg.obj = swInfo;
msg.sendToTarget();
} catch (IOException e) {
Log.e(TAG, e.getMessage());
} finally {
try {
in.close();
} catch (IOException e) {
Log.e(TAG, e.getMessage());
}
}

}

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}

class SwInfo {
int pre_versionNo;
int versionNo;
String pre_versionName;
String versionName;
String apkLocation;

public SwInfo(int pre_versionNo, int versionNo, String pre_versionName, String versionName, String apkLocation) {
super();
this.pre_versionNo = pre_versionNo;
this.versionNo = versionNo;
this.pre_versionName = pre_versionName;
this.versionName = versionName;
this.apkLocation = apkLocation;
}

public boolean isNew() {
return versionNo > pre_versionNo;
}
}
}[/java]

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值