简体   繁体   English

检测android是否已连接到互联网

[英]Detecting if android is connected to internet

I have a big problem: I want control if 3G or WiFi are activated. 我有一个大问题:我想控制3GWiFi是否被激活。 This is my code: 这是我的代码:

//controllo se è accesa la connessione 
        ConnectivityManager cm =
                (ConnectivityManager)this.getSystemService(Context.CONNECTIVITY_SERVICE);

        NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
        boolean isConnected = activeNetwork.isConnectedOrConnecting();
        //controllo se sono connesso
        if(isConnected==false){
            final AlertDialog.Builder builder=new AlertDialog.Builder(this);
        builder.setTitle("Attenzione!");
        builder.setMessage("L'applicazione senza la connessione ad internet non può funzionare. La preghiamo di attivarla.");
        builder.setIcon(android.R.drawable.ic_dialog_alert);
        builder.setPositiveButton("OK", new OnClickListener() {

            public void onClick(DialogInterface dialog, int which) {

            }
        });
        builder.show();}

It's into the oncreate. 它进入了oncreate。 When I open the activity I have a force close. 当我打开活动时,我会强制关闭。 The cause is this: 原因是:

Caused by: java.lang.NullPointerException 造成原因:java.lang.NullPointerException

I don't understand where is the problem :( 我不明白问题出在哪里:(

如果没有活动的连接,则getActiveNetworkInfo()返回null

You can Check if the device has Internet connection or not as: 您可以通过以下方式检查设备是否具有Internet连接:

public  boolean CheckConnection() {
    ConnectivityManager cm = (ConnectivityManager) MbridgeApp.getContext().getSystemService(
        Context.CONNECTIVITY_SERVICE);

    NetworkInfo wifiNetwork = cm.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
    if (wifiNetwork != null && wifiNetwork.isConnected()) {
      return true;
    }

    NetworkInfo mobileNetwork = cm.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
    if (mobileNetwork != null && mobileNetwork.isConnected()) {
      return true;
    }

    NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
    if (activeNetwork != null && activeNetwork.isConnected()) {
      return true;
    }
    return false;
  }

The getActiveNetworkInfo() method of ConnectivityManager returns a NetworkInfo instance representing the first connected network interface it can find or null if none if the interfaces are connected. ConnectivityManager的getActiveNetworkInfo()方法返回一个NetworkInfo实例,该实例表示它可以找到的第一个连接的网络接口;如果连接的接口没有,则返回null。 Checking if this method returns null should be enough to tell if an internet connection is available. 检查此方法是否返回null应该足以判断是否有Internet连接。

private boolean isNetworkAvailable() {
    ConnectivityManager connectivityManager 
          = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
    return activeNetworkInfo != null;
}

You will also need: 您还需要:

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

in your android manifest. 在您的Android清单中。

ConnectivityManager cm = (ConnectivityManager)getApplicationContext().getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
boolean isConnected = false;
if(activeNetwork!=null)
    isConnected = true;
activeNetwork.isConnectedOrConnecting();
if(!isConnected){
   final AlertDialog.Builder builder=new AlertDialog.Builder(YourActivity.this);
   builder.setTitle("Attenzione!");
   builder.setMessage("L'applicazione senza la connessione ad internet non può funzionare. La preghiamo di attivarla.");
   builder.setIcon(android.R.drawable.ic_dialog_alert);
   builder.setPositiveButton("OK", new OnClickListener() {

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM