Checking network types explicitly like this is bad - what happens if the device is connected through a bluetooth tether or via ethernet adapter?
You could use getActiveNetworkInfo() and check it's connection, but this is still polling. If you need to get notified when a connection comes or goes (have something to do on next connect, for example) The CONNECTIVITY_ACTION broadcast intent is the way to go. Note that this can be chatty - there are several secondary networks that currently cause this broadcast that you may have to weed out. You should use getActiveNetworkInfo in your handler to find the state that applies to you. Can you post the stack trace of the NPE? R On Wed, Oct 3, 2012 at 5:46 AM, Rahul Kaushik <rahulkaushi...@gmail.com>wrote: > hi subodh > > try this > > package com.FranConnectMobile; > > import java.io.BufferedReader; > import java.io.InputStreamReader; > import java.net.URL; > import android.app.Activity; > import android.content.Context; > import android.net.ConnectivityManager; > > > > > public class chkInternet extends Activity > { > public boolean isInternetAvailable(Context context){ > ConnectivityManager connec = (ConnectivityManager) > context.getSystemService(Context.CONNECTIVITY_SERVICE); > android.net.NetworkInfo wifi = > connec.getNetworkInfo(ConnectivityManager.TYPE_WIFI); > android.net.NetworkInfo mobile = > connec.getNetworkInfo(ConnectivityManager.TYPE_MOBILE); > > if(wifi.isConnected() || mobile.isConnected()){ > // Check for web site > try{ > // Create a URL for the desired page > URL url = new URL("http://www.google.com"); > // Read all the text returned by the server > BufferedReader in = new BufferedReader(new > InputStreamReader(url.openStream())); > in.close(); > return true; > } catch (Exception e) { > return false; > } > } > > return false; > } > > } > > TX > RK > > On Wed, Oct 3, 2012 at 6:08 PM, Subodh Nijsure > <subodh.nijs...@gmail.com>wrote: > >> Hello, >> >> I am trying to implement a service that is supposed to download stuff >> from a cloud service. So I want this service to be notified whenever >> phone/tablet looses network connectivity. >> >> >> So I implemented code that looks like this: >> >> receiver = new ConnectivityReceiver(); >> filter = new IntentFilter(); >> filter.addAction(ConnectivityManager.CONNECTIVITY_ACTION); >> try { >> Intent i = registerReceiver(receiver, >> filter); >> } >> catch (Exception e) { >> .... >> } >> However when I have this code in a class that extends a Service >> registerReciver always throws a NullPointerException. >> >> However the same code in a class that extends an activity does not throw >> such an exception. I have attached my manifest file and the full code for >> the service template. Would appreciate any pointers as to why this works in >> activity and not in a service. >> >> Or is there any better way for a service that run in the background to >> know when device looses/gains (IP) network connectivity? >> >> Regards, >> -Subodh >> >> -- >> You received this message because you are subscribed to the Google >> Groups "Android Developers" group. >> To post to this group, send email to android-developers@googlegroups.com >> To unsubscribe from this group, send email to >> android-developers+unsubscr...@googlegroups.com >> For more options, visit this group at >> http://groups.google.com/group/android-developers?hl=en > > > -- > You received this message because you are subscribed to the Google > Groups "Android Developers" group. > To post to this group, send email to android-developers@googlegroups.com > To unsubscribe from this group, send email to > android-developers+unsubscr...@googlegroups.com > For more options, visit this group at > http://groups.google.com/group/android-developers?hl=en > -- You received this message because you are subscribed to the Google Groups "Android Developers" group. To post to this group, send email to android-developers@googlegroups.com To unsubscribe from this group, send email to android-developers+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/android-developers?hl=en