1

我有一个大问题:我想控制是否激活了3GWiFi 。这是我的代码:

//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();}

它在 oncreate 中。当我打开活动时,我会强制关闭。原因是这样的:

引起:java.lang.NullPointerException

我不明白问题出在哪里:(

4

4 回答 4

2

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

于 2012-06-02T14:25:24.197 回答
2

您可以检查设备是否有互联网连接:

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;
  }
于 2012-06-02T14:27:50.200 回答
2

ConnectivityManager 的 getActiveNetworkInfo() 方法返回一个 NetworkInfo 实例,该实例表示它可以找到的第一个连接的网络接口,如果接口已连接,则返回 null 如果没有。检查此方法是否返回 null 应该足以判断 Internet 连接是否可用。

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

您还需要:

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

在你的 android 清单中。

于 2012-06-02T14:33:29.603 回答
1
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();
}
于 2012-06-02T14:36:15.310 回答