OnReceive () android.net.ConnectivityManager.CONNECTIVITY_ACTION
In my application, I need to check for changes to the internet connection. So we register the receiver "android.net.ConnectivityManager.CONNECTIVITY_ACTION" and in onReceive () handle the connection change as shown below.
BroadcastReceiver receiver = new BroadcastReceiver()
{
@Override
public void onReceive(Context context, Intent intent)
{
if (action.equals(android.net.ConnectivityManager.CONNECTIVITY_ACTION)) {
// handle netwrk changes code
}
}
};
The above code works fine and it handles changes in internet connection. But the problem is when I go to another activity and return to that activity, the onReceive () method will be executed because it will receive the broadcast for " android.net.ConnectivityManager.CONNECTIVITY_ACTION " which I don't want that. Does anyone know why this is happening?
When I return to this operation, I do not want to receive the broadcast for "android.net.ConnectivityManager.CONNECTIVITY_ACTION"
.
source to share
try creating your broadcast receiver in a separate BroadcastReceiver class if you are not calling your activity.
public class Netreciever extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
if (action.equals(android.net.ConnectivityManager.CONNECTIVITY_ACTION)) {
// handle netwrk changes code
}
}
manifest.xml
<receiver
android:name=".Netreciever"
android:enabled="true" >
<intent-filter>
<action android:name="android.net.wifi.WIFI_STATE_CHANGED" />
<action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
<action android:name="android.net.conn.DATA_ACTIVITY_CHANGE" />
</intent-filter>
</receiver>
source to share