3

In my application need to check the internet connection changes. So we register the receiver "android.net.ConnectivityManager.CONNECTIVITY_ACTION" and in the onReceive() handle the connectivity change like as below code.

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 is working fine and it handles the internet connectivity changes. But the problem is when I goes to some other activity and come back to this activity the onReceive() method will executed because receive the broadcast for "android.net.ConnectivityManager.CONNECTIVITY_ACTION" which I don't want it. Can anybody know why this is happening?

When I come back to the activity I don't want to receive the broadcast for "android.net.ConnectivityManager.CONNECTIVITY_ACTION".

1
  • then try to declare broadcast reciever in seprate class in place of your activity class
    – raj
    Nov 7, 2014 at 12:09

1 Answer 1

1

try creating broadcast reciever in seprate BroadcastReceiver class when not get called with 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>
2
  • still "android.net.conn.Connectivity_change is getting called.
    – krish
    Nov 7, 2014 at 12:28
  • Why it's getting called when comes back to the activity
    – krish
    Nov 7, 2014 at 13:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.