简体   繁体   English

android:等待连接时显示进度对话框

[英]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: 这是在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;
}

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. 在UI线程(调用“onCreate”的线程)中进行网络调用是个坏主意。 It will stall the refresh of the UI till the network operation is completed. 它将停止UI的刷新,直到网络操作完成。 Instead, spawn a new thread in onCreate like so: 相反,在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();

Also, I'd recommend using a ProgressDialog to show progress (which you can dismiss, once the code in the thread is done). 另外,我建议使用ProgressDialog来显示进度(一旦线程中的代码完成,你就可以解除它)。 Tutorial: http://developer.android.com/guide/topics/ui/dialogs.html 教程: 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. 注意 :您无法从新线程中关闭对话框,因此您必须使用Handler将消息从线程发布到UI线程。 Here a tutorial for that: http://www.tutorialforandroid.com/2009/01/using-handler-in-android.html 这是一个教程: 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;
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM