【问题标题】:Checking for data connection [duplicate]检查数据连接[重复]
【发布时间】:2013-08-07 14:14:00
【问题描述】:

我有一个带有 onclick 事件的按钮,可以打开另一个类。另一个类正在进行网络调用以检索数据,因此如果没有连接,则应用程序强制关闭。

我将如何实现可以检查数据连接的 try catch 或 if 语句,如果没有,则显示一个 toast 说明。如果有则继续上二等。

【问题讨论】:

    标签: android if-statement try-catch


    【解决方案1】:

    我用:

    public boolean isOnline() {
        ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo netInfo = cm.getActiveNetworkInfo();
        if (netInfo != null && netInfo.isConnectedOrConnecting()) {
            return true;
        }
        return false;
    }
    

    【讨论】:

      【解决方案2】:

      做这样的事情来检查互联网连接:

      static public boolean isInternetActive() 
      {
          ConnectivityManager connectivity = (ConnectivityManager) getApplicationContext().getSystemService(Context.CONNECTIVITY_SERVICE);
      
          NetworkInfo info = (NetworkInfo) connectivity.getActiveNetworkInfo();
      
          if (info == null || !info.isConnected()) 
          {
              return false;
          }
          if (info.isRoaming()) 
          {
              return false;
          }
          return true;
      }
      

      【讨论】:

        【解决方案3】:
        public boolean isOnline() {
        ConnectivityManager cm =
            (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
        
        return cm.getActiveNetworkInfo() != null && 
           cm.getActiveNetworkInfo().isConnectedOrConnecting();
        }
        

        您还需要:

         <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
        

        在您的 android 清单中。

        【讨论】:

          【解决方案4】:

          此代码检查互联网连接是否打开或关闭代码检查 wifi 和电话网络。

          public boolean CheckInternet() 
          {
              ConnectivityManager connec = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
           android.net.NetworkInfo wifi = connec.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
          
              android.net.NetworkInfo mobile = connec.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
          
              // Here if condition check for wifi and mobile network is available or not.
              // If anyone of them is available or connected then it will return true, otherwise false;
          
              if (wifi.isConnected()) {
                  return true;
              } else if (mobile.isConnected()) {
                  return true;
              }
              return false;
          }
          

          清单文件::

          <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
          

          【讨论】:

            【解决方案5】:

            试试这个,这对你有帮助..

            public class CheckNetworkInfo {
            
                public static boolean haveNetworkConnection(Context context) {
            
                     ConnectivityManager cm = (ConnectivityManager) context
                                .getSystemService(Context.CONNECTIVITY_SERVICE);
                        // test for connection
                        if (cm.getActiveNetworkInfo() != null
                                && cm.getActiveNetworkInfo().isAvailable()
                                && cm.getActiveNetworkInfo().isConnected()) {
                            Log.v("Yeah", "Internet is working");
                            // txt_status.setText("Internet is working");
                            return true;
                        } else {
                            // txt_status.setText("Internet Connection Not Present");
                            Log.v("Sorry", "Internet Connection Not Present");
                            return false;
                        }
            }
            

            // 在你想检查网络可用性的地方调用上面的类静态方法

            class Myactivity extends Activity{
            
            public void onCreate(Bundle savedInstanceState) {
            
                if(CheckNetworkInfo.haveNetworkConnection(Myactivity.this)){
                        startActivity(new Intent(HomePage.this,SearchTerm.class));
                    }
                    else{
                        AlertDialog.Builder a=new AlertDialog.Builder(Myactivity.this);
                        a.setTitle("Check Network Connection");
                        a.setPositiveButton("OK",new android.content.DialogInterface.OnClickListener() {
            
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                // TODO Auto-generated method stub
                            dialog.dismiss();   
                            }
                        });
                        a.show();
                    }
            }
            }
            

            【讨论】:

              【解决方案6】:

              只需使用它。

              从任何地方调用此代码,它将返回网络的当前状态。

              public static boolean isInternetAvailable(Context c)
                  {
                       ConnectivityManager connectivityManager 
                       = (ConnectivityManager) c.getSystemService(Context.CONNECTIVITY_SERVICE);
                       NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
                       return activeNetworkInfo != null && activeNetworkInfo.getState() == NetworkInfo.State.CONNECTED;
                  }
              

              如果已连接则返回 true,否则返回 false。对其输出采取任何所需的操作。

              注意:它是一个静态方法。因此,如果您只想从 Android Activity 类中调用它,请删除 static 关键字。

              【讨论】:

                猜你喜欢
                • 2021-08-17
                • 2017-11-30
                • 2012-03-23
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 2013-04-25
                • 2012-09-06
                • 1970-01-01
                相关资源
                最近更新 更多