1 實現效果說明:
1.1
載入題目資訊之前,顯示正在載入
1.2載入題目資訊成功後,進行題目綁定
1.3載入不成功,轉到載入失敗
2 非同步載入實現
2.1 介面設定
載入前介面
View Code
<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#ffffff"
android:orientation="horizontal"
>
<ProgressBar
android:id="@+id/circleProgressBar"
style="?android:attr/progressBarStyleLarge" mce_style="?android:attr/progressBarStyleLarge"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="gone"
android:max="100"
android:progress="20"
/>
<TextView android:text="載入資訊中。。。。。"
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:textColor="#000000"
android:layout_height="wrap_content"></TextView>
</LinearLayout>
載入失敗介面
<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#ffffff"
android:orientation="horizontal"
>
<TextView android:text="網路異常,不能載入資料" android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content"></TextView>
<Button android:text="重新載入" android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
</LinearLayout>
2.2 建立自訂非同步類
//AsyncTask class AsyncLoader_GuessInfo extends AsyncTask<String, Void, Integer>{ @Override protected Integer doInBackground(String... params) { int result=0; try{ gi=GuessDataServiceHelper.GetGuessInfo(params[0]); result=2; } catch(Exception ex){ result=-1; } return result; } }
2.3 調用非同步類,並根據反饋結果進行顯示不同的內容
View Code
/*
* 負責做下載題目資訊
* */
private BegindownHandler mbegindownHandler = new BegindownHandler();
class BegindownHandler extends Handler {
@Override
public void handleMessage(Message msg) {
AsyncTask<String,Void, Integer> results= new AsyncLoader_GuessInfo().execute(GuessNo);
try{
if( results.get()==2)
{
LoadAndBandingData(GuessNo);
}
else
setContentView(R.layout.async_error);
}
catch(Exception ex)
{
setContentView(R.layout.async_error);
}
}
public void sleep(long delayMillis) {
this.removeMessages(0);
sendMessageDelayed(obtainMessage(0), delayMillis);
}
};