Broadcasting an intent to a specific component.

Posted by Nic Strong on Stack Overflow See other posts from Stack Overflow or by Nic Strong
Published on 2010-05-04T01:41:56Z Indexed on 2010/05/04 1:48 UTC
Read the original article Hit count: 387

Filed under:
|

I have an activity that is starting a background operation in another service. This activity receives various notifications from this service. As part of the intent I use to initiate the background operation I pass extra data with the context of my activity so the background service can broadcast intents back to me (the download service is a good example of this usage).

So in the activity I use the following to attach the context:

intent.putExtra(Intents.EXTRA_NOTIFICATION_PACKAGE, IntentTestActivity.this.getPackageName());
intent.putExtra(Intents.EXTRA_NOTIFICATION_CLASS, IntentTestActivity.class.getCanonicalName());
intent.putExtra(Intents.EXTRA_NOTIFICATION_EXTRAS, myContext);

I register for the notifications in the activity:

IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction(Intents.ACTION_NOTIFICATION);
intentFilter.addCategory(Intents.CATEGORY_COMPLETION);
intentFilter.addCategory(Intents.CATEGORY_PROGRESSS);
Intent intent = registerReceiver(receiver, intentFilter);

In the background service I send notifications with the following code:

void broadcastNotification(String action, String category, String packageName, String className, String extras, int operationResult)
{
    Intent intent = new Intent(action);
    intent.addCategory(category);
    intent.setClassName(packageName, className);
    if (extras != null) {
        intent.putExtra(Intents.EXTRA_NOTIFICATION_EXTRAS, extras);
    }
    intent.putExtra(Intents.EXTRA_OPERATION_RESULT, operationResult);
    context.sendBroadcast(intent);
}

My problem is that the above broadcast will never be received. If however I comment out the line

intent.setClassName(packageName, className);

Then the broadcast is received. Is it a problem with my filter? Do I have to specify intents intended for a specific component? Or cannot I not use such fine grain control over the delivery of broadcasts.

Thanks, Nic

© Stack Overflow or respective owner

Related posts about android

Related posts about intent