在上一篇文章中咱們已經開發好了對應的啓動頁面,今天咱們將繼續開發進去啓動頁面時自動的鏈接服務器,獲取服務器上的最新信息,若是服務器上的版本大於當前版本的話,則彈出對話框提示下載更新,不然直接進入主界面!下面是效果圖:java
下面是SplashActivity的所有代碼:android
1 package lq.wangzhen.mobilesafe; 2 3 import java.io.IOException; 4 import java.io.InputStream; 5 import java.net.HttpURLConnection; 6 import java.net.MalformedURLException; 7 import java.net.URL; 8 9 import lq.wangzhen.mobilesafe.domain.UpdateInfo; 10 import lq.wangzhen.mobilesafe.engine.UpdateInfoParser; 11 12 import org.xmlpull.v1.XmlPullParserException; 13 14 import android.app.Activity; 15 import android.app.AlertDialog; 16 import android.app.AlertDialog.Builder; 17 import android.content.DialogInterface; 18 import android.content.DialogInterface.OnClickListener; 19 import android.content.Intent; 20 import android.content.pm.PackageInfo; 21 import android.content.pm.PackageManager; 22 import android.content.pm.PackageManager.NameNotFoundException; 23 import android.os.Bundle; 24 import android.os.Handler; 25 import android.os.Message; 26 import android.util.Log; 27 import android.widget.TextView; 28 import android.widget.Toast; 29 30 public class SplashActivity extends Activity { 31 private TextView tv_splash_version; 32 private UpdateInfo info; 33 34 private static final int GET_INFO_SUCCESS = 10; 35 private static final int SERVER_ERROR = 11; 36 private static final int SERVER_URL_ERROR = 12; 37 private static final int IO_ERROR = 13; 38 private static final int XML_PARSER_ERROR = 14; 39 protected static final String TAG = "SplashActivity"; 40 41 private Handler handler = new Handler(){ 42 43 @Override 44 public void handleMessage(Message msg) { 45 switch (msg.what) { 46 case XML_PARSER_ERROR: 47 Toast.makeText(getApplicationContext(), "XML解析異常", Toast.LENGTH_LONG).show(); 48 loadMainUI(); //加載主界面 49 break; 50 case IO_ERROR: 51 Toast.makeText(getApplicationContext(), "IO異常", Toast.LENGTH_LONG).show(); 52 loadMainUI(); //加載主界面 53 break; 54 case SERVER_URL_ERROR: 55 Toast.makeText(getApplicationContext(), "服務器URL出錯", Toast.LENGTH_LONG).show(); 56 loadMainUI(); //加載主界面 57 break; 58 case SERVER_ERROR: 59 Toast.makeText(getApplicationContext(), "服務器異常", Toast.LENGTH_LONG).show(); 60 loadMainUI();//加載主界面 61 break; 62 case GET_INFO_SUCCESS: 63 String serverVersion = info.getVersion(); //取得服務器上的版本號 64 String currentVersion = getVersion(); //取得當前應用的版本號 65 if(currentVersion.equals(serverVersion)){ //判斷兩個版本號是否相同 66 Log.i(TAG, "版本號相同,進入主界面!"); 67 loadMainUI(); //若是版本號相同,則進入主界面 68 }else{ 69 Log.i(TAG, "版本號不相同,對話框!"); 70 showUpdateDialog(); //若是版本號不相同,則加載更新對話框 71 } 72 break; 73 74 default: 75 break; 76 } 77 } 78 79 }; 80 //加載主界面 81 public void loadMainUI(){ 82 Intent intent = new Intent(this,MainActivity.class); 83 startActivity(intent); 84 finish(); //結束當前的Activity 85 } 86 //顯示升級對話框 87 protected void showUpdateDialog() { 88 AlertDialog.Builder builder = new Builder(this); //實例化對話框 89 builder.setIcon(getResources().getDrawable(R.drawable.notification)); //添加圖標 90 builder.setTitle("升級提示"); //添加標題 91 builder.setMessage(info.getDescription()); //添加內容 92 builder.setPositiveButton("升級", new OnClickListener() { //點擊升級時的操做方法 93 @Override 94 public void onClick(DialogInterface dialog, int which) { 95 Log.i(TAG, "升級,地址:"+info.getApkurl()); 96 } 97 }); 98 builder.setNegativeButton("取消", new OnClickListener() { //點擊取消時的操做方法 99 @Override 100 public void onClick(DialogInterface dialog, int which) { 101 loadMainUI(); 102 } 103 }); 104 builder.create().show(); //顯示對話框 105 } 106 @Override 107 protected void onCreate(Bundle savedInstanceState) { 108 super.onCreate(savedInstanceState); 109 setContentView(R.layout.activity_splash); 110 this.tv_splash_version = (TextView) this.findViewById(R.id.tv_splash_version); 111 this.tv_splash_version.setText("版本號:"+getVersion()); //設置應用程序的版本號 112 new Thread(new CheckVersionTask()){}.start(); //啓動一個線程進行服務器鏈接判斷版本號是否相同 113 } 114 115 private class CheckVersionTask implements Runnable{ 116 @Override 117 public void run() { 118 Message msg = Message.obtain(); 119 //一、取得服務器地址 120 String serverUrl = getResources().getString(R.string.serverurl); //取得服務器地址 121 //二、鏈接服務器 122 try { 123 URL url = new URL(serverUrl); 124 HttpURLConnection conn = (HttpURLConnection) url.openConnection(); 125 conn.setConnectTimeout(5000); 126 conn.setRequestMethod("GET"); 127 int code = conn.getResponseCode(); 128 if(code == 200){ 129 InputStream is = conn.getInputStream(); //取得服務器返回的內容 130 info = UpdateInfoParser.getUpdateInfo(is); //調用本身編寫的方法,將輸入流轉換爲UpdateInfo對象 131 msg.what = GET_INFO_SUCCESS; 132 handler.sendMessage(msg); 133 }else{ 134 msg.what = SERVER_ERROR; 135 handler.sendMessage(msg); 136 } 137 } catch (MalformedURLException e) { 138 msg.what = SERVER_URL_ERROR; 139 handler.sendMessage(msg); 140 handler.sendMessage(msg); 141 } catch (IOException e) { 142 msg.what = IO_ERROR; 143 handler.sendMessage(msg); 144 e.printStackTrace(); 145 } catch (XmlPullParserException e) { 146 msg.what = XML_PARSER_ERROR; 147 handler.sendMessage(msg); 148 e.printStackTrace(); 149 } 150 } 151 } 152 /** 153 * 取得應用的版本號 154 * @return 155 */ 156 public String getVersion(){ 157 PackageManager pm = getPackageManager(); //取得包管理器的對象,這樣就能夠拿到應用程序的管理對象 158 try { 159 PackageInfo info = pm.getPackageInfo(getPackageName(), 0); //獲得應用程序的包信息對象 160 return info.versionName; //取得應用程序的版本號 161 } catch (NameNotFoundException e) { 162 e.printStackTrace(); 163 //此異常不會發生 164 return ""; 165 } 166 } 167 }
以上代碼中用到的UpdateInfoParser.getUpdateInfo()方法,以下:服務器
1 package lq.wangzhen.mobilesafe.engine; 2 3 import java.io.IOException; 4 import java.io.InputStream; 5 6 import org.xmlpull.v1.XmlPullParser; 7 import org.xmlpull.v1.XmlPullParserException; 8 9 import android.util.Xml; 10 11 import lq.wangzhen.mobilesafe.domain.UpdateInfo; 12 13 public class UpdateInfoParser { 14 15 public static UpdateInfo getUpdateInfo(InputStream is) throws XmlPullParserException, IOException{ 16 XmlPullParser parser = Xml.newPullParser(); //取得XmlPullParser解析器,準備對XML進行解析 17 parser.setInput(is, "UTF-8"); 18 UpdateInfo info = new UpdateInfo(); //實例化UpdateInfo對象 19 int type = parser.getEventType(); 20 while(type != XmlPullParser.END_DOCUMENT){ 21 if(type==XmlPullParser.START_TAG){ 22 if("version".equals(parser.getName())){ 23 String version = parser.nextText(); 24 info.setVersion(version); 25 }else if("description".equals(parser.getName())){ 26 String description = parser.nextText(); 27 info.setDescription(description); 28 }else if("apkurl".equals(parser.getName())){ 29 String apkurl = parser.nextText(); 30 info.setApkurl(apkurl); 31 } 32 } 33 type = parser.next(); 34 } 35 return info; 36 } 37 }
以上代碼中用到的UpdateInfo類,此類是根據服務器中提供的info.xml對象編寫的:網絡
1 package lq.wangzhen.mobilesafe.domain; 2 3 public class UpdateInfo { 4 5 private String version; //服務器上的版本號 6 private String description; //升級信息 7 private String apkurl; //apk下載地址 8 public String getVersion() { 9 return version; 10 } 11 public void setVersion(String version) { 12 this.version = version; 13 } 14 public String getDescription() { 15 return description; 16 } 17 public void setDescription(String description) { 18 this.description = description; 19 } 20 public String getApkurl() { 21 return apkurl; 22 } 23 public void setApkurl(String apkurl) { 24 this.apkurl = apkurl; 25 } 26 27 28 }
在layout文件夾中新創建main.xml佈局文件,做爲應用程序的主界面,以下:app
1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 android:layout_width="match_parent" 4 android:layout_height="match_parent" 5 android:orientation="vertical" > 6 7 <TextView 8 android:id="@+id/test" 9 android:layout_width="wrap_content" 10 android:layout_height="wrap_content" 11 android:text="mainUI"/> 12 13 </LinearLayout>
創建對應的MainActivity.java,加載main.xml:dom
1 package lq.wangzhen.mobilesafe; 2 3 import android.app.Activity; 4 import android.os.Bundle; 5 6 public class MainActivity extends Activity { 7 8 @Override 9 protected void onCreate(Bundle savedInstanceState) { 10 super.onCreate(savedInstanceState); 11 setContentView(R.layout.main); 12 } 13 14 }
在mainfest.xml文件中配置MainActivity:ide
1 <activity android:name="lq.wangzhen.mobilesafe.MainActivity"></activity>
由於要訪問網絡,別忘了加入訪問網絡的權限哦:佈局
1 <uses-permission android:name="android.permission.INTERNET"/>
對應的源碼下載地址:http://pan.baidu.com/share/link?shareid=1241370091&uk=1646729842ui