如何在 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();
I have a function that dynamically creates all my user interface.
What can I do to show a dialog progress while my function is executing, and then dismiss the dialog when my function has finished the user interface?
This is an example of my code:
Sorry, I'm new to android, it is hard for me to understand some code... I will write my code here...
I have this function:
public void principal() {
//CODE TO CREATE ALL THE USER INTERFACE DYNAMICALLY
}
and I have the asyncTask like this:
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();
}
}
When I execute the asynctask in the onCreate, it crashes:
new EjecutarTarea().execute();
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
使用 AsyncTask。它提供了一种奇妙而简单的方法来在后台加载内容并将它们粘贴到主线程中。
以下是我的 AsyncTask 的示例:
编辑注意 我建议不要保留
setProgressAndSleep(int, int int)
方法。我使用它只是因为它加载太快而且我真的想要加载栏。 :-PUse an AsyncTask. It gives a fantastic and easy way to load stuff in the background and them paste views in the main thread.
Here is an example of my AsyncTask:
Edit NOTE I recommend not keeping the
setProgressAndSleep(int, int int)
method. I only use it cause it loads too fast and I really wanted the loading bar. :-PAndroid UI 中的所有更改都将在 UI 线程 中进行。要在 UI 线程中运行函数,您需要使用 Activity http://developer.android.com/reference/android/app/Activity.html#runOnUiThread(java.lang.Runnable)
希望对您有所帮助!
All changes in Android UI will makes in UI Thread. To run your function in UI thread you need to use mathos runOnUIThread() from activity http://developer.android.com/reference/android/app/Activity.html#runOnUiThread(java.lang.Runnable)
Hope it help you!
您最好的办法可能是使用 ViewSwitcher 来获得临时视图构建其他视图时的进度条。在
AsyncTask
的doInBackground()
中执行需要时间的操作,但 UI 上的实际操作(例如翻转视图)必须在postExecute()
中完成,或者通过runOnUIThread()
。进度更新可以在onProgressUpdate()
中完成Your best shot is, probably, to use ViewSwitcher to have temporary view with progress bar while you construct other view. Do operations that take time in
doInBackground()
ofAsyncTask
but actual operations on UI such as flipping the view must be done inpostExecute()
or viarunOnUIThread()
. Progress updates may be done inonProgressUpdate()