I have around seven activities in my android project and on all seven activities, i want to monitor network disconnection.I have created a networkreceiver
class which receive notification when wifi is enabled or disabled.
public class NetworkReceiver extends BroadcastReceiver{
@Override
public void onReceive(Context context, Intent arg1) {
ConnectivityManager conMan = (ConnectivityManager) context.
getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo netInfo = conMan.getActiveNetworkInfo();
if (netInfo != null && netInfo.getType() == ConnectivityManager.TYPE_WIFI)
Log.d("WifiReceiver", "Have Wifi Connection");
else
Log.d("WifiReceiver", "Don't have Wifi Connection");
}
}
and in manifest
<receiver android:name=".NetworkReceiver" >
<intent-filter>
<action android:name="android.net.wifi.STATE_CHANGE" />
<action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
</intent-filter>
</receiver>
My question is , Since i created it as a seperate class, i am not getting how to get notify on every activity that wifi is not enable.or do i have to create a network receiver as inner class in every activity.
Well I think that the answer with the handlers is a bit smelly - the biggest smell being that you have to keep the instance of the broadcast receiver around - a manifest receiver at that instantiated by request. Instead drop the registration in the manifest and register unregister the receiver onSart/Stop of your activity - no need to make the receiver an inner class for that. Unregistering onStop may not work on Froyo but hey - we're on KitKat
This way is better IMO as you haver the receiver in the activity it should signal and not mixing activity and receiver logic in the receiver class. Plus if you manage to hold a reference to the receiver let me know how - I am curious