Dynamic Broadcastreceiver To Check Online Connectivity
Solution 1:
Add connectivity action
onlineIntentFilter.addAction(ConnectivityManager.CONNECTIVITY_ACTION)
and don't forget to add the permission
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
The broadcast receiver's onReceive()
will be called on connectivity change which means on both connected and disconnected. so whenever you receive the braodcast you have check for connectivity as below
publicbooleanisNetworkAvailable() {
ConnectivityManagercm= (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfonetworkInfo= cm.getActiveNetworkInfo();
if (networkInfo != null && networkInfo.isConnected()) {
returntrue;
}
returnfalse;
}
Solution 2:
Here is the standard way to do this.
Create a interface which will have two methods onNetworkConnected() and onNetworkDisconnected()
publicinterfaceNetworkConnectivityListener {
publicvoidonNetworkConnected();
publicvoidonNetworkDisconnected();
}
Any class that wants to listen to network changes will implement this interface and override it's two methods.
Create a class which will extend the BroadcastReceiver and this receiver's onReceive() will catch the connectivity changes. In this class create a function to register the listeners
publicclassNetworkBroadcastReceiverextendsBroadcastReceiver {
private NetworkConnectivityListener listener;
publicNetworkBroadcastReceiver(NetworkConnectivityListener listener) {
this.listener = listener;
}
@OverridepublicvoidonReceive(Context context, Intent intent) {
Stringaction= intent.getAction();
if (listener != null) {
if (isConnectedToInternet()) {
listener.onNetworkConnected();
} else {
listener.onNetworkDisconnected();
}
}
}
PS you can easily check if your device is connected to internet or not. Just google.
Now let's say you want your MainActivity to listen to netwrork changes, all you have to do is implement the NetworkConnectivityListener in your main activity call and create an instance of the NetworkBroadcastReceiver passing the context of MainActivity and it will start to get the network updates.
Solution 3:
add intentfilter CONNECTIVITY_ACTION
Solution 4:
You will need to create a service as you are looking to perform a long-running task in the background.
A service will need to be registered in the manifest, within the <application>
tags.
<serviceandroid:name=".WifiService"android:stopWithTask="true"android:enabled="true"/>
stopWithTask
causes the service to be destroyed when all of your activities are destroyed (the default value is true).
You will need to register and unregister a BroadcastReceiver to the CONNECTIVITY_CHANGE action in the service class.
publicclassWifiServiceextendsService {
private BroadcastReceiver mReceiver;
@Nullable@Overridepublic IBinder onBind(Intent intent) {
IntentFilterfilter=newIntentFilter();
filter.addAction("android.net.conn.CONNECTIVITY_CHANGE");
// Your BroadcastReceiver class
mReceiver = newWifiReceiver();
registerReceiver(mReceiver, filter);
returnnull;
}
@OverridepublicbooleanonUnbind(Intent intent) {
unregisterReceiver(mReceiver);
returnsuper.onUnbind(intent);
}
}
And then in the BroadcastReceiver you must listen for CONNECTIVITY_CHANGE.
publicclassWifiReceiverextendsBroadcastReceiver {
@OverridepublicvoidonReceive(Context context, Intent intent) {
ConnectivityManagermanager= (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfonetworkInfo= manager.getActiveNetworkInfo();
booleanisConnected= networkInfo != null &&
networkInfo.isConnectedOrConnecting();
if (isConnected) {
switch (networkInfo.getType()) {
case ConnectivityManager.TYPE_WIFI:
// Connected via wifibreak;
case ConnectivityManager.TYPE_ETHERNET:
// Connected via ethernetbreak;
case ConnectivityManager.TYPE_MOBILE:
// Connected via mobile databreak;
}
}
}
}
With this setup you will be able to notify components of your application on connectivity change.
Post a Comment for "Dynamic Broadcastreceiver To Check Online Connectivity"