Note that there are some explanatory texts on larger screens.

plurals
  1. POAndroid Monitor Connectivity: I receive it twice
    primarykey
    data
    text
    <p>Some days ago I found a code in a Stackoverflow post to know when the connectivity of my device changes using a Broadcast Receiver. I adapted it to my application and it works, but everytime I turn on/off my internet connection, the onReceive method on my receiver is called twice. Why is that? My application works fine anyway, but I do some actions twice when once is enough.</p> <p>This is my receiver:</p> <pre><code>public class ConnectivityReceiver extends BroadcastReceiver { private Context mContext; Thread network_check = new Thread(new Runnable(){ public void run() { if (NetworkHelper.isOnline(mContext)) { // Do some stuff } } }); @Override public void onReceive(Context context, Intent intent) { Log.d("MonitorInternetConnectivity", "onReceive"); mContext = context; network_check.start(); } } </code></pre> <p>Network Helper class:</p> <pre><code>public class NetworkHelper { public static boolean isOnline(Context context) { ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo netInfo = cm.getActiveNetworkInfo(); if (netInfo != null &amp;&amp; netInfo.isConnectedOrConnecting() &amp;&amp; canHit()) { return true; } return false; } public static boolean canHit() { try { URL url = new URL("http://www.google.com/"); HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection(); urlConnection.setConnectTimeout(3000); urlConnection.connect(); urlConnection.disconnect(); return true; } catch (Exception e) { e.printStackTrace(); return false; } } } </code></pre> <p>And I added this to my manifest (and the needed permissions):</p> <pre><code>&lt;receiver android:name=".receivers.ConnectivityReceiver" android:exported="false"&gt; &lt;intent-filter&gt; &lt;action android:name="android.net.conn.CONNECTIVITY_CHANGE" /&gt; &lt;/intent-filter&gt; &lt;/receiver&gt; </code></pre> <p>Everytime my connection changes the log shows "onReceive" twice. Why?</p> <p>Thanks!</p> <p>--------------- EDIT ---------------------</p> <p>I add info about the intents received. Everytime the connectivity changes the two intents received are exactly the same.</p> <p>When the device goes offline:</p> <pre><code>Intent { act=android.net.conn.CONNECTIVITY_CHANGE flg=0x10000010 cmp=com.pfc.app/.receivers.ConnectivityReceiver (has extras) } Extras: NetworkInfo: type: WIFI[], state: DISCONNECTED/DISCONNECTED, reason: (unspecified), extra: (none), roaming: false, failover: false, isAvailable: false noConnectivity: true inetCondition: 0 </code></pre> <p>When the device goes online:</p> <pre><code>Intent { act=android.net.conn.CONNECTIVITY_CHANGE flg=0x10000010 cmp=pfc.dani.palou/.receivers.ConnectivityReceiver (has extras) } Extras: NetworkInfo: type: WIFI[], state: CONNECTED/CONNECTED, reason: (unspecified), extra: (none), roaming: false, failover: false, isAvailable: true inetCondition: 0 </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload