-1

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.

Mr_and_Mrs_D
  • 32,208
  • 39
  • 178
  • 361
Waqar Ahmed
  • 5,005
  • 2
  • 23
  • 45

4 Answers4

1

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

Mr_and_Mrs_D
  • 32,208
  • 39
  • 178
  • 361
  • but how to stop task when i get notifide that internet is not available. – Waqar Ahmed Feb 27 '14 at 11:16
  • @wqrahd: stop _which_ task ? – Mr_and_Mrs_D Feb 27 '14 at 11:36
  • let say i am downlaoding the file and in between network disconnect.then how to stop or pause the downloading task and when network again connect resume or start again the task that was stopped or paused. – Waqar Ahmed Feb 27 '14 at 11:37
  • @wqrahd: The receiver may call a static method of the class - but static means trouble. The receiver may be an inner class after all and have access to the Activity's methods/fields - a pain if you have to repeat for all 7. Easiest: The receiver changes an internal preference (call getDefaultSharedPreferences(context) _in_ `onReceive`) and the activities all listen for such a change. You may even have a base activity the implements shared prefs listener and defines common behavior - heck you can have in this base activity the onStart/onStop – Mr_and_Mrs_D Feb 27 '14 at 11:49
  • let say i have sharedpreference variable. and iam making a http request. so do i have to make this request in while loop and check like that "while variable true make http call"? – Waqar Ahmed Feb 27 '14 at 11:59
  • request may be a web page request. Since this request is only one time so how do i get info of network broken in between requesting this page. – Waqar Ahmed Feb 27 '14 at 12:00
  • You don't - set a timeout and catch the timeout exception – Mr_and_Mrs_D Feb 27 '14 at 12:02
  • thanks man you making things clear to me. now one more thing let say i have a time out for 10 sec and at 5th sec network disconnect. do my app say force close or raise any exception or run until for the 10 sec mean upto time out time.? – Waqar Ahmed Feb 27 '14 at 12:04
  • i tried to use time out exception but it say this exception is never thrown from try block. i added parameter to request like HttpConnectionParams.setConnectionTimeout(httpParams, 10); HttpConnectionParams.setSoTimeout(httpParams, 10); – Waqar Ahmed Feb 27 '14 at 12:14
  • @wqrahd: That's another question - and maybe it's not that simple was just an idea of mine. Make sure you solve the notification if network exists or not and edit your question _adding your final solution_ – Mr_and_Mrs_D Feb 27 '14 at 14:00
  • i used a method to check if internet is availavle or not, whenever i am going to make http call. but i think its not correct way to do because while my request is processing , if network goes down then it might get crashed. – Waqar Ahmed Feb 28 '14 at 12:24
  • @wqrahd: you have asked 9 questions and accepted only one answer - this means that your questions are not focused - you are asking a question here you get many answers and not one satisfies you because your question _is too broad_. Understand ? So better ask more carefully – Mr_and_Mrs_D Feb 28 '14 at 12:27
  • oh thanks for clearing me.but i think my question is clear enough. like you understand my question and give advices that helped me understading problem. – Waqar Ahmed Feb 28 '14 at 12:29
  • can please explain me . how to make a perfect , complete http request using android. mean if it crashes in between or time out occur so my app should not get close.it should handle error or exception properly. – Waqar Ahmed Feb 28 '14 at 12:32
  • i am accepting this as answer because you helped me alot uptil now. – Waqar Ahmed Feb 28 '14 at 12:33
  • @wqrahd: Ok - thanks but I do not want you to accept my answer if it did not really answer your question. So `can please explain me . how to make a perfect , complete http request using android. mean if it crashes in between or time out occur so my app should not get close.it should handle error or exception properly` : this is TOO GENERAL for a question. The answer to this question is 25 pages ! You must break it into smaller steps and ask a question for each. That way you will receive answers you can accept and are useful. So write code and post SMALL questions for PARTICULAR problems. – Mr_and_Mrs_D Feb 28 '14 at 12:57
  • then i have to ask a new question...:P....i meant that if there is any kind of link than can explain my answer. thank you...:) – Waqar Ahmed Feb 28 '14 at 12:58
0

A nice solution works as follows. Your broadcast receiver has an array of objects (one for every activity) and when a notification arrives, you loop through the objects to let them know the WiFi state changed.

The details:

First, your broadcast receiver instance should be available to all Activities (through a static variable, singleton or however you want).

Second, each activity has a Handler object to handle events, let's call it mHandler.

Third, the broadcast recevier has an array of Handlers, let's call it 'mHandlerArray'.

In your broadcast receiver class have an array of Handler objects. Each activity subscribes itself by passing a pointer to a mHandler owned by each Activity. Your broadcast receiver could have two methods: one to register and one to unregister.

So, each Activity implements onResume() and onPause()

void onResume()
{
    mBroadcastReceiver.register(mHandler);
}

void onPause()
{
    mBroadcastReceiver.unregister(mHandler);
}

The broadcast receiver does this:

void register(Handler handler)
{
    mHandlerArray.add(handler);
}

void register(Handler handler)
{
    mHandlerArray.remove(handler);
}

Finally, when the WiFi state changes, from your onReceive() you iterate through the mHandlerArray and send each Handler a message. Each Activity processes the message accordingly.

Merlevede
  • 8,140
  • 1
  • 24
  • 39
-1

if you register your receiver in manifest, then only defined class can receive the broadcast info. So, you can define register and unregister option in application class, then Broadcast-receiver method can keep your activities class (which activity class need to know network status)

Ratna Halder
  • 1,954
  • 16
  • 17
-2

Create a separate class Networkinfo and create a methode to check Internet connection.

Where you want to check internet then you call this methode with object of that class. below link will help to detect connectivity.

How do I see if Wi-Fi is connected on Android?

Best of luck.

Community
  • 1
  • 1
Yogendra
  • 4,817
  • 1
  • 28
  • 21