axr 0284
axr 0284

Reputation: 113

intentService to activity using LocalBroadcastManager

I have been trying to use LocalBroadcastManager but it does not seem to work. Basically I want my intentService to broadcast an intent whenever it's running. I then want my activity to receive the broadcast whenever the user is actively using the activity.

I tried to follow the instructions here: how to use LocalBroadcastManager?

but it does not work for some reason. I don't get any log entry for the receiver being called.

Here is the relevant part of my code:

public class MainActivity extends Activity {
  // Debug log tag
  private static final String tag = "PhoneControlMainActivity";

  // Broadcast receiver
  PhoneControlBroadcastReceiver receiver;

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    // Register the receiver with the local broadcast manager
    receiver = new PhoneControlBroadcastReceiver();
    LocalBroadcastManager.getInstance(this).registerReceiver(receiver, new IntentFilter(PhoneControlBroadcastReceiver.SERVICE_OUTPUT));

    // Start IntentService
    try {
      serviceName = startService(new Intent(this, PhoneControlIntentService.class));
    }
    catch(SecurityException e)
    {
      Log.d(tag,"SecurityException occured when starting IntentService: "+ e.getMessage());
      return;
    }

    if(serviceName != null) {
      Log.d(tag,"serviceName NOT NULL");
    }
    else {
      Log.d(tag,"serviceName NULL");
    }
  }
}

public class PhoneControlIntentService extends IntentService  {
  public PhoneControlIntentService() {
    super("PhoneControlIntentService");
  }

  @Override
  protected void onHandleIntent(Intent intent) {    
    Intent msgIntent = new Intent(PhoneControlBroadcastReceiver.SERVICE_OUTPUT);
    msgIntent.putExtra("message", "This is my message!");
    LocalBroadcastManager.getInstance(this).sendBroadcast(msgIntent);
  }
}

public class PhoneControlBroadcastReceiver extends BroadcastReceiver{   
  // Action
  public static final String SERVICE_OUTPUT = "com.axr0284.phonecontrol.SERVICE_OUTPUT";

  @Override
  public void onReceive(Context context, Intent intent) {
    Log.d(tag,"onReceive called");      
  }
}

Any help would be highly appreciated? Thanks Amish

Upvotes: 0

Views: 1957

Answers (2)

axr 0284
axr 0284

Reputation: 113

Here is what I did wrong, my service declaraion in the AndroidManifest.xml had this line

android:process=":PhoneControlIntentService_process"

<service
    android:name=".PhoneControlIntentService" 
    android:label="@string/IntentService_name"
    android:process=":PhoneControlIntentService_process"**
    android:exported="false"/>

This is supposed to start the service as a separate process. I guess somehow it prevented the localBroadcastManager to send the intent correctly. I don't quite understand it but when I removed that line, it started working.

Upvotes: 0

ostergaard
ostergaard

Reputation: 3507

A LocalBroadcastManager can't broadcast outside of it's own process.

Upvotes: 3

Related Questions