我正在尝试在启动新活动时添加进度对话框,该活动必须等待来自互联网的响应。目前屏幕在等待时变黑。有没有人知道它需要在哪里工作?
这个进程对话:
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;
}
这是个人资料活动:
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
}
}
答案 0 :(得分:11)
您应该使用“进度”对话框。 “配置文件”活动中应使用“进度”对话框。 您可以使用以下代码:
final ProgressDialog dialog = ProgressDialog.show(MyProfileActivity.this, "","Loading..Wait.." , true);
dialog.show();
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
public void run() {
//your code here
dialog.dismiss();
}
}, 3000); // 3000 milliseconds
答案 1 :(得分:2)
在UI线程(调用“onCreate”的线程)中进行网络调用是个坏主意。它将停止UI的刷新,直到网络操作完成。相反,在onCreate中生成一个新线程,如下所示:
Thread networkThread = new Thread() {
public void run() {
String xml = XMLfunctions.getXMLFromBarId(id); // makes httpPost call
Document doc = XMLfunctions.XMLfromString(xml);
NodeList nodes = doc.getElementsByTagName("result");
Element e = (Element)nodes.item(0);
....
}
}
networkThread.start();
另外,我建议使用ProgressDialog来显示进度(一旦完成线程中的代码,就可以解除进度)。教程:http://developer.android.com/guide/topics/ui/dialogs.html
注意:您无法从新线程中删除该对话框,因此您必须使用Handler将消息从线程发布到UI线程。这是一个教程:http://www.tutorialforandroid.com/2009/01/using-handler-in-android.html
实施例: 在您的个人资料活动类中,添加以下内容:
class ProfileActivity extends Activity {
class ProfileHandler extends Handler {
private ProfileActivity parent;
public ProfileHandler(ProfileActivity parent) {
this.parent = parent;
}
public void handleMessage(Message msg) {
parent.handleMessage(msg);
}
}
private ProfileHandler handler;
public void onCreate(Bundle savedInstanceState) {
handler = new ProfileHandler(this);
Thread networkThread = new Thread() {
public void run() {
String xml = XMLfunctions.getXMLFromBarId(id); // makes httpPost call
Document doc = XMLfunctions.XMLfromString(xml);
NodeList nodes = doc.getElementsByTagName("result");
Element e = (Element)nodes.item(0);
....
ProfileActivity.this.handler.sendEmptyMessage(0);
}
}
networkThread.start();
}
public void handleMessage(msg) {
switch(msg.what) {
case 0:
// Update UI here
break;
}
}
}