Android:如何使用后台线程?

标签 android multithreading background

我开发了一个应用程序,可以从 Internet 获取内容并将其相应地显示在设备的屏幕上。该程序工作正常,有点慢。加载和显示内容大约需要 3-4 秒。我想把所有获取内容并在后台线程中显示它的代码放在一起,当程序执行这些功能时,我想显示一个进度对话框。你能帮我做这个吗?我特别想学习如何将代码放在后台线程中。

我的代码

public class Activity1 extends Activity
{
    private ProgressDialog progressDialog;

    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        new AsyncTask<Integer, Integer, Boolean>()
        {
            ProgressDialog progressDialog;

            @Override
            protected void onPreExecute()
            {
                /*
                 * This is executed on UI thread before doInBackground(). It is
                 * the perfect place to show the progress dialog.
                 */
                progressDialog = ProgressDialog.show(Activity1.this, "",
                        "Loading...");
            }

            @Override
            protected Boolean doInBackground(Integer... params)
            {
                if (params == null)
                {
                    return false;
                }
                try
                {
                    /*
                     * This is run on a background thread, so we can sleep here
                     * or do whatever we want without blocking UI thread. A more
                     * advanced use would download chunks of fixed size and call
                     * publishProgress();
                     */
                    Thread.sleep(params[0]);
                    // HERE I'VE PUT ALL THE FUNCTIONS THAT WORK FOR ME
                }
                catch (Exception e)
                {
                    Log.e("tag", e.getMessage());
                    /*
                     * The task failed
                     */
                    return false;
                }

                /*
                 * The task succeeded
                 */
                return true;
            }

            @Override
            protected void onPostExecute(Boolean result)
            {
                progressDialog.dismiss();
                /*
                 * Update here your view objects with content from download. It
                 * is save to dismiss dialogs, update views, etc., since we are
                 * working on UI thread.
                 */
                AlertDialog.Builder b = new AlertDialog.Builder(Activity1.this);
                b.setTitle(android.R.string.dialog_alert_title);
                if (result)
                {
                    b.setMessage("Download succeeded");
                }
                else
                {
                    b.setMessage("Download failed");
                }
                b.setPositiveButton(getString(android.R.string.ok),
                        new DialogInterface.OnClickListener()
                        {

                            @Override
                            public void onClick(DialogInterface dlg, int arg1)
                            {
                                dlg.dismiss();
                            }
                        });
                b.create().show();
            }
        }.execute(2000);

        new Thread()
        {
            @Override
            public void run()
            {

                // dismiss the progressdialog
                progressDialog.dismiss();
            }
        }.start();
    }
}

最佳答案

检查 ASyncTask ,它是专门为此类任务创建的。

关于Android:如何使用后台线程?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7714299/

相关文章:

android - 单击时更改 RadioButton 的颜色

java - 从 UncaughtExceptionHandler 抛出异常

css - 如何使用 CSS 制作具有半透明效果的整页背景图片?

iphone - 在iPhone Web App中播放背景声音

html - Jumbotron 背景图像与 HTML 背景图像不同步

c# - 在 PCL,Xamarin 中读取文件

android - DialogFragment 的简单示例 - 示例代码不适合我

java - 将静态字段传递给线程

在单独的线程上运行的android服务

multithreading - 在原子上调用 `into_inner()` 是否考虑了所有宽松的写入?