Account Options

  1. Sign in
The old Google Groups will be going away soon, but your browser is incompatible with the new version.
Google Groups Home
« Groups Home
Unable to register CONNECTIVITY_ACTION in a service
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  5 messages - Collapse all  -  Translate all to Translated (View all originals)
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
Subodh Nijsure  
View profile  
 More options Oct 3 2012, 8:38 am
From: Subodh Nijsure <subodh.nijs...@gmail.com>
Date: Wed, 3 Oct 2012 05:38:09 -0700 (PDT)
Local: Wed, Oct 3 2012 8:38 am
Subject: Unable to register CONNECTIVITY_ACTION in a service

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

  AndroidManifest.xml
1K Download

  ScsService.java
5K Download

 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Rahul Kaushik  
View profile  
 More options Oct 3 2012, 8:47 am
From: Rahul Kaushik <rahulkaushi...@gmail.com>
Date: Wed, 3 Oct 2012 18:16:01 +0530
Local: Wed, Oct 3 2012 8:46 am
Subject: Re: [android-developers] Unable to register CONNECTIVITY_ACTION in a service

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:


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Robert Greenwalt  
View profile  
 More options Oct 3 2012, 10:43 am
From: Robert Greenwalt <rgreenw...@google.com>
Date: Wed, 3 Oct 2012 07:39:09 -0700
Local: Wed, Oct 3 2012 10:39 am
Subject: Re: [android-developers] Unable to register CONNECTIVITY_ACTION in a service

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:


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Subodh Nijsure  
View profile  
 More options Oct 4 2012, 12:19 am
From: Subodh Nijsure <subodh.nijs...@gmail.com>
Date: Wed, 3 Oct 2012 21:17:24 -0700
Local: Thurs, Oct 4 2012 12:17 am
Subject: Re: [android-developers] Unable to register CONNECTIVITY_ACTION in a service
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


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Robert Greenwalt  
View profile  
 More options Oct 4 2012, 8:29 pm
From: Robert Greenwalt <rgreenw...@google.com>
Date: Thu, 4 Oct 2012 17:26:48 -0700
Local: Thurs, Oct 4 2012 8:26 pm
Subject: Re: [android-developers] Unable to register CONNECTIVITY_ACTION in a service

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 <subodh.nijs...@gmail.com>wrote:


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »