2013-10-06 8 views
5

मैं अपने प्रकटBroadcastReceiver कई बार

<receiver android:name=".receiver.WifiReceiver" > 
      <intent-filter> 
       <action android:name="android.net.conn.CONNECTIVITY_CHANGE" /> 
      </intent-filter> 
</receiver> 

और निम्नलिखित BroadcastReceiver में निम्नलिखित है कहा जाता है:

public class WifiReceiver extends BroadcastReceiver { 
private static String TAG = makeLogTag(WifiReceiver.class); 

@Override 
public void onReceive(Context context, Intent intent) { 

    ConnectivityManager connectivityManager = (ConnectivityManager) 
      context.getSystemService(Context.CONNECTIVITY_SERVICE); 
    NetworkInfo networkInfo = null; 
    if (connectivityManager != null) { 
     networkInfo = 
       connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI); 

     LOGD(TAG, "connectivity info:" + networkInfo); 
    } 

    if(networkInfo != null && networkInfo.isConnected()) { 
     //TODO: see why this is called multiple times and handle schedule reloading 
     LOGD(TAG, "have Wifi connection and is connected"); 
    }else 
     LOGD(TAG, "don't have Wifi connect or it isn't connected"); 
} 

जब मैं मोबाइल से स्विच रिसीवर Wi-Fi से कई बार बुलाएं (कोई समस्या नहीं है) लेकिन if(networkInfo != null && networkInfo.isConnected()) शाखा सभी 4 गुना

उत्तर

0

सच में मूल्यांकन करती है। वैसे भी इस थोड़ा अलग कोड का प्रयास करें:

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

NetworkInfo activeNetwork = cm.getActiveNetworkInfo(); 
if(activeNetwork != null){ 
    boolean isConnected = activeNetwork.isConnected(); 
    boolean isWiFi = activeNetwork.getType() == ConnectivityManager.TYPE_WIFI; 

    if(isConnected && isWiFi){ 

    } 
} 
4
public class NetworkChangeReceiver extends BroadcastReceiver { 
Context mContext; 

@Override 
public void onReceive(Context context, Intent intent) { 
    mContext = context; 
    String status = NetworkUtil.getConnectivityStatusString(context); 

    Log.e("Receiver ", "" + status); 

    if (status.equals("Not connected to Internet")) { 
     Log.e("Receiver ", "not connction");// your code when internet lost 


    } else { 
     Log.e("Receiver ", "connected to internet");//your code when internet connection come back 
    } 

} 

}

और चेक वाईफ़ाई या इंटरनेट conenctivity के लिए इस कॉल का उपयोग

public class NetworkUtil { 

public static int TYPE_WIFI = 1; 
public static int TYPE_MOBILE = 2; 
public static int TYPE_NOT_CONNECTED = 0; 

public static int getConnectivityStatus(Context context) { 

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

    NetworkInfo activeNetwork = cm.getActiveNetworkInfo(); 

    if (null != activeNetwork) { 

     if (activeNetwork.getType() == ConnectivityManager.TYPE_WIFI) 
      return TYPE_WIFI; 

     if (activeNetwork.getType() == ConnectivityManager.TYPE_MOBILE) 
      return TYPE_MOBILE; 
    } 
    return TYPE_NOT_CONNECTED; 
} 

public static String getConnectivityStatusString(Context context) { 

    int conn = NetworkUtil.getConnectivityStatus(context); 

    String status = null; 
    if (conn == NetworkUtil.TYPE_WIFI) { 
     //status = "Wifi enabled"; 
     status="Internet connection available"; 
    } else if (conn == NetworkUtil.TYPE_MOBILE) { 
     //status = "Mobile data enabled"; 
     status="Internet connection available"; 
    } else if (conn == NetworkUtil.TYPE_NOT_CONNECTED) { 
     status = "Not connected to Internet"; 
    } 
    return status; 
} 

}

खो दिया है और में इस ऐड आपकी androidmanifest फ़ाइल

<receiver 
     android:name=".NetworkChangeReceiver" 
     android:label="NetworkChangeReceiver" > 
     <intent-filter> 
      <action android:name="android.net.conn.CONNECTIVITY_CHANGE" /> 
      <action android:name="android.net.wifi.WIFI_STATE_CHANGED" /> 
     </intent-filter> 
    </receiver> 

प्रकट फ़ाइल में इंटरनेट अनुमति जोड़ें:

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

मैं आशा है कि यह और आप के लिए काम करते

+1

ठीक काम करता है, लेकिन मैं प्रकट फ़ाइल में जोड़ने के लिए भी जरूरत है: '<का उपयोग करता है-अनुमति एंड्रॉयड: नाम = "android.permission.ACCESS_NETWORK_STATE" /> ' – blub

संबंधित मुद्दे