1

I've been looking into this for a while and have been unable to find a concrete solution.

I'm trying to detect when a user has started using their internet (3G, Wifi). (Or is about to start would be even better).

Does anyone know if there is an Intent that could detect a person is starting to use the network? What I'm trying to do is run some code once the user starts using their internet regardless of whether or not it is 3G or Wifi.

I've been looking into ConnectivityManager, and Trafficstats() but have so far been unable to come up with a solution. I am not simply looking to see if a connection is available.

One solution that I've been thinking would be to create my own intent but I've been unable to find any good documentation on how to create your own intent filter. (most tutorials I've seen say to use/specify your own Intent, but neglect to say how to create the intent) Because of this, I'm assuming it's something simple that people don't feel is worth mentioning.

The idea behind using an intent was so that I could trigger code to execute on the event that the user is using the internet, not just connected to it. This would be preferable to having a continually executing loop that looks for rx/tx bytes sent out.

If anyone has any ideas and sample code that would be really appreciated.

Cheers

Dave
  • 3,178
  • 5
  • 28
  • 44
  • see if [this](http://stackoverflow.com/questions/4238921/android-detect-whether-there-is-an-internet-connection-available) questions helps. – Kevin Qiu Jul 15 '11 at 21:53

2 Answers2

1

I do not know if this helps, but with this code I am trying to find when device goes into no network. I am sure you can modify this to detect if its 3G or WiFi.

no network

Community
  • 1
  • 1
anargund
  • 3,249
  • 2
  • 21
  • 25
  • Thanks bandaa25, I can assume for mine that the user is always connected to either 3G or Wifi I need to be able to detect when a user starts using a large amount of bandwidth over either 3G or Wifi. – Dave Jul 15 '11 at 23:14
  • Ya, I got your problem, what I wanted to tell you is, there is a way to detect this inside onDataConnectionStateChanged method. If you just add this code and put logs, you would notice that there are some different parameters you get. – anargund Jul 15 '11 at 23:16
0

Copied from one of my projects http://code.google.com/p/android-menu-navigator/

You can combine it with the connection state changed mentioned by bandaa25 and I think you are done.

  public boolean isOnWifi() {
        Log.d(TAG, "Checking if we are on wifi");
        final ConnectivityManager mgrConn = (ConnectivityManager) ctx.getSystemService(Context.CONNECTIVITY_SERVICE);
        Log.d(TAG, "Retrieved connectivity manager");
        final NetworkInfo network = mgrConn.getActiveNetworkInfo();
        Log.d(TAG, "Retrieved network info: " + network);
        final boolean result = network != null && network.getType() == ConnectivityManager.TYPE_WIFI;
        Log.d(TAG, "Result : " + result);
        return result;
    }
Jarek Potiuk
  • 19,317
  • 2
  • 60
  • 61
  • Hey Jarek, Thanks for your quick response. What I'm trying to do is run some code once the user starts using their internet regardless of whether or not it is 3G or Wifi. The idea behind using an intent was so that I could trigger code to execute on an event rather than having a continually executing loop that looks for rx/tx bytes sent out. Unless I am mistaken, I believe that your code, with the no network piece, will simply tell me if I'm on wifi, or if there is no network connection. I can assume that a user is either on 3G or Wifi. I will update my question to make it more clear. Cheers – Dave Jul 15 '11 at 23:08