//动画结束跳转更新版本 import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.content.Intent; import android.view.animation.Animation; import android.view.animation.Animation.AnimationListener; import android.view.animation.RotateAnimation; import android.widget.ImageView; public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //初始化控件 ImageView img = (ImageView) findViewById(R.id.img); //设置动画 RotateAnimation animation = new RotateAnimation(0, 360*4, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f); //设置动画时间 animation.setDuration(2000); //保持动画结束时候的状态、 animation.setFillAfter(true); img.setAnimation(animation); //设置动画的监听事件 animation.setAnimationListener(new AnimationListener() { @Override public void onAnimationStart(Animation animation) { // TODO Auto-generated method stub } @Override public void onAnimationRepeat(Animation animation) { // TODO Auto-generated method stub } @Override public void onAnimationEnd(Animation animation) { // TODO Auto-generated method stub Intent in = new Intent(MainActivity.this, Main2Activity.class); startActivity(in); } }); } }
//检查版本信息 import java.io.BufferedInputStream; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.net.HttpURLConnection; import java.net.MalformedURLException; import java.net.URL; import org.xmlpull.v1.XmlPullParser; import android.app.AlertDialog; import android.app.AlertDialog.Builder; import android.app.ProgressDialog; import android.content.DialogInterface; import android.content.Intent; import android.content.pm.PackageInfo; import android.content.pm.PackageManager; import android.net.Uri; import android.os.AsyncTask; import android.os.Bundle; import android.os.Environment; import android.support.v7.app.AppCompatActivity; import android.util.Log; import android.util.Xml; import android.widget.Toast; public class Main2Activity extends AppCompatActivity { private int versionCode; private String path = "http://www.oschina.net/MobileAppVersion.xml"; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main2); // 获得当前版本号 try { versionCode = getVersionName(); Log.e("TAG", "项目版本号:" + versionCode); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } // 请求网络数据 forInfo(); } // 请求网络数据 private void forInfo() { new AsyncTask<String, Void, InputStream>() { @Override protected InputStream doInBackground(String... params) { try { URL url = new URL(path); HttpURLConnection conn = (HttpURLConnection) url .openConnection(); conn.setRequestMethod("GET"); conn.setConnectTimeout(3000); // /判断结果码 if (conn.getResponseCode() == 200) { InputStream inputStream = conn.getInputStream(); return inputStream; } } catch (MalformedURLException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } return null; } @Override protected void onPostExecute(InputStream result) { super.onPostExecute(result); // 解析xml文件 try { UpdataInfo info = getUpdataInfo(result); Log.e("TAG", "info.vercode= "+info.versionCode); // 对比版本号 if (info.versionCode.equals(versionCode+"")) { Toast.makeText(Main2Activity.this, "已经是最新版本", Toast.LENGTH_SHORT).show(); } else { // 弹出提示框 tishigengxin(info); } } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } }.execute(path); } // 提示更新的框 protected void tishigengxin(final UpdataInfo info) { AlertDialog.Builder builder = new Builder(this); builder.setTitle("有新版本").setMessage(info.updateLog); builder.setNegativeButton("以后再说", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface arg0, int arg1) { } }); builder.setPositiveButton("立即更新", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface arg0, int arg1) { xiazaiapk(info.downloadUrl);// 下载 } }); AlertDialog dialog = builder.create(); dialog.show(); } // 下载APK protected void xiazaiapk(final String url) { final ProgressDialog pd = new ProgressDialog(this); pd.setTitle("正在下载新版本"); pd.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); pd.show(); new Thread() { public void run() { File file = getapk(pd, url); try { sleep(2000); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } Intent intent = new Intent(); intent.setAction(Intent.ACTION_VIEW); intent.setDataAndType(Uri.fromFile(file), "application/vnd.android.package-archive"); startActivity(intent); pd.dismiss(); } }.start(); } // 安装APK protected File getapk(final ProgressDialog pd, final String url) { try { URL url2 = new URL(url); HttpURLConnection conn = (HttpURLConnection) url2.openConnection(); conn.setConnectTimeout(5000); int responseCode = conn.getResponseCode(); if (responseCode == 200) { int contentLength = conn.getContentLength(); pd.setMax(contentLength); InputStream inputStream = conn.getInputStream(); File file = new File(Environment.getExternalStorageDirectory(), "update.apk"); FileOutputStream fileOutputStream = new FileOutputStream(file); BufferedInputStream bis = new BufferedInputStream(inputStream); int len = 0; byte[] buffer = new byte[1024]; int total = 0; while ((len = bis.read(buffer)) != -1) { fileOutputStream.write(buffer, 0, len); total += len; pd.setProgress(total); } return file; } } catch (MalformedURLException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } return null; } // 获得当前版本号 private int getVersionName() throws Exception { PackageManager packageManager = getPackageManager(); PackageInfo packageInfo = packageManager.getPackageInfo( "com.bwie.test", 0); return packageInfo.versionCode; } // 获得服务器的版本号 public static UpdataInfo getUpdataInfo(InputStream is) throws Exception { XmlPullParser parser = Xml.newPullParser(); parser.setInput(is, "utf-8");// 设置解析的数据源 int type = parser.getEventType(); UpdataInfo info = new UpdataInfo();// 实体 while (type != XmlPullParser.END_DOCUMENT) { switch (type) { case XmlPullParser.START_TAG: if ("versionCode".equals(parser.getName())) { info.versionCode = parser.nextText(); // 获取版本号 } else if ("downloadUrl".equals(parser.getName())) { info.downloadUrl = parser.nextText(); // 获取要升级的APK文件 } else if ("updateLog".equals(parser.getName())) { info.updateLog = parser.nextText(); // 获取该文件的信息 } break; } type = parser.next(); } return info; } }
//ben包
public class UpdataInfo { public String versionCode; public String versionName; public String downloadUrl; public String updateLog; public UpdataInfo() { super(); // TODO Auto-generated constructor stub } public UpdataInfo(String versionCode, String versionName, String downloadUrl, String updateLog) { super(); this.versionCode = versionCode; this.versionName = versionName; this.downloadUrl = downloadUrl; this.updateLog = updateLog; } }