標籤:des android style blog http color io os ar
今天學習的主要是SplashActivity頁面的初始化,其中涉及很多知識點
1 擷取包名 擷取版本號碼資訊
2 檢查升級,以及利用第三方工具對json資料的解析
3 Thread線程和message的運用
package com.djf.mobilesafty;import java.io.IOException;import java.io.InputStream;import java.net.HttpURLConnection;import java.net.MalformedURLException;import java.net.URL;import org.json.JSONException;import org.json.JSONObject;import com.djf.mobilesafty.utils.StreamTools;import android.app.Activity;import android.content.Intent;import android.content.pm.PackageInfo;import android.content.pm.PackageManager;import android.content.pm.PackageManager.NameNotFoundException;import android.os.Bundle;import android.os.Handler;import android.os.Message;import android.text.Annotation;import android.util.Log;import android.view.Menu;import android.view.MenuItem;import android.view.animation.AlphaAnimation;import android.widget.TextView;import android.widget.Toast;public class SplashActivity extends Activity { protected static final String TAG = "SplashActivity"; protected static final int ENTER_HOME = 0; protected static final int SHOW_DIALOG = 1; protected static final int URL_ERROR = 2; protected static final int NET_ERROR = 3; protected static final int JSON_ERROR = 4; private TextView tv_splash_verison; private String description; private String apkurl; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); tv_splash_verison = (TextView) findViewById(R.id.tv_splash_verison); tv_splash_verison.setText("版本號碼:" + getVersionName()); AlphaAnimation aa = new AlphaAnimation(0.2f, 1.0f); aa.setDuration(500); findViewById(R.layout.activity_main).startAnimation(aa); // 檢查升級 checkupdate(); } private Handler handler = new Handler() { public void handleMessage(Message msg) { super.handleMessage(msg); switch (msg.what) { case ENTER_HOME:// 進入首頁面 EnterHome(); break; case SHOW_DIALOG:// 顯示對話方塊 Log.i(TAG, "顯示升級對話方塊"); break; case URL_ERROR:// url錯誤 EnterHome(); Toast.makeText(getApplicationContext(), "url錯誤", 0).show(); break; case NET_ERROR:// 網路錯誤 EnterHome(); Toast.makeText(SplashActivity.this, "網路錯誤", 0).show(); break; case JSON_ERROR:// json解析出錯 EnterHome(); Toast.makeText(SplashActivity.this, "json解析出錯", 0).show(); break; default: break; } } }; protected void EnterHome() { // TODO Auto-generated method stub Intent intent = new Intent(this, HomeActivity.class); startActivity(intent); // 關閉當前頁面 finish(); }; /** * 檢查是否需要升級 */ private void checkupdate() { // http://125.39.51.195:8080/updatinfo.html new Thread() { public void run() { Message msg = Message.obtain(); long starttime = System.currentTimeMillis(); try { URL url = new URL(getString(R.string.serverurl)); HttpURLConnection connection = (HttpURLConnection) url .openConnection(); connection.setRequestMethod("GET"); connection.setReadTimeout(4000); int code = connection.getResponseCode(); if (code == 200) { InputStream in = connection.getInputStream(); // 把流轉化成string String result = StreamTools.readFromStream(in); Log.i(TAG, "連網成功" + result); // json解析 JSONObject obj = new JSONObject(result); // 得到伺服器的版本資訊 String version = (String) obj.get("version"); description = (String) obj.get("description"); apkurl = (String) obj.get("apkurl"); // 校正是否有新版本 if (getVersionName().equals(version)) { // 版本一致 msg.what = ENTER_HOME; } else { // 版本不一致 msg.what = SHOW_DIALOG; } } } catch (MalformedURLException e) { msg.what = URL_ERROR; e.printStackTrace(); } catch (IOException e) { msg.what = NET_ERROR; e.printStackTrace(); } catch (JSONException e) { // TODO Auto-generated catch block msg.what = JSON_ERROR; e.printStackTrace(); } finally { long endtime = System.currentTimeMillis(); long dtime = endtime - starttime; if (dtime < 2000) { try { Thread.sleep(2000 - dtime); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } handler.sendMessage(msg); } }; }.start(); } /** * 得到應用程式資訊的版本 */ private String getVersionName() { PackageManager pManager = getPackageManager(); try { PackageInfo info = pManager.getPackageInfo(getPackageName(), 0); return info.versionName; } catch (NameNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } return ""; }}
手機安全衛士的學習(1)