Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/197.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
检查android中的Internet连接_Android_Android Alertdialog_Android Logcat_Android Context_Android Internet - Fatal编程技术网

检查android中的Internet连接

检查android中的Internet连接,android,android-alertdialog,android-logcat,android-context,android-internet,Android,Android Alertdialog,Android Logcat,Android Context,Android Internet,我正在开发一个应用程序,检查互联网是否可用 我得到了他的帮助 这是我的连接类: public class ChechConnection { private Context _context; public ChechConnection(Context context){ this._context = context; } public boolean isConnectingToInternet(){ ConnectivityManager connectivit

我正在开发一个应用程序,检查互联网是否可用

我得到了他的帮助

这是我的连接类:

  public class ChechConnection {

private Context _context;

public ChechConnection(Context context){
    this._context = context;
}

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;
}
}
如果有人点击一个按钮,它应该会显示一些东西,如果有互联网连接

 cDetactor=new ChechConnection(getApplicationContext());
 isInternetPresent = cDetactor.isConnectingToInternet();


   btn_recharge.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View v) {
         if (isInternetPresent){
        Toast.makeText(mContext,"Button Pressed",Toast.LENGTH_LONG).show();
        }
        else
            alert.showAlertDialog(mContext,"Check Connection","Check Your Connection Setting",false);
    }
});
这是我自己的对话框管理器:

公共类ALertDialogManager{

public void showAlertDialog(final Context context, String title, String message,
        Boolean status) {
    final Dialog alertDialog = new Dialog(new ContextThemeWrapper(context, android.R.style.Theme_Translucent));
    alertDialog.requestWindowFeature(Window.FEATURE_NO_TITLE); 
    alertDialog.setCancelable(true);
    alertDialog.setContentView(R.layout.dialog);
    alertDialog.setTitle(title);

   Button ok=(Button) alertDialog.findViewById(R.id.btncancel);
   Button cancel=(Button) alertDialog.findViewById(R.id.btnsearch);
   ok.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View v) {

        Activity activity=(Activity) context;
        activity.finish();
    }
});
   cancel.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View v) {
        alertDialog.dismiss();
    }
});

    alertDialog.show();
}
}

但如果有互联网连接,它会给我一个错误。请检查我的logcat值:

04-29 11:26:15.011: E/AndroidRuntime(2177): Process: com.example.lifegoal, PID: 2177
04-29 11:26:15.011: E/AndroidRuntime(2177): java.lang.NullPointerException: Attempt to invoke virtual method 'void com.lifegoal.eshop.helper.ALertDialogManager.showAlertDialog(android.content.Context, java.lang.String, java.lang.String, java.lang.Boolean)' on a null object reference
04-29 11:26:15.011: E/AndroidRuntime(2177):     at com.lifegoal.eshop.Recharge_Activity$1.onClick(Recharge_Activity.java:51)
但如果有一个互联网连接,它会转到其他部分,并给我那个logcat值

谢谢

ConnectivityManager connMgr=(ConnectivityManager)此
ConnectivityManager connMgr = (ConnectivityManager) this
            .getSystemService(Context.CONNECTIVITY_SERVICE);

    final android.net.NetworkInfo wifi = connMgr
            .getNetworkInfo(ConnectivityManager.TYPE_WIFI);

    final android.net.NetworkInfo mobile = connMgr
            .getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
    boolean connection_check = isConnectingToInternet();
    if (connection_check) {
        newuser.setEnabled(false);
        spinner.setVisibility(View.VISIBLE);
}

    else 
        Toast.makeText(this, "No Internet Connection", Toast.LENGTH_SHORT)
                .show();



public boolean isConnectingToInternet() {
    ConnectivityManager connectivity = (ConnectivityManager) 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;
}
.getSystemService(Context.CONNECTIVITY\u服务); 最终android.net.NetworkInfo wifi=connMgr .getNetworkInfo(ConnectionManager.TYPE_WIFI); 最终android.net.NetworkInfo mobile=connMgr .getNetworkInfo(ConnectionManager.TYPE_MOBILE); 布尔连接\u check=isConnectingToInternet(); 如果(连接检查){ newuser.setEnabled(false); spinner.setVisibility(View.VISIBLE); } 其他的 Toast.makeText(此“无互联网连接”,Toast.LENGTH\u SHORT) .show(); 公共布尔值未连接到Internet(){ ConnectivityManager connectivity=(ConnectivityManager)getSystemService(Context.connectivity_服务); if(连接性!=null){ NetworkInfo[]info=connectivity.getAllNetworkInfo(); 如果(信息!=null) 对于(int i=0;i
我觉得这个。可以帮助您使用此方法:

public static boolean isDeviceOnline(Context context) {
        boolean isConnectionAvail = false;
        try {
            ConnectivityManager cm = (ConnectivityManager) context
                    .getSystemService(Context.CONNECTIVITY_SERVICE);
            NetworkInfo netInfo = cm.getActiveNetworkInfo();
            if(netInfo != null)
            return netInfo.isConnected();
            else 
                return isConnectionAvail;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return isConnectionAvail;
    }

创建一个类NetworkInformation.java

import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;

public class NetworkInformation {

     private static NetworkInfo networkInfo;

     public static boolean isConnected(Context context) {

             ConnectivityManager cm = (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);

             try{
                networkInfo = cm.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
            } catch (Exception e) {
                e.printStackTrace();
            }

            // test for connection for WIFI
            if (networkInfo != null
                    && networkInfo.isAvailable()
                    && networkInfo.isConnected()) {
                return true;
            }

            networkInfo = cm.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
            // test for connection for Mobile
            if (networkInfo != null
                    && networkInfo.isAvailable()
                    && networkInfo.isConnected()) {
                return true;
            }

            return false;
      }




}
现在使用该类通过以下代码检查internet是否存在:

 if(NetworkInformation.isConnected(Login.this)) 
                 {
                    //your code
                 }else{
                     Toast.makeText(Login.this,"No network connection",Toast.LENGTH_LONG).show();
                 }
另外,不要忘记在AndroidManifest.xml中定义以下权限:

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


这是一个非常常见的问题,每次都要遵循一行一行的教程。我想您可能忘记了变量cdetator的初始化。并且始终遵循编码标准,这样您就可以消除这样的错误。

这种方法很好:

public boolean checkInternetConnection(){
      ConnectivityManager connec = 
              (ConnectivityManager)context.getSystemService(context.CONNECTIVITY_SERVICE);

  // Check for network connections
   if ( connec.getNetworkInfo(0).getState() == android.net.NetworkInfo.State.CONNECTED ||
        connec.getNetworkInfo(0).getState() == android.net.NetworkInfo.State.CONNECTING ||
        connec.getNetworkInfo(1).getState() == android.net.NetworkInfo.State.CONNECTING ||
        connec.getNetworkInfo(1).getState() == android.net.NetworkInfo.State.CONNECTED ) {

       // if connected with internet


       return true;

   } else if (
     connec.getNetworkInfo(0).getState() == android.net.NetworkInfo.State.DISCONNECTED ||
     connec.getNetworkInfo(1).getState() == android.net.NetworkInfo.State.DISCONNECTED  ) {


       return false;
   }
 return false;

}
不要忘记
AndroidManifest.xml
。您必须定义权限:

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


祝您编码顺利)。

充值活动第51行有什么内容?警报为空,请勾选“警报”object@PareshMayanin再次检查我已编辑是的,现在我已连接互联网,它不应连接到其他部分@Prachii我认为
mContext
为空,检查that@Tufan查看答案
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />