編輯:Android開發實例
說Android是一款互聯網手機操作系統一點也不過為過。打開G2的"大抽屜”,一大堆的軟件,大部分都是需要網絡支持才能正常運行的。
曾經和同學說:沒有網絡支持,G2跟Nokia 1200沒啥區別!
既然Android定位為“網絡操作系統”,自然提供了很威水的網絡訪問接口。既有java.net.*,又有org.apache.http.*,在數據處理方面支持json,xml等常用的數據格式。
示例代碼如下:
1:
2: public class HttpDemo extends Activity {
3:
4: private static final int MSGWHAT = 0x000000;
5:
6: private TextView textView;
7: private Button btnLink;
8: private EditText etUrl;
9: private String data;
10: /**
11: * 消息控制器,用來更新界面,因為在普通線程是無法用來更新界面的
12: */
13: private Handler handler = new Handler() {
14: @Override
15: public void handleMessage(Message msg) {
16: switch (msg.what) {
17: case MSGWHAT:
18: //設置顯示文本
19: textView.setText(data);
20: break;
21: default:
22: break;
23: }
24: }
25: };
26:
27: /** Called when the activity is first created. */
28: @Override
29: public void onCreate(Bundle savedInstanceState) {
30: super.onCreate(savedInstanceState);
31: setContentView(R.layout.main);
32: //注意界面控件的初始化的位置,不要放在setContentView()前面
33: initComponent();
34: btnLink.setOnClickListener(new OnClickListener() {
35:
36: @Override
37: public void onClick(View v) {
38: //可以放在另外的線程完成
39: try {
40: data = getData();
41: } catch (ClientProtocolException e) {
42: data = e.getMessage();
43: } catch (IOException e) {
44: data = e.getMessage();
45: } catch (URISyntaxException e) {
46: data = e.getMessage();
47: }
48: Message msg = new Message();
49: msg.what = MSGWHAT;
50: handler.sendMessage(msg);
51: }
52: });
53: }
54:
55: /**
56: * 初始化界面組件
57: */
58: private void initComponent() {
59: textView = (TextView) findViewById(R.id.text);
60: btnLink = (Button) findViewById(R.id.btn_link);
61: etUrl = (EditText) findViewById(R.id.url);
62: }
63:
64: /**
65: * 取數據
66: * @return
67: * @throws ClientProtocolException
68: * @throws IOException
69: * @throws URISyntaxException
70: */
71: private String getData() throws ClientProtocolException, IOException, URISyntaxException {
72: String urlString = etUrl.getText().toString();
73: return request(urlString);
74: }
75:
76: /**
77: * 發送請求
78: * @param url 請求地址
79: * @return
80: * @throws ClientProtocolException
81: * @throws IOException
82: * @throws URISyntaxException
83: */
84: private String request(String url) throws ClientProtocolException, IOException, URISyntaxException {
85: HttpClient httpClient = new DefaultHttpClient();
86: URI uri = URIUtils.createURI("http", url, -1, null, null, null);
87: HttpGet get = new HttpGet(uri);
88: ResponseHandler<String> responseHandler = new BasicResponseHandler();
89: return httpClient.execute(get, responseHandler);
90: }
91:
92: @Override
93: protected void onDestroy() {
94: super.onDestroy();
95: System.gc();
96: System.exit(0);
97: }
98: }
轉自:http://www.cnblogs.com/halzhang/archive/2010/05/28/1746591.html
前面文章介紹了Activity以及Intent的使用,本文就來介紹Service。如果把Activity比喻為前台程序,那麼Service就是後台程序,Servi
微信平台開放後倒是挺火的,許多第三方應用都想試下接入微信這個平台,畢竟可以利用微信建立起來的關系鏈來拓展自己的應用還是挺不錯的,可以節約很多在社交方面的開銷,我最
屏幕亮度自動調節:主要是從Sensor分析之中分離出來分析LIGHT 光線感應器,因此就分析一下自動調節屏幕亮度(手機隨著光線的強度自我調節,也就是在亮的光線下屏
Google為ndroid平台開發Web Service提供了支持,提供了Ksoap2-android相關架包 1.下載該夾包可以直接登錄http://code.