Android 中的互联网连接

发布于 2024-10-03 16:35:40 字数 1862 浏览 3 评论 0原文

我正在研究使用互联网的应用程序。

  • 它包含从 url 进行 XML 解析。 比
  • 解析时显示进度对话框 时间。
  • 解析完成后停止对话框。
  • 在 ListView 中设置 TEXT(Data)。

我的问题是,当设备连接到互联网时,我的应用程序工作正常,但是当设备未连接到互联网时,“进度对话框”会无限期运行。

如果设备未连接到互联网或 wifi,我想停止对话框。如何做到这一点?
有什么想法吗? 抱歉,大家......我改变了主意,我想在单击按钮时检查互联网连接。到目前为止我尝试过的是..

public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.homelayout);
        mclip = (ImageButton) findViewById(R.id.clip);
        mclip.setClickable(true);
        mclip.setFocusable(true);

        mclip.setOnClickListener(new OnClickListener() {

            public void onClick(View view) {
                ConnectivityManager cm = (ConnectivityManager)              getSystemService(Context.CONNECTIVITY_SERVICE);
                NetworkInfo netInfo = cm.getActiveNetworkInfo();
                if (netInfo != null && netInfo.isConnectedOrConnecting()) {
               //if it is connected to internet than start Another Activity.
                startActivity(new Intent(ListViewExample.this, ClipOfDay.class));
                } else if (netInfo == null) {
                    AlertDialog alertDialog = new AlertDialog.Builder(ListViewExample.this).create();
                    alertDialog.setTitle("Connection Problem");
                    alertDialog.setMessage("You are not connected to Internet");
                    alertDialog.setButton("OK", new DialogInterface.OnClickListener() {

                        public void onClick(DialogInterface dialog, int which) {
                            return;
                        }
                    });
                    alertDialog.show();
                }

            }
        });
       }

但这不起作用。如果设备未连接到互联网,我想显示AlertDialog。否则它将启动 Activity。谁能告诉我该怎么做?
谢谢。

I have work on Application That use Internet.

  • It contains XML parsing from url.
    than
  • Showing Progress Dialog While parsing
    time.
  • Stop dialog when parsing is done.
  • set TEXT(Data) in ListView.

my issue is that, When device is connected to internet than my Apps works fine But when Device is not connected to internet "Progress dialog" is running for infinite time.

i want to stop dialog if device is not connected to internet or wifi. how to do this?
Any idea?
Sorry guys.... I have change my mind i want to check internet connection when i click on button. what i have tried so far is..

public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.homelayout);
        mclip = (ImageButton) findViewById(R.id.clip);
        mclip.setClickable(true);
        mclip.setFocusable(true);

        mclip.setOnClickListener(new OnClickListener() {

            public void onClick(View view) {
                ConnectivityManager cm = (ConnectivityManager)              getSystemService(Context.CONNECTIVITY_SERVICE);
                NetworkInfo netInfo = cm.getActiveNetworkInfo();
                if (netInfo != null && netInfo.isConnectedOrConnecting()) {
               //if it is connected to internet than start Another Activity.
                startActivity(new Intent(ListViewExample.this, ClipOfDay.class));
                } else if (netInfo == null) {
                    AlertDialog alertDialog = new AlertDialog.Builder(ListViewExample.this).create();
                    alertDialog.setTitle("Connection Problem");
                    alertDialog.setMessage("You are not connected to Internet");
                    alertDialog.setButton("OK", new DialogInterface.OnClickListener() {

                        public void onClick(DialogInterface dialog, int which) {
                            return;
                        }
                    });
                    alertDialog.show();
                }

            }
        });
       }

But this is not working.if device is not connected to internet than i want to show AlertDialog. otherwise it will start Activity. can Anybody tell me what to do?
Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

聆听风音 2024-10-10 16:35:40

检查网络连接
检查连接的简单代码

ConnectivityManager cMgr = (ConnectivityManager) con.getSystemService(Context.CONNECTIVITY_SERVICE);
            NetworkInfo netInfo = cMgr.getActiveNetworkInfo();
            String status = netInfo.getState().toString();
            if (status.equals("CONNECTED")) {
               //DO you work
            } else {
                Log.e("error", "No connection available ");
            }

Check network connection
A simple code for checking connection

ConnectivityManager cMgr = (ConnectivityManager) con.getSystemService(Context.CONNECTIVITY_SERVICE);
            NetworkInfo netInfo = cMgr.getActiveNetworkInfo();
            String status = netInfo.getState().toString();
            if (status.equals("CONNECTED")) {
               //DO you work
            } else {
                Log.e("error", "No connection available ");
            }
探春 2024-10-10 16:35:40

使用这两种方法来检查连接:

Context.getSystemService(Context.CONNECTIVITY_SERVICE).getNetworkInfo(ConnectivityManager.TYPE_MOBILE)
Context.getSystemService(Context.CONNECTIVITY_SERVICE).getNetworkInfo(ConnectivityManager.TYPE_WIFI)

然后解析返回的结果以了解连接是否可用。
如果它可用,那么只做这些事情并放置对话框。

或者,您也可以弹出该对话框,并在执行任何网络操作之前​​,使用这些方法检查连接情况,如果连接不可用,则隐藏/取消该对话框并显示错误消息。
否则,请采取网络行动。
是的,总是有一个 try-catch 块。

Use these two methods to check connectivity:

Context.getSystemService(Context.CONNECTIVITY_SERVICE).getNetworkInfo(ConnectivityManager.TYPE_MOBILE)
Context.getSystemService(Context.CONNECTIVITY_SERVICE).getNetworkInfo(ConnectivityManager.TYPE_WIFI)

then parse the results returned to know whether the connection is available.
If its available, then only do the things and put the dialog box.

alternatively, you could also pop the dialog box up, and before doing any network action, check for connectivity with these methods, and if connection isn't available, hide/cancel the dialog box and display an error message.
else, go for the network action.
and yes, always have a try-catch block.

电影里的梦 2024-10-10 16:35:40
  • 首先检查互联网或 wifi 是否已连接,然后运行您的代码
  • 使用try、catch,如果由于连接错误而发生异常,请删除catch块中的对话框。

这可能是做到这一点的一种方法..............
希望有帮助......

  • First check if internet or wifi is connected, then run your code
  • Use try, catch, in case some exception occurs because of connection error, remove dialog in catch block.

This can be one way of doing this..............
Hope it helps.......

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