You are registering in your constructor. I think you need to register in onCreate. You appear to not have a context when your constructor is called.
On Wed, Oct 3, 2012 at 9:17 PM, Subodh Nijsure <[email protected]>wrote: > Hello Robert, > > Below is the stack trace that I see when system catches the exception: > > Not sure if you had any chance glancing at my sample code on original > email, and see if anything obviously wrong there? > > I/System.out(12738): java.lang.NullPointerException > I/System.out(12738): at > android.content.ContextWrapper.registerReceiver(ContextWrapper.java:341) > I/System.out(12738): at > com.mycompany.ScsService.ScsService.<init>(ScsService.java:50) > I/System.out(12738): at java.lang.Class.newInstanceImpl(Native Method) > I/System.out(12738): at java.lang.Class.newInstance(Class.java:1319) > I/System.out(12738): at > android.app.ActivityThread.handleCreateService(ActivityThread.java:2234) > I/System.out(12738): at > android.app.ActivityThread.access$1600(ActivityThread.java:123) > I/System.out(12738): at > android.app.ActivityThread$H.handleMessage(ActivityThread.java:1201) > I/System.out(12738): at > android.os.Handler.dispatchMessage(Handler.java:99) > I/System.out(12738): at android.os.Looper.loop(Looper.java:137) > I/System.out(12738): at > android.app.ActivityThread.main(ActivityThread.java:4424) > I/System.out(12738): at java.lang.reflect.Method.invokeNative(Native > Method) > I/System.out(12738): at java.lang.reflect.Method.invoke(Method.java:511) > I/System.out(12738): at > > com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784) > I/System.out(12738): at > com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551) > I/System.out(12738): at dalvik.system.NativeStart.main(Native Method) > > -Subodh > > On Wed, Oct 3, 2012 at 7:39 AM, Robert Greenwalt <[email protected]> > wrote: > > 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 <[email protected]> > > 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 < > [email protected]> > >> 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 > [email protected] > >>> To unsubscribe from this group, send email to > >>> [email protected] > >>> 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 > [email protected] > >> To unsubscribe from this group, send email to > >> [email protected] > >> 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 [email protected] > > To unsubscribe from this group, send email to > > [email protected] > > 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 [email protected] > To unsubscribe from this group, send email to > [email protected] > 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 [email protected] To unsubscribe from this group, send email to [email protected] For more options, visit this group at http://groups.google.com/group/android-developers?hl=en

