編輯:關於Android編程
異步任務AsyncTask
AsyncTask主要用來更新UI線程,比較耗時的操作可以在AsyncTask中使用。
AsyncTask是個抽象類,使用時需要繼承這個類,然後調用execute()方法。注意繼承時需要設定三個泛型Params,Progress和Result的類型,如AsyncTask
異步任務所使用的三種類型:
Params
,發送到任務的類型參數
執行。<喎?/kf/ware/vc/" target="_blank" class="keylink">vcD4KPHA+PC9wPgo8Y2VudGVyPjxicj4KPC9jZW50ZXI+CjxjZW50ZXI+MqGiuPm+3dDo0qrKtc/WQXN5bmNUYXNrtcTI58/Ct723qKO6PC9jZW50ZXI+CjxjZW50ZXI+PC9jZW50ZXI+CjxjZW50ZXI+PC9jZW50ZXI+CjxwPjwvcD4KPGNlbnRlcj4gICAgICAgKDEpb25QcmVFeGVjdXRlKCk6uMO3vbeovavU2ta00NC688yousTKsbLZ1/dkb0luQmFja2dyb3VuZCgpt723qMewsbu199PDoaPNqLOjuMO3vbeo08PT2s3qs8nSu9Cps/XKvLuvtcTXvLG4uaTX96OsscjI59TavefD5snPz9TKvr34tsjM9bXIoaM8L2NlbnRlcj4KPHA+PC9wPgo8Y2VudGVyPiAgICAgPC9jZW50ZXI+CjxjZW50ZXI+ICAgICCjqDKjqWRvSW5CYWNrZ3JvdW5kKFBhcmFtcy4uLik61tjQtLjDt723qL7NysejrLrzzKjP37PMvavSqs3qs8m1xLrEyrHIzs7xo6y4w7e9t6jW0LK7xNzQ3rjEVUmhozwvY2VudGVyPgo8cD48L3A+CjxwPjwvcD4KPGNlbnRlcj4gICAgIDwvY2VudGVyPgo8Y2VudGVyPiAgICAgo6gzo6lvblByb2dyZXNzVXBkYXRlo6hQcm9ncmVzcy4uLnZhbHVlc6OpOtTaZG9JbkJhY2tncm91bmSjqKOpuMO3vbeo1tC199PDcHVibGlzaFByb2dyZXNzo6ijqbe9t6i4/NDCyM7O8bXE1rTQ0L34tsi686Osvau74bSlt6K4w7e9t6ijrLOjs6PTw9PauPzQwr34tsjM9aGjPC9jZW50ZXI+Cjx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cSjrLT4vfi2yMz1oaMKPGltZyBzcmM9"" alt="\">
package com.example.asynctestdemo; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.InputStream; import org.apache.http.HttpEntity; import org.apache.http.HttpResponse; import org.apache.http.HttpStatus; import org.apache.http.client.ClientProtocolException; import org.apache.http.client.HttpClient; import org.apache.http.client.methods.HttpGet; import org.apache.http.impl.client.DefaultHttpClient; import android.app.Activity; import android.os.AsyncTask; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.ProgressBar; import android.widget.TextView; public class MainActivity extends Activity { private Button bt_execute; private Button bt_cancel; private ProgressBar progressBar; private TextView textView; MyTask myTask; protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); bt_execute = (Button) findViewById(R.id.bt_execute); bt_cancel = (Button) findViewById(R.id.bt_cancel); progressBar = (ProgressBar) findViewById(R.id.progressBar); textView = (TextView) findViewById(R.id.text_view); bt_execute.setOnClickListener(new OnClickListener() { public void onClick(View v) { // 實例化異步任務MyTask類 myTask = new MyTask(); myTask.execute("http://www.baidu.com"); } }); bt_cancel.setOnClickListener(new OnClickListener() { public void onClick(View v) { myTask.cancel(true); } }); } class MyTask extends AsyncTask{ // onpreExecute()方法用於,在執行後台任務前,做一些UI操作 protected void onPreExecute() { textView.setText("Loading......"); } // doInBackground()方法,內部執行後台耗時任務操作,但不是不能在此方法內修改UI protected String doInBackground(String... params) { try { HttpClient client = new DefaultHttpClient(); HttpGet get = new HttpGet(params[0]); HttpResponse response = client.execute(get); if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) { HttpEntity entity = response.getEntity(); InputStream is = entity.getContent(); long total = entity.getContentLength(); System.out.println(total); ByteArrayOutputStream baos = new ByteArrayOutputStream(); byte[] buf = new byte[1024]; int count = 0; int length = -1; while ((length = is.read(buf)) != -1) { baos.write(buf, 0, length); count+=length; // 調用publishProgress()方法公布進度,最後onProgressUpdata方法將被執行 publishProgress((int) (count/length)); //為了更明顯的展現效果,每次暫停0.5s Thread.sleep(500); } return new String(baos.toByteArray(), "utf-8"); } } catch (ClientProtocolException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } catch (InterruptedException e) { e.printStackTrace(); } return null; } // onProgressUpdata方法,用於更新進度信息,例如執行進度條程序 protected void onProgressUpdate(Integer... values) { progressBar.setProgress(values[0]); textView.setText("Loading......" + values[0]); } // onPostExecute方法,用於在執行完後台任務後,更新UI,顯示結果 protected void onPostExecute(String result) { textView.setText(result); } // onCacelled方法,用於在取消執行中的任務時,更改UI protected void onCancelled() { textView.setText("cancelled"); progressBar.setProgress(0); } } }
先上效果圖:本篇文章我們來學習一個開源項目Android-ItemTouchHelper-Demo這個項目使用了RecyclerView的ItemTouchHelper類
什麼是dex文件他是Android系統的可執行文件,包含應用程序的全部操作指令以及運行時數據。由於dalvik是一種針對嵌入式設備而特殊設計的java虛擬機,所以dex文
android的view中有setPadding,但是沒有直接的setMargin方法。如果要在代碼中設置該怎麼做呢?可以通過設置view裡面的 LayoutParams
前兩天在開發在微信訪問的HTML5頁面,裡面有個訂單查詢要選擇時間,剛開始使用的<input type="date">輸入框,沒加任何的樣