android:等待连接时显示进度对话框
我试图在启动新活动时添加一个进度对话框,该活动必须等待来自互联网的响应。目前,等待时屏幕会变黑。有谁知道它需要放置在哪里才能工作吗?
这个progressDialog:
ProgressDialog dialog = ProgressDialog.show(SearchActivity.this, "", "Loading. Please wait...", true);
dialog.dismiss();
这是在overlayActivity中扩展了ItemizedOverlay:
@Override
protected boolean onTap(int index) {
final OverlayItem item = (OverlayItem) items.get(index);
final Context mContext = context;
AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
builder.setTitle(item.getTitle())
.setCancelable(true)
.setPositiveButton("View Details", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
Intent intent = new Intent(mContext, Profile.class);
intent.putExtra("id", item.getSnippet());
mContext.startActivity(intent);
}
});
AlertDialog alert = builder.create();
alert.show();
return true;
}
这是Profile活动:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.profile);
Bundle extras = getIntent().getExtras();
String id;
if (extras != null) {
id = extras.getString("id");
String xml = XMLfunctions.getXMLFromBarId(id); // makes httpPost call
Document doc = XMLfunctions.XMLfromString(xml);
NodeList nodes = doc.getElementsByTagName("result");
Element e = (Element)nodes.item(0);
// rest of profile created here
}
}
I am trying to add a progress dialog when a new activity is launched that has to wait for a response from the internet. At the moment the screen just goes black while it is waiting. Does any one know where it needs to be placed to work?
this progressDialog:
ProgressDialog dialog = ProgressDialog.show(SearchActivity.this, "", "Loading. Please wait...", true);
dialog.dismiss();
this is in the overlayActivity extends ItemizedOverlay:
@Override
protected boolean onTap(int index) {
final OverlayItem item = (OverlayItem) items.get(index);
final Context mContext = context;
AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
builder.setTitle(item.getTitle())
.setCancelable(true)
.setPositiveButton("View Details", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
Intent intent = new Intent(mContext, Profile.class);
intent.putExtra("id", item.getSnippet());
mContext.startActivity(intent);
}
});
AlertDialog alert = builder.create();
alert.show();
return true;
}
and this is the Profile activity:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.profile);
Bundle extras = getIntent().getExtras();
String id;
if (extras != null) {
id = extras.getString("id");
String xml = XMLfunctions.getXMLFromBarId(id); // makes httpPost call
Document doc = XMLfunctions.XMLfromString(xml);
NodeList nodes = doc.getElementsByTagName("result");
Element e = (Element)nodes.item(0);
// rest of profile created here
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您应该使用进度对话框。进度对话框应在配置文件活动中使用。
您可以使用以下代码:
You should use Progress dialog. Progress dialog should be used in the Profile activity.
You can use the following code:
在 UI 线程(调用“onCreate”的线程)中进行网络调用是一个坏主意。它将停止 UI 的刷新,直到网络操作完成。相反,在 onCreate 中生成一个新线程,如下所示:
另外,我建议使用 ProgressDialog 来显示进度(一旦线程中的代码完成,您可以将其关闭)。教程: https://developer.android.com/guide/topics/ui/ dialogs.html
注意:您无法从新线程中关闭对话框,因此您必须使用处理程序将消息从线程发布到 UI 线程。这是一个教程: https://web.archive.org/web/20200810154212/http://www.tutorialforandroid.com/2009/01/using-handler-in-android.html
例子:
在您的 Profile 活动类中,添加以下内容:
Doing network calls in the UI thread (the thread which calls "onCreate") is a bad idea. It will stall the refresh of the UI till the network operation is completed. Instead, spawn a new thread in onCreate like so:
Also, I'd recommend using a ProgressDialog to show progress (which you can dismiss, once the code in the thread is done). Tutorial: https://developer.android.com/guide/topics/ui/dialogs.html
Note: You cannot dismiss the dialog from the new thread, so you will have to use a Handler to post a message from the thread to the UI thread. Here is a tutorial for that: https://web.archive.org/web/20200810154212/http://www.tutorialforandroid.com/2009/01/using-handler-in-android.html
Example:
In your Profile activity class, add this: