标签: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 ""; } }
标签:des android style blog http color io os ar
原文地址:http://www.cnblogs.com/jiefeiduan/p/4044478.html