Android: Checking network connectivity return not connected when connected to wifi/3g in Broadcast receiver

前端 未结 3 514
名媛妹妹
名媛妹妹 2020-12-06 19:03

I want to check if device in connected or not in broadcastReceiver. below is my code :

public boolean isOnline(Context context) {
NetworkInfo info = (Network         


        
3条回答
  •  无人及你
    2020-12-06 19:22

        ConnectivityManager cm =
            (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);
    
    NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
    boolean isConnected = activeNetwork != null &&
                          activeNetwork.isConnectedOrConnecting();
    

    I found this on this google tutorial: http://developer.android.com/intl/pt-br/training/monitoring-device-state/connectivity-monitoring.html. check it out.

提交回复
热议问题