现在的位置: 首页 > java > android > 正文
安卓升级机制
2016年04月15日 android ⁄ 共 3242字 暂无评论
 
package com.laileyou.fmscms.activity; import android.app.Activity; import android.app.AlertDialog; import android.content.DialogInterface; import android.content.Intent; import android.content.pm.PackageManager; import android.net.Uri; import android.os.Bundle; import android.view.View; import android.view.Window; import android.view.animation.AlphaAnimation; import android.view.animation.Animation; import com.lidroid.xutils.HttpUtils; import com.lidroid.xutils.exception.HttpException; import com.lidroid.xutils.http.ResponseInfo; import com.lidroid.xutils.http.callback.RequestCallBack; import com.lidroid.xutils.http.client.HttpRequest; import com.laileyou.fmscms.*; import com.laileyou.fmscms.util.Util; import java.io.File; import java.util.Timer; import java.util.TimerTask; /** * */ public class StartActivity extends Activity { private String url=""; private boolean misUpdate = false; @Override public void onCreate(Bundle savedInstanceState) { this.requestWindowFeature(Window.FEATURE_NO_TITLE); super.onCreate(savedInstanceState); final View view = View.inflate(this, R.layout.start, null); setContentView(view); url= FmsCmsApplication.mUrl+"/index.php/ApiNew/getappversion"; //Timer timer = new Timer(); //TimerTask task = new TimerTask() { //@Override //public void run() { // redirectTo(); // } //}; //timer.schedule(task,3000); //渐变展示启动屏 AlphaAnimation aa = new AlphaAnimation(0.3f,1.0f); aa.setDuration(4000); view.startAnimation(aa); aa.setAnimationListener(new Animation.AnimationListener() { @Override public void onAnimationEnd(Animation arg0) { if(!misUpdate){ redirectTo(); } } @Override public void onAnimationRepeat(Animation animation) {} @Override public void onAnimationStart(Animation animation) { //获取app版本号,查看app是否需要更新 HttpUtils httpUtils = new HttpUtils(); httpUtils.send(HttpRequest.HttpMethod.GET, url, new RequestCallBack() { @Override public void onSuccess(ResponseInforesponseInfo) { try {//获取版本号判断是否需要更新 String version = getPackageManager().getPackageInfo(StartActivity.this.getPackageName(), 0).versionName; if (!responseInfo.result.toString().trim().equals(version.trim())) { new AlertDialog.Builder(StartActivity.this).setTitle("提示").setMessage("有新版本是否更新") .setPositiveButton("更新",new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { File file = new File("/sdcard/fmscms.apk"); if(file.exists()){ installApk(); }else { HttpUtils httpUtils = new HttpUtils(); httpUtils.download( FmsCmsApplication.mUrl + "/fmscms.apk", "/sdcard/fmscms.apk", true, false, new RequestCallBack() { @Override public void onSuccess(ResponseInforesponseInfo) { installApk(); } @Override public void onFailure(HttpException e, String s) { } @Override public void onStart() { Util.showToast(StartActivity.this, "正在下载..."); } }); } } }).setNegativeButton("取消", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { redirectTo(); } }).setCancelable(false).show(); } } catch (PackageManager.NameNotFoundException e) { e.printStackTrace(); } } @Override public void onFailure(HttpException e, String s) { redirectTo(); } }); } }); } /** * 跳转到... */ private void redirectTo(){ Intent intent = new Intent(this,IndexActivity.class); startActivity(intent); finish(); } private void installApk(){ Intent intent = new Intent(); intent.setAction(Intent.ACTION_VIEW); File file = new File("/sdcard/fmscms.apk"); intent.setDataAndType(Uri.fromFile(file), "application/vnd.android.package-archive"); startActivity(intent); } }

给我留言

您必须 [ 登录 ] 才能发表留言!

×