Matt
Matt

Reputation: 13

is there a broadcast if network provider changes in Android?

Is that possible that my android app receive a broadcast while my network provider changed? thanks in advance.

Upvotes: 1

Views: 496

Answers (1)

Balaji.K
Balaji.K

Reputation: 4829

Yes.It is posible to find this.

see the code

public class NetworkStateReceiver extends BroadcastReceiver {
public static final String TAG = "NetworkReceiver";
@Override
public void onReceive(Context context, Intent intent) {
     boolean isNetworkDown = intent.getBooleanExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, false);  // 2
    if (isNetworkDown) {
        Log.d(TAG, "onReceive: NOT connected, stopping UpdaterService");
    } 
    else 
    {
        Log.d(TAG, "onReceive: connected, starting UpdaterService");
    }
}

In the Manifes file add this code

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

Upvotes: 1

Related Questions