Teo*_*Teo 7 android dialog progress
我的应用程序从互联网上获取一些HTML代码,完成后,将其显示在设备屏幕上.由于这需要大约3-4秒,因此在这段时间屏幕保持黑色,我想使用进度对话框.这是我的代码:
package com.nextlogic.golfnews;
// ALL THE IMPORTS ....
public class Activity1 extends Activity {
private ProgressDialog progressDialog;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main2);
progressDialog = ProgressDialog.show(Activity1.this, "", "Loading...");
new Thread()
{
public void run()
{
try
{
sleep(2000);
// HERE I'VE PUT ALL THE FUNCTIONS THAT WORK FOR ME
}
catch (Exception e)
{
Log.e("tag",e.getMessage());
}
// dismiss the progressdialog
progressDialog.dismiss();
}
}.start();
Run Code Online (Sandbox Code Playgroud)
该程序有效但不再显示任何内容.我在logcat中有一个错误:
Only the original thread that created a view hierarchy can touch its views.
Run Code Online (Sandbox Code Playgroud)
请你帮助我好吗 ?提前致谢.
Fer*_*lez 17
该错误足以解释.要更新一个可视对象,必须在主线程内运行更改.快速而肮脏的修复可能是调用runOnUiThread()中的更新代码.
但是在您的情况下,我将使用AsyncTask下载并更新进度条的进度.该任务具有在UI线程结束时运行的属性(因此您可以更新其中的视图,例如关闭进度对话框)
以下是如何使用AsyncTask显示下载进度对话框的示例.
更新
Stackoverflow已经有你所有问题的答案.下面是一个AsyncTask示例,用于下载某些内容并显示下载进度.正是你想要的.
更新2
好的,这是使用AsyncTask的代码:
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();
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
32351 次 |
| 最近记录: |