Broadcastreceiver When Wifi or 3G Network State Changed

BroadcastReceiver when wifi or 3g network state changed

You need

<intent-filter>
<action android:name="android.net.wifi.WIFI_STATE_CHANGED"/>
<action android:name="android.net.wifi.STATE_CHANGE"/>
</intent-filter>

In your receiver tag.

Or if you want more control over it, before registering BroadcastReceiver set these up:

final IntentFilter filters = new IntentFilter();
filters.addAction("android.net.wifi.WIFI_STATE_CHANGED");
filters.addAction("android.net.wifi.STATE_CHANGE");
super.registerReceiver(yourReceiver, filters);

WIFI_STATE_CHANGED

Broadcast <intent-action> indicating that Wi-Fi has been enabled, disabled, enabling, disabling, or unknown. One extra provides this state as an int. Another extra provides the previous state, if available.

STATE_CHANGE

Broadcast <intent-action> indicating that the state of Wi-Fi connectivity has changed. One extra provides the new state in the form of a NetworkInfo object. If the new state is CONNECTED, additional extras may provide the BSSID and WifiInfo of the access point. as a String

Also, you'll need to specify the right permissions inside manifest tag:

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

To check connectivity, you can use ConnectivityManager as it tells you what type of connection is available.

ConnectivityManager conMngr = (ConnectivityManager)this.getSystemService(this.CONNECTIVITY_SERVICE);
android.net.NetworkInfo wifi = conMngr.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
android.net.NetworkInfo mobile = conMngr.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);

Broadcast receiver is not calling on internet connectivity check

Apps targeting Android 7.0 (API level 24) and higher must register the
following broadcasts with

registerReceiver(BroadcastReceiver,IntentFilter)

Declaring a receiver in the manifest does not work.

CONNECTIVITY_ACTION

Beginning with Android 8.0 (API level 26), the
system imposes additional restrictions on manifest-declared receivers.
If your app targets API level 26 or higher, you cannot use the
manifest to declare a receiver for most implicit broadcasts
(broadcasts that do not target your app specifically). You can still
use a context-registered reciever when the user is actively using your
app.

directly from official doc.

you need to register for CONNECTIVITY_CHANGE action at runtime from activity.
using registerReceiver.

IntentFilter filter = new IntentFilter();
filter.addAction("android.net.conn.CONNECTIVITY_CHANGE");
registerReceiver(new NetworkStateChangeReceiver(), filter);

And don't forget to unregister.

Android network state change detection takes time

I found the solution.

Instead of extending BroadcastReceiver class and creating NetworkStateChangeReceiver, i created a broadcastreceiver on my activity and registered it there. Now it works and onReceive() method is triggered immediately.

Android BroadcastReceiver for internet connection called twice

Check this link

For Android 7 and above you need to register your receiver in your activity file not in the manifest file.

So in your activity's onCreate() add the following lines:

    myConnectivityReceiver = new ConnectivityReceiver();
IntentFilter filter = new IntentFilter();
filter.addAction(getResources().getString(R.string.action_connectivity_change));
registerReceiver(myConnectivityReceiver,filter);

and in onDestroy()

  @Override
protected void onDestroy() {
unregisterReceiver(myConnectivityReceiver);
super.onDestroy();
}

define the intent action in strings.xml

<string name="action_connectivity_change">android.net.conn.CONNECTIVITY_CHANGE</string>

Broadcast receiver for checking internet connection in android app

Answer to your first question: Your broadcast receiver is being called two times because

You have added two <intent-filter>

  1. Change in network connection :

    <action android:name="android.net.conn.CONNECTIVITY_CHANGE" />

  2. Change in WiFi state:

    <action android:name="android.net.wifi.WIFI_STATE_CHANGED" />

Just use one:

<action android:name="android.net.conn.CONNECTIVITY_CHANGE" />.

It will respond to only one action instead of two. See here for more information.

Answer to your second question (you want receiver to call only one time if internet connection available):

Your code is perfect; you notify only when internet is available.

UPDATE

You can use this method to check your connectivity if you want just to check whether mobile is connected with the internet or not.

public boolean isOnline(Context context) {

ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo netInfo = cm.getActiveNetworkInfo();
//should check null because in airplane mode it will be null
return (netInfo != null && netInfo.isConnected());
}


Related Topics



Leave a reply



Submit