【问题标题】:"Connected" even when wifi is off即使 wifi 关闭也“已连接”
【发布时间】:2018-07-19 00:31:50
【问题描述】:

任务是检查手机是否连接到互联网。我有问题。 即使wifi关闭,它也会显示“已连接”。这是我的课。

public class InterneProvjera {
    Context context;
    @SuppressLint("MissingPermission")
    public InterneProvjera(Context context){
        this.context = context;
    }

    public boolean isNetworkAvailable() {
        ConnectivityManager connectivity = (ConnectivityManager) this.context.getSystemService(Context.CONNECTIVITY_SERVICE);
        if (connectivity != null) {
            NetworkInfo[] info = connectivity.getAllNetworkInfo();
            if (info != null) {
                for (NetworkInfo i: info) {
                    if (i.getState() == NetworkInfo.State.CONNECTED)
                        return true;
                }
            }
        }
        return false;
    }
}

这里是主要活动:

InterneProvjera interneProvjera = new InterneProvjera(this);
        String tKonekcija = (interneProvjera.isNetworkAvailable()) ?  "Connected" : "No connection";
        txtIspis.setText(tKonekcija);

对不起,如果它的琐碎问题是 android 编程中的新问题。 ps:有没有Connection监听器以及如何查看网络信号强度(3G、4G、wifi)?

【问题讨论】:

    标签: java android connection android-connectivitymanager


    【解决方案1】:

    您应该使用BroadcastReceiver 使用ConnectivityManager 检查网络状态

    以下是检查您的活动是否已连接网络的代码。如果已连接,它将在Toast 中显示您的网络名称:

    ConnectivityStatusReceiver.java

    public class ConnectivityStatusReceiver extends BroadcastReceiver {
    
      @Override
      public void onReceive(Context context, Intent intent) {
    
        final ConnectivityManager connMgr = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    
        NetworkInfo activeNetworkInfo = connMgr.getActiveNetworkInfo();
    
        if (activeNetworkInfo != null) {
          Toast.makeText(context, activeNetworkInfo.getTypeName() + " connected", Toast.LENGTH_SHORT).show();
        } else {
          Toast.makeText(context, "No Internet or Network connection available", Toast.LENGTH_LONG).show();
        }
      }
    
    }
    

    MainActivity.java

    public class MainActivity extends AppCompatActivity {
      ConnectivityStatusReceiver connectivityStatusReceiver;
    
      @Override
      protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    
        connectivityStatusReceiver = new ConnectivityStatusReceiver();
      }
    
      @Override
      protected void onResume() {
        super.onResume();
        IntentFilter intentFilter = new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION);
        registerReceiver(connectivityStatusReceiver, intentFilter);
      }
    
      @Override
      protected void onDestroy() {
        super.onDestroy();
        if (connectivityStatusReceiver != null) {
          // unregister receiver
          unregisterReceiver(connectivityStatusReceiver);
        }
      }
    }
    

    【讨论】:

    • @JKLHSDFJKL 我已经添加了完整的代码。希望对您有所帮助。
    猜你喜欢
    • 2020-09-16
    • 2012-09-29
    • 1970-01-01
    • 1970-01-01
    • 2021-07-21
    • 2011-10-11
    • 1970-01-01
    • 1970-01-01
    • 2011-02-16
    相关资源
    最近更新 更多