0

I have a background service which has a receiver for connectivity change which only seems to be received if the activity is active.

@Override
public void onCreate() {

    mContext = this;

    IntentFilter connectivityChangeFilter = new IntentFilter("android.net.conn.CONNECTIVITY_CHANGE");
    registerReceiver(receiver, connectivityChangeFilter);

I've set it up in the manifest as follows:

<service
     android:name="com.myservice.TimeService"
     android:label="com.myservice.TimeService" >
     <intent-filter>
          <action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
     </intent-filter>
</service>

I have another receiver for boot completed which works ok, which is registered as a receiver in the manifest (unlike this one).

Is the intent filter not enough to run a broadcast? I would want the receiver to call a method on the service so it needs to be able to access methods of the service but I don't think receivers can bind to services.

-- Update In a nutshell, I want to know if I can statically declare a receiver that interacts with a service. Dynamic declaration works only if the app is active.

Carrie Hall
  • 931
  • 3
  • 15
  • 30

1 Answers1

0

Use android sticky intent

A normal broadcast Intent is not available anymore after is was send and processed by the system. If you use the sendStickyBroadcast(Intent) method, the Intent is sticky, meaning the Intent you are sending stays around after the broadcast is complete.

example code here:

Community
  • 1
  • 1
Ali Imran
  • 8,927
  • 3
  • 39
  • 50
  • thanks, it's interesting but not exactly what I want. I'm not bothered about the broadcast being lost, I only need it to run a method of the service. – Carrie Hall Dec 12 '12 at 11:36
  • May be this post help :-http://stackoverflow.com/questions/8169749/send-message-from-activity-to-service-android – Ali Imran Dec 12 '12 at 11:42