Android app 启动画面实现
1.直接上代码
package com.holl.vwifi.login.ui; import java.io.File; import java.io.IOException; import java.io.InputStream; import java.net.HttpURLConnection; import java.net.URL; import org.xmlpull.v1.XmlPullParser; import org.xmlpull.v1.XmlPullParserFactory; import cn.jpush.android.api.InstrumentedActivity; import com.holl.vwifi.R; import com.holl.vwifi.application.AppContext; import com.holl.vwifi.login.bean.ApkInfo; import com.holl.vwifi.login.bean.MyDevInfo; import com.holl.vwifi.login.bean.User; import com.holl.vwifi.login.common.CommandManagement; import com.holl.vwifi.login.db.DbManager; import com.holl.vwifi.login.task.DownLoadFileTask; import com.holl.vwifi.util.ComHelper; import com.holl.vwifi.util.Logger; import android.app.Activity; import android.app.AlertDialog; import android.app.ProgressDialog; import android.app.AlertDialog.Builder; import android.content.DialogInterface; import android.content.Intent; import android.content.SharedPreferences; import android.content.DialogInterface.OnClickListener; import android.content.pm.PackageManager.NameNotFoundException; import android.net.Uri; import android.os.Bundle; import android.os.Environment; import android.os.Handler; import android.os.Message; import android.view.View; import android.view.animation.AlphaAnimation; import android.view.animation.Animation; import android.view.animation.Animation.AnimationListener; import android.widget.LinearLayout; import android.widget.Toast; /** * 应用程序启动类:显示欢迎界面并跳转到主界面 * @Description: (这里用一句话描述这个类的作用) * @author yehj * @date 2014-8-7 上午9:28:15 * @version 1.01 */ public class AppStart extends InstrumentedActivity { private static final String TAG="AppStart"; private MyDevInfo devInfo; private ProgressDialog progressDialog ; private static final String SHAREDPREFERENCES_NAME = "first_pref"; private static final String transferIp="112.124.124.183"; private boolean isFirstIn ; private AppContext appContext;// 全局Context ApkInfo info=null; DownLoadFileThreadTask task; private Handler mHandler = new Handler(){ @Override public void handleMessage(Message msg) { super.handleMessage(msg); // 判断服务器版本号 和客户端的版本号 是否相同 if(msg.what==0){ //showUpdataDialog();//弹出来升级对话框 } if(msg.what==1){//下载 progressDialog.show(); new Thread(task).start(); } if(msg.what==2){//下载过程中,网络异常 Toast.makeText(AppStart.this,R.string.download_apk_fail,Toast.LENGTH_SHORT).show(); progressDialog.dismiss(); } if(msg.what==3){//到导航页面 } } }; @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); final View view = View.inflate(this, R.layout.start, null); LinearLayout wellcome = (LinearLayout) view.findViewById(R.id.app_start_view); setContentView(view); appContext = (AppContext) getApplication(); //渐变展示启动屏 AlphaAnimation aa = new AlphaAnimation(0.3f,1.0f); aa.setDuration(2000); view.startAnimation(aa); aa.setAnimationListener(new AnimationListener() { @Override public void onAnimationEnd(Animation arg0) { redirectTo(); } @Override public void onAnimationRepeat(Animation animation) {} @Override public void onAnimationStart(Animation animation) {} }); progressDialog = new ProgressDialog(this); progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); progressDialog.setMessage(this.getResources().getText(R.string.downloading)); progressDialog.setCancelable(false);//注意一下 new Thread(){ @Override public void run() { try { sleep(1000); //handler.sendEmptyMessage(0); if (isNeedUpdate(getVerCode())) { //弹出来升级对话框 mHandler.sendEmptyMessage(0); } } catch (InterruptedException e) { e.printStackTrace(); } } }.start(); } @Override protected void onDestroy() { // TODO Auto-generated method stub super.onDestroy(); } /** * 跳转到... */ private void redirectTo(){ SharedPreferences preferences = getSharedPreferences( SHAREDPREFERENCES_NAME, MODE_PRIVATE); // 取得相应的值,如果没有该值,说明还未写入,用true作为默认值 isFirstIn = preferences.getBoolean("isFirstIn", true); Intent intent=null; if(isFirstIn){ intent= new Intent(this, GuideActivity.class); startActivity(intent); }else{ Logger.d(TAG,appContext.ifHaveUserLogin()+"" ); if(appContext.ifHaveUserLogin()){ intent= new Intent(this, LoginBindRouter.class); startActivity(intent); }else{ if(ComHelper.isWifiConnected(appContext)){ appContext.getCom().connectVwifi(null, null, ComHelper.getGateWay(appContext), transferIp); int flag=appContext.getCom().connectvwifiready(); Logger.d("yehj","flag=="+flag); if(flag==0){ devInfo=appContext.getCom().getMyDevInfo(); if(devInfo!=null&&devInfo.getRes()!=null&&devInfo.getRes().equals("0")){ appContext.setWifiSSID(devInfo.getAp_Info().getSSID()); Logger.d("yehj", "STATUS=="+devInfo.getSTATUS()); if(devInfo.getSTATUS()!=null&&devInfo.getSTATUS().equals("0")){ intent=new Intent(this,CheckWanActivity.class); startActivity(intent); }else{ intent=new Intent(this,LoginBindRouter.class); startActivity(intent); } } }else{ intent=new Intent(this,LoginBindRouter.class); startActivity(intent); } }else{ intent=new Intent(this,LoginBindRouter.class); startActivity(intent); } } } finish(); } /** * * @param versiontext * 当前客户端的版本号信息 * @return 是否需要更新 */ private boolean isNeedUpdate(int versionCode) { try { info =getUpdateApkInfo(); int latestCode = info.getVersionCode();//这里info可以为空,会跳到异常处理里面 if (latestCode<=versionCode) { //Log.i("splashActivity", "版本相同,无需升级, 进入主界面"); return false; } else { //Log.i("splashActivity", "版本不同,需要升级"); return true; } } catch (Exception e) { e.printStackTrace(); //Log.i("splashActivity", "获取更新信息异常, 进入主界面"); return false; } } public int getVerCode() { int verCode = -1; try { verCode = this.getPackageManager().getPackageInfo("com.holl.vwifi", 0).versionCode; } catch (NameNotFoundException e) { e.printStackTrace(); } return verCode; } /** * 安装apk * @param file */ private void install(File file){ Intent intent = new Intent(); intent.setAction(Intent.ACTION_VIEW); intent.setDataAndType(Uri.fromFile(file), "application/vnd.android.package-archive"); finish(); startActivity(intent); } //检查版本更新 private ApkInfo getUpdateApkInfo(){ ApkInfo apkInfo=null; InputStream inputStream=null; HttpURLConnection connection=null; try { URL url=new URL("http://www.holl.cn/APP/android/holl_smart.xml"); connection= (HttpURLConnection)url.openConnection();// connection.setRequestMethod("GET"); connection.setConnectTimeout(2000);//设置连接主机超时(单位:毫秒) connection.setReadTimeout(2000);//设置从主机读取数据超时(单位:毫秒) if(connection.getResponseCode()!= 200){ return apkInfo; } inputStream=connection.getInputStream(); XmlPullParserFactory pullParserFactory = XmlPullParserFactory.newInstance(); XmlPullParser parser = pullParserFactory.newPullParser(); parser.setInput(inputStream, "UTF-8"); // 编码 int event = parser.getEventType();// 产生第一个事件 while (event!=parser.END_DOCUMENT) { switch (event) { case XmlPullParser.START_DOCUMENT: apkInfo = new ApkInfo(); break; case XmlPullParser.START_TAG:// 判断当前事件是否是标签元素开始事件 String tempParser=parser.getName(); if ("name".equalsIgnoreCase(tempParser)) // 判断开始标签元素是否是CMD_RES { apkInfo.setName(parser.nextText()); } // 开始解析XML else if("version".equalsIgnoreCase(tempParser)){ apkInfo.setVersion(parser.nextText()); } else if("versionCode".equalsIgnoreCase(tempParser)){ apkInfo.setVersionCode(Integer.parseInt(parser.nextText())); } else if("desc".equalsIgnoreCase(tempParser)){ apkInfo.setDes(parser.nextText()); } else if("url".equalsIgnoreCase(tempParser)){ apkInfo.setUrl(parser.nextText()); } case XmlPullParser.END_TAG: break; } event = parser.next();//进入下一个元素并触发相应事件 } } catch (Exception e) { e.printStackTrace(); apkInfo=null; }finally { try { if(inputStream != null) inputStream.close(); if(connection!=null) connection.disconnect(); } catch (IOException e) { e.printStackTrace(); } } return apkInfo; } /** * 升级的对话框 */ private void showUpdataDialog() { AlertDialog.Builder buider = new Builder(this); buider.setIcon(R.drawable.prompt); buider.setTitle("升级提醒"); buider.setMessage("汇尔微管家"+info.getVersion()+"发布了!\n"+"更新内容: "+info.getDes()); buider.setCancelable(false); // 让用户不能取消对话框 buider.setPositiveButton("马上升级", new OnClickListener() { public void onClick(DialogInterface dialog, int which) { //Log.i("splashActivity", "下载apk文件" + info.getUrl()); if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){ task=new DownLoadFileThreadTask(info.getUrl(), "/sdcard/holl/holl_smart.apk"); mHandler.sendEmptyMessage(1); }else{ Toast.makeText(getApplicationContext(), "sd卡不可用",Toast.LENGTH_SHORT).show(); } } }); buider.setNegativeButton("以后再说", new OnClickListener() { public void onClick(DialogInterface dialog, int which) { //Log.i("splashActivity", "用户取消进入程序主界面") } }); buider.create().show(); } private class DownLoadFileThreadTask implements Runnable { private String path; // 服务器路径 private String filepath; // 本地文件路径 public DownLoadFileThreadTask(String path, String filepath) { this.path = path; this.filepath = filepath; } public void run() { try { File file = DownLoadFileTask.getFile(path, filepath,progressDialog); //Log.i("splashActivity","下载成功"); progressDialog.dismiss(); install(file); } catch (Exception e) { e.printStackTrace(); mHandler.sendEmptyMessage(2);//"下载fail" } } } }
布局文件很简单
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/app_start_view" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" android:gravity="bottom" android:background="@drawable/start_background"> </LinearLayout>