[英]How can I run my function in a thread in android?
我有一個動態創建所有用戶界面的功能。
在執行函數時如何顯示對話框進度,然后在函數完成用戶界面后關閉對話框?
這是我的代碼的示例:
抱歉,我是android的新手,我很難理解一些代碼...我將在這里編寫代碼...
我有這個功能:
public void principal() {
//CODE TO CREATE ALL THE USER INTERFACE DYNAMICALLY
}
我有這樣的asyncTask:
public class EjecutarTarea extends AsyncTask<Void, Void, Void>
{
protected void onPreExecute() {
dialog = new ProgressDialog(StadioNenaActivity.this);
dialog.setMessage("Cargando..");
dialog.setIndeterminate(true);
dialog.setCancelable(false);
dialog.show();
}
protected Void doInBackground(Void... unused) {
Principal();
return null;
}
@Override
protected void onProgressUpdate(Void... unused) {
}
@Override
protected void onPostExecute(Void unused) {
dialog.dismiss();
}
}
當我在onCreate中執行asynctask時,它崩潰了:
new EjecutarTarea().execute();
使用AsyncTask 。 它提供了一種奇妙而又簡單的方法來在后台加載內容,並將其粘貼到主線程中。
這是我的AsyncTask的示例:
private class LoadingTask extends AsyncTask<Void, Integer, Void> {
private ProgressBar mProg;
private TextView mLoadingText;
@Override protected void onPreExecute() {
mProg = (ProgressBar)findViewById(R.id.launcherbar_loadprogress);
mProg.setTouchDelegate(null);
mProg.setClickable(false);
mProg.setLongClickable(false);
mProg.setOnTouchListener(null);
mProg.setMax(100);
mLoadingText = (TextView) findViewById(R.id.launcheractivity_loadingwhat);
}
@Override protected Void doInBackground(Void... voids) {
try { Thread.sleep(1000); } catch (InterruptedException e) { }
setProgressAndSleep(R.string.loading_log, 29, 250);
LOG.setContext(LauncherActivity.this);
LOG.setDebug(true);
setProgressAndSleep(R.string.loading_database, 43, 250);
AppionDatabase.setContext(LauncherActivity.this);
setProgressAndSleep(R.string.loading_sensors, 57, 250);
Sensor.init(LauncherActivity.this);
setProgressAndSleep(R.string.loading_jobs, 71, 250);
Job.init(LauncherActivity.this);
setProgressAndSleep(R.string.loading_workbenches, 86, 250);
WorkbenchState.init(LauncherActivity.this);
setProgressAndSleep(R.string.loading_application_state, 100, 250);
ApplicationState.setContext(LauncherActivity.this);
startService(new Intent(LauncherActivity.this, BluetoothConnectionService.class));
return null;
}
@Override public void onProgressUpdate(Integer... prog) {
mLoadingText.setText(prog[0]);
mProg.setProgress(prog[1]);
}
@Override public void onPostExecute(Void voids) {
startActivity(new Intent(LauncherActivity.this, HomescreenActivity.class));
}
private void setProgressAndSleep(int text, int progress, int duration) {
try {
publishProgress(new Integer[] {text, progress});
Thread.sleep(duration);
} catch (InterruptedException e) {
LOG.e(TAG, "Failed to sleep thread while loading Application Contexts!", e);
}
}
}
編輯注意我建議不要保留setProgressAndSleep(int, int int)
方法。 我只使用它,因為它加載速度太快了,我真的很想要加載欄。 :-P
Android UI中的所有更改都將在UI Thread中進行 。 要在UI線程中運行函數,您需要使用http://developer.android.com/reference/android/app/Activity.html#runOnUiThread(java.lang.Runnable )活動中的mathos runOnUIThread()
希望對您有幫助!
最好的選擇可能是在構建其他視圖時,使用ViewSwitcher臨時顯示帶有進度條的視圖。 在AsyncTask
doInBackground()
中執行doInBackground()
操作,但在UI上進行的實際操作(如翻轉視圖)則必須在postExecute()
或通過runOnUIThread()
。 進度更新可以在onProgressUpdate()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.