如何在启动时检查Android应用程序中的Internet状态 [英] How to check internet status in android application at start up time

查看:86
本文介绍了如何在启动时检查Android应用程序中的Internet状态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨朋友们,



我开发了一个带有web-view控件的android应用程序。当我打开应用程序然后它将加载我的Android应用程序中的一些Web应用程序。如果我的手机没有任何互联网连接,那么它会显示一条消息,如网页不可用......有些东西。

Hi friends,

I developed a android application with web-view control. When i open the application then it will load the some web-application with in my android application. If my mobile don't have any internet connection then it will display a message like "Web page not available ...... some thing some thing".


但我的要求是当我打开应用程序时,它应检查互联网连接状态。如果连接可用,则允许应用程序执行所有操作。如果连接不可用,那么它应该显示没有互联网连接的toast消息,当我在toast消息中点击OK然后我应该关闭应用程序。所以请为我提供一个符合我要求的代码



先谢谢啦。

But my requirement is when i open the application, it should check the internet connection status. If the connection is available then it allow the application to perform all operations. If the connection is not available then it should display the toast message with "no internet connection" and when i click on OK in toast message then i should close the application. So please provide me a code for my requirement

Thanks in advance friends.

推荐答案

这很简单。请参阅:

It's pretty simple. See :
public boolean isConnectingToInternet(){
        ConnectivityManager connectivity = (ConnectivityManager) _context.getSystemService(Context.CONNECTIVITY_SERVICE);
          if (connectivity != null)
          {
              NetworkInfo[] info = connectivity.getAllNetworkInfo();
              if (info != null)
                  for (int i = 0; i < info.length; i++)
                      if (info[i].getState() == NetworkInfo.State.CONNECTED)
                      {
                          return true;
                      }

          }
          return false;
    }





在您的启动器活动上调用此方法应用程序,在 setContentView()之后。

如果它返回 true 那么你呢去!



src: androidhive [ ^ ]



-KR



Call this method on the launcher activity of your application, right after setContentView().
If it is returning true then there you go !

src : androidhive[^]

-KR


检查出来:安卓检测互联网连接状态 [ ^ ]


这篇关于如何在启动时检查Android应用程序中的Internet状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆