2

I'm having a strange situation, where an intent receiver is registered with LocalBroadcastManager either from service or from MainActivity, and when intent is sent from PHONE_STATE receiver (defined in AndroidManifest.xml), it's never received.

A "self-test" with sending same intent from activity|service - works.

Is it worth trying to specify LocalBroadcastManager's intent to be received via AndroidManifest.xml?


Service is defined as:

    <service
     android:name=".AppService"
     android:process=":remote">
      <intent-filter>
       <action android:name="me.cmp.app.AppService" />
      </intent-filter>
    </service>

In service:

 public class AppService extends Service {
 @Override
 public void onCreate() {
        super.onCreate();
        LocalBroadcastManager.getInstance(this).registerReceiver(mMessageReceiver, new IntentFilter("me.cmp.app.statechange"));

            // self-test uses same intent sending code:
            Intent intent2 = new Intent("me.cmp.app.statechange");
            intent2.putExtra("message", "selfsend");
            LocalBroadcastManager.getInstance(this).sendBroadcastSync(intent2);

 }  
 ...

In manifest:

    <receiver android:name="me.cmp.app.PhoneReceiver" >
        <intent-filter>
            <action android:name="android.intent.action.PHONE_STATE" >
            </action>
        </intent-filter>
    </receiver>        

Listener:

public class PhoneReceiver extends BroadcastReceiver {

  @Override
  public void onReceive(Context context, Intent intent) {
    Bundle extras = intent.getExtras();
    if (extras != null) {
      Log.e("test", extras.getString(TelephonyManager.EXTRA_STATE));

      Intent intent2 = new Intent("me.cmp.app.statechange");
      intent2.putExtra("message", state.toString());
      LocalBroadcastManager.getInstance(context).sendBroadcastSync(intent2);
      Log.w("test", "Broadcast sent");
    }
  }
}

--

The main problem seems to be Should I use android: process =":remote" in my receiver? ; however I'm still not sure why MainActivity's receiver didn't worked earlier, maybe fully qualified names are a must.

Community
  • 1
  • 1
kagali-san
  • 2,964
  • 7
  • 48
  • 87
  • Post the code for each broadcast send. – A--C Jan 10 '14 at 04:23
  • current code does not works (so, neither fully qualified intent names or sendBroadcastSync) are helping much. – kagali-san Jan 10 '14 at 04:32
  • The discrepancy I see is that your Service is `App` whereas the manifest has `AppService`. I'd make sure that the service is running but I'd take out `android:process=":remote"`to see if that does anything. – A--C Jan 10 '14 at 04:38
  • 1
    @A--c App/Appservice is a typo; removing android:process=":remote" worked, can you make it the answer? – kagali-san Jan 10 '14 at 04:40
  • I saw your edit, what do you mean by `MainActivity's receiver didn't worked earlier?` If the service is the only thing that is `remote`, it makes sense that the `LocalBroadcasts` won't work as expected – A--C Jan 10 '14 at 04:50

1 Answers1

12

The LocalBroadcastReceiver documentation states that it is a

Helper to register for and send broadcasts of Intents to local objects within your process

This means if you are using a Service that is running in a separate process (eg android:process=":remote"), LocalBroadcastManager is quite likely to fail (albeit silently) because you are probably getting two separate instances of this class (one for each process).

A--C
  • 36,351
  • 10
  • 106
  • 92