如何在 android 的线程中运行我的函数?

发布于 2024-12-09 16:55:43 字数 1025 浏览 0 评论 0原文

我有一个可以动态创建所有用户界面的函数。

我可以做什么来在函数执行时显示对话框进度,然后在函数完成用户界面后关闭对话框?

这是我的代码示例:

抱歉,我是 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

怀里藏娇 2024-12-16 16:55:43

使用 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

Use 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:

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);
            }
        }
    }

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. :-P

骄兵必败 2024-12-16 16:55:43

Android 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!

智商已欠费 2024-12-16 16:55:43

您最好的办法可能是使用 ViewSwitcher 来获得临时视图构建其他视图时的进度条。在 AsyncTaskdoInBackground() 中执行需要时间的操作,但 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() of AsyncTask but actual operations on UI such as flipping the view must be done in postExecute() or via runOnUIThread(). Progress updates may be done in onProgressUpdate()

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文