Android的后台线程 [英] Android background thread
问题描述
我已经开发了一个应用程序,采用内容从互联网上,并相应地显示在设备屏幕上。该项目工作得很好,有点慢。它需要大约3-4秒,以加载和显示的内容。我愿把所有的code,它获取的内容,并在后台线程中显示,并在程序做这些功能,我想显示一个进度对话框。你能帮我做到这一点?我想特别是学习如何把code在后台线程。
我的code
公共类活性1扩展活动
{
私人ProgressDialog progressDialog;
@覆盖
公共无效的onCreate(包savedInstanceState)
{
super.onCreate(savedInstanceState);
的setContentView(R.layout.main);
新的AsyncTask<整型,整型,布尔>()
{
ProgressDialog progressDialog;
@覆盖
在preExecute保护无效()
{
/ *
*这是doInBackground之前,UI线程上执行的()。它是
*完美的地方,以显示进度对话框。
* /
progressDialog = ProgressDialog.show(Activity1.this,,
载入中...);
}
@覆盖
保护布尔doInBackground(整数... PARAMS)
{
如果(PARAMS == NULL)
{
返回false;
}
尝试
{
/ *
*这是运行在后台线程,所以我们可以在这里睡
*或做任何我们想做而不阻塞UI线程。更
*先进的用途是下载固定的大小和呼叫大块
* publishProgress();
* /
视频下载(PARAMS [0]);
//这里我已经把所有的工作对我来说是功能
}
赶上(例外五)
{
Log.e(标签,e.getMessage());
/ *
*任务失败
* /
返回false;
}
/ *
*任务成功
* /
返回true;
}
@覆盖
保护无效onPostExecute(布尔结果)
{
progressDialog.dismiss();
/ *
*与下载内容更新在这里您的视图对象。它
*为节省解雇对话框,更新观点,等等,因为我们是
*工作UI线程。
* /
AlertDialog.Builder B =新AlertDialog.Builder(Activity1.this);
b.setTitle(android.R.string.dialog_alert_title);
如果(结果)
{
b.setMessage(下载成功);
}
其他
{
b.setMessage(下载失败);
}
b.setPositiveButton(的getString(android.R.string.ok)
新DialogInterface.OnClickListener()
{
@覆盖
公共无效的onClick(DialogInterface DLG,诠释ARG1)
{
dlg.dismiss();
}
});
。b.create()显示();
}
} .execute(2000);
新的Thread()
{
@覆盖
公共无效的run()
{
//驳回progressdialog
progressDialog.dismiss();
}
}。开始();
}
}
检查 AsyncTask的,它专门为这样的任务创建。
I've developed an application that takes content from the internet and shows it accordingly on the device's screen . The program works just fine , a little bit slow . It takes about 3-4 seconds to load and display the content . I would like to put all the code that fetches the content and displays it in a background thread and while the program is doing those functions , I would like to display a progress dialog. Could you help me do this ? I would like especially to learn how to put the code in a background thread.
MY CODE
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();
}
}
Check ASyncTask, its specifically created for such tasks.
这篇关于Android的后台线程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!