一. 使用eclipse與設備連接進行斷點調試 2013-02-26
1)將Y:\[project_name]\development\ide\eclipse下的.classpath拷貝到Y:\[project_name]\下。
2)Eclipse啓動,file->new->project,導入工程。
3)連接設備,打開DDMS,在device列表中的進程選中你要調試的進程。
4)設置debug。如下圖。
Project選擇調試項目。
Port選擇遠程端口,這裏是8700。
最後按debug。
出現如下綠色標記,代表設置成功。
具體端口視具體情況而定。
5)在代碼中打上斷點,其他操作如同調試java一般程序那樣。
1. AsyncTask介紹與應用 2013-02-25
轉載自:http://blog.csdn.net/bd_zengxinxin/article/details/8504696
先大概認識下Android.os.AsyncTask類:
* android的類AsyncTask對線程間通訊進行了包裝,提供了簡易的編程方式來使後臺線程和UI線程進行通訊:後臺線程執行異步任務,並把操作結果通知UI線程。
* AsyncTask是抽象類.AsyncTask定義了三種泛型類型 Params,Progress和Result。
* Params 啓動任務執行的輸入參數,比如HTTP請求的URL。
* Progress 後臺任務執行的百分比。
* Result 後臺執行任務最終返回的結果,比如String,Integer等。
* AsyncTask的執行分爲四個步驟,每一步都對應一個回調方法,開發者需要實現這些方法。
* 1) 繼承AsyncTask
* 2) 實現AsyncTask中定義的下面一個或幾個方法
* onPreExecute(), 該方法將在執行實際的後臺操作前被UI 線程調用。可以在該方法中做一些準備工作,如在界面上顯示一個進度條,或者一些控件的實例化,這個方法可以不用實現。
* doInBackground(Params...), 將在onPreExecute 方法執行後馬上執行,該方法運行在後臺線程中。這裏將主要負責執行那些很耗時的後臺處理工作。可以調用 publishProgress方法來更新實時的任務進度。該方法是抽象方法,子類必須實現。
* onProgressUpdate(Progress...),在publishProgress方法被調用後,UI 線程將調用這個方法從而在界面上展示任務的進展情況,例如通過一個進度條進行展示。
* onPostExecute(Result), 在doInBackground 執行完成後,onPostExecute 方法將被UI 線程調用,後臺的計算結果將通過該方法傳遞到UI 線程,並且在界面上展示給用戶.
* onCancelled(),在用戶取消線程操作的時候調用。在主線程中調用onCancelled()的時候調用。
爲了正確的使用AsyncTask類,以下是幾條必須遵守的準則:
1) Task的實例必須在UI 線程中創建
2) execute方法必須在UI 線程中調用
3) 不要手動的調用onPreExecute(), onPostExecute(Result),doInBackground(Params...), onProgressUpdate(Progress...)這幾個方法,需要在UI線程中實例化這個task來調用。
4) 該task只能被執行一次,否則多次調用時將會出現異常
doInBackground方法和onPostExecute的參數必須對應,這兩個參數在AsyncTask聲明的泛型參數列表中指定,第一個爲doInBackground接受的參數,第二個爲顯示進度的參數,第第三個爲doInBackground返回和onPostExecute傳入的參數。
下面通過一個Demo來說明如何使用Android.os.AsyncTask類,通過進度條來顯示進行的進度,然後用TextView來顯示進度值。程序結構圖如下:
[1] \layout\main.xml 佈局文件源碼如下:
[html] view plaincopy
1 <?xml version="1.0" encoding="utf-8"?>
2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
3 android:orientation="vertical"
4 android:layout_width="fill_parent"
5 android:layout_height="fill_parent"
6 >
7 <TextView
8 android:layout_width="fill_parent"
9 android:layout_height="wrap_content"
10 android:text="Hello , Welcome to Andy's Blog!"/>
11 <Button
12 android:id="@+id/download"
13 android:layout_width="fill_parent"
14 android:layout_height="wrap_content"
15 android:text="Download"/>
16 <TextView
17 android:id="@+id/tv"
18 android:layout_width="fill_parent"
19 android:layout_height="wrap_content"
20 android:text="當前進度顯示"/>
21 <ProgressBar
22 android:id="@+id/pb"
23 android:layout_width="fill_parent"
24 android:layout_height="wrap_content"
25 style="?android:attr/progressBarStyleHorizontal"/>
26 </LinearLayout>
[2] /src中的MainActivity.java源碼如下:
[html] view plaincopy
27 package com.andyidea.demo;
28
29 import android.app.Activity;
30 import android.os.AsyncTask;
31 import android.os.Bundle;
32 import android.view.View;
33 import android.widget.Button;
34 import android.widget.ProgressBar;
35 import android.widget.TextView;
36
37 public class MainActivity extends Activity {
38
39 Button download;
40 ProgressBar pb;
41 TextView tv;
42
43 /** Called when the activity is first created. */
44 @Override
45 public void onCreate(Bundle savedInstanceState) {
46 super.onCreate(savedInstanceState);
47 setContentView(R.layout.main);
48 pb=(ProgressBar)findViewById(R.id.pb);
49 tv=(TextView)findViewById(R.id.tv);
50
51 download = (Button)findViewById(R.id.download);
52 download.setOnClickListener(new View.OnClickListener() {
53 @Override
54 public void onClick(View v) {
55 DownloadTask dTask = new DownloadTask();
56 dTask.execute(100);
57 }
58 });
59 }
60
61 class DownloadTask extends AsyncTask<Integer, Integer, String>{
62 //後面尖括號內分別是參數(例子裏是線程休息時間),進度(publishProgress用到),返回值 類型
63
64 @Override
65 protected void onPreExecute() {
66 //第一個執行方法
67 super.onPreExecute();
68 }
69
70 @Override
71 protected String doInBackground(Integer... params) {
72 //第二個執行方法,onPreExecute()執行完後執行
73 for(int i=0;i<=100;i++){
74 pb.setProgress(i);
75 publishProgress(i);
76 try {
77 Thread.sleep(params[0]);
78 } catch (InterruptedException e) {
79 e.printStackTrace();
80 }
81 }
82 return "執行完畢";
83 }
84
85 @Override
86 protected void onProgressUpdate(Integer... progress) {
87 //這個函數在doInBackground調用publishProgress時觸發,雖然調用時只有一個參數
88 //但是這裏取到的是一個數組,所以要用progesss[0]來取值
89 //第n個參數就用progress[n]來取值
90 tv.setText(progress[0]+"%");
91 super.onProgressUpdate(progress);
92 }
93
94 @Override
95 protected void onPostExecute(String result) {
96 //doInBackground返回時觸發,換句話說,就是doInBackground執行完後觸發
97 //這裏的result就是上面doInBackground執行後的返回值,所以這裏是"執行完畢"
98 setTitle(result);
99 super.onPostExecute(result);
100 }
101
102 }
103 }
[3] 下面看下程序的運行結果截圖: