Step One 应用系统配置文件----应用版本配置信息
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="TP.NationalTest"
android:versionCode="4"
android:versionName="1.4">
<uses-sdk android:minSdkVersion="4" />
versionCode:版本号
versionName:版本名称
minSdkVersion:最小SDk版本
Step Two 后台验证当前应用版本是否要更新
/*检测应用版本*/
privatevoid CheckAppEdtion()
{
//判断网络连接,如连接
//Step One 判断当前是否有网络连接,
if(!NetWorkUtility.CheckConnection(this))
{
return;
}
//获取网络平台的版本信息
CAppInfo model=AppDataServiceHelper.GetAppEdtionInfo(ProjectConstant.AppID);
//比较版本信息,有新版本 弹出 对话框
if(model!=null)
{
try {
PackageInfo info =this.getPackageManager().getPackageInfo(this.getPackageName(), 0);
int oldVersion = info.versionCode;
int newVersion = Integer.parseInt(model.Edition);
if(newVersion > oldVersion){
//创建更新对话框,并弹出
AppUpdateDialog.CreateRegisterAlert(this);
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
privatevoid CheckAppEdtion()
{
//判断网络连接,如连接
//Step One 判断当前是否有网络连接,
if(!NetWorkUtility.CheckConnection(this))
{
return;
}
//获取网络平台的版本信息
CAppInfo model=AppDataServiceHelper.GetAppEdtionInfo(ProjectConstant.AppID);
//比较版本信息,有新版本 弹出 对话框
if(model!=null)
{
try {
PackageInfo info =this.getPackageManager().getPackageInfo(this.getPackageName(), 0);
int oldVersion = info.versionCode;
int newVersion = Integer.parseInt(model.Edition);
if(newVersion > oldVersion){
//创建更新对话框,并弹出
AppUpdateDialog.CreateRegisterAlert(this);
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
Step THree 弹出对话框 ,用户选择是否要更新APK
static ProgressDialog m_Dialog;
static Context m_context=null ;
static AlertDialog appupdatedialog=null;
/*创建查看单题对话框*/
publicstaticvoid CreateRegisterAlert(Context context)
{
m_context=context;
Dialog dialog =new AlertDialog.Builder(context).setTitle("系统更新")
.setMessage("已有新版本,是否更新?")
.setPositiveButton("确定",new DialogInterface.OnClickListener() {
publicvoid onClick(DialogInterface dialog, int which) {
m_Dialog=ProgressDialog.show(m_context, "请等待...", "正在更新,请稍后...",true);
mRegsiterHandler.sleep(1000);
}
})
.setNegativeButton("取消", new DialogInterface.OnClickListener() {
publicvoid onClick(DialogInterface dialog, int which) {
}
}).create();
dialog.show();
}
static Context m_context=null ;
static AlertDialog appupdatedialog=null;
/*创建查看单题对话框*/
publicstaticvoid CreateRegisterAlert(Context context)
{
m_context=context;
Dialog dialog =new AlertDialog.Builder(context).setTitle("系统更新")
.setMessage("已有新版本,是否更新?")
.setPositiveButton("确定",new DialogInterface.OnClickListener() {
publicvoid onClick(DialogInterface dialog, int which) {
m_Dialog=ProgressDialog.show(m_context, "请等待...", "正在更新,请稍后...",true);
mRegsiterHandler.sleep(1000);
}
})
.setNegativeButton("取消", new DialogInterface.OnClickListener() {
publicvoid onClick(DialogInterface dialog, int which) {
}
}).create();
dialog.show();
}
Step four 下载APK,并更新应用版本
/*
*定时注册程序
* */
privatestatic RegsiterHandler mRegsiterHandler =new RegsiterHandler();
staticclass RegsiterHandler extends Handler {
@Override
publicvoid handleMessage(Message msg) {
try{
String url="http://xx.xx.xx.xx/gointel/nationaltest.html";
HttpClient client =new DefaultHttpClient();
HttpGet get =new HttpGet(url);
HttpResponse response;
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(), "nationaltest.apk");
fileOutputStream =new FileOutputStream(file);
byte[] buf =newbyte[1024];
int ch =-1;
while ((ch = is.read(buf)) !=-1) {
fileOutputStream.write(buf, 0, ch);
}
}
fileOutputStream.flush();
if (fileOutputStream !=null) {
fileOutputStream.close();
}
}
catch(Exception ex)
{
}
finally{
m_Dialog.cancel();
}
install();
}
publicvoid sleep(long delayMillis) {
this.removeMessages(0);
sendMessageDelayed(obtainMessage(0), delayMillis);
}
};
privatestaticvoid install() {
Intent i =new Intent();
//i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
i.setAction(Intent.ACTION_VIEW);
i.setDataAndType(Uri.fromFile(new File("/sdcard/nationaltest.apk")),
"application/vnd.android.package-archive");
m_context.startActivity(i);
//finish();
}
*定时注册程序
* */
privatestatic RegsiterHandler mRegsiterHandler =new RegsiterHandler();
staticclass RegsiterHandler extends Handler {
@Override
publicvoid handleMessage(Message msg) {
try{
String url="http://xx.xx.xx.xx/gointel/nationaltest.html";
HttpClient client =new DefaultHttpClient();
HttpGet get =new HttpGet(url);
HttpResponse response;
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(), "nationaltest.apk");
fileOutputStream =new FileOutputStream(file);
byte[] buf =newbyte[1024];
int ch =-1;
while ((ch = is.read(buf)) !=-1) {
fileOutputStream.write(buf, 0, ch);
}
}
fileOutputStream.flush();
if (fileOutputStream !=null) {
fileOutputStream.close();
}
}
catch(Exception ex)
{
}
finally{
m_Dialog.cancel();
}
install();
}
publicvoid sleep(long delayMillis) {
this.removeMessages(0);
sendMessageDelayed(obtainMessage(0), delayMillis);
}
};
privatestaticvoid install() {
Intent i =new Intent();
//i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
i.setAction(Intent.ACTION_VIEW);
i.setDataAndType(Uri.fromFile(new File("/sdcard/nationaltest.apk")),
"application/vnd.android.package-archive");
m_context.startActivity(i);
//finish();
}