android: displaying progress dialog when waiting for connection
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);
开发者_JAVA技巧 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
}
}
You should use Progress dialog. Progress dialog should be used in the Profile activity. You can use the following code:
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
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:
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();
Also, I'd recommend using a ProgressDialog to show progress (which you can dismiss, once the code in the thread is done). Tutorial: http://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 a tutorial for that: http://www.tutorialforandroid.com/2009/01/using-handler-in-android.html
Example: In your Profile activity class, add this:
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;
}
}
}
精彩评论