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

发布于 2024-11-17 08:30:38 字数 1589 浏览 3 评论 0原文

我试图在启动新活动时添加一个进度对话框,该活动必须等待来自互联网的响应。目前,等待时屏幕会变黑。有谁知道它需要放置在哪里才能工作吗?

这个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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

素衣风尘叹 2024-11-24 08:30:38

您应该使用进度对话框。进度对话框应在配置文件活动中使用。
您可以使用以下代码:

    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

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
假情假意假温柔 2024-11-24 08:30:38

在 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 来显示进度(一旦线程中的代码完成,您可以将其关闭)。教程: 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 活动类中,添加以下内容:

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;
        }
    }
}

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: 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:

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;
        }
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文