Skip to content

Instantly share code, notes, and snippets.

@rogerbinns
Created July 30, 2012 03:30
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rogerbinns/3204034 to your computer and use it in GitHub Desktop.
Save rogerbinns/3204034 to your computer and use it in GitHub Desktop.
Chaining install broadcast receivers
@Override
public void onReceive(Context context, Intent intent) {
// ensure all declared receivers can handle this intent
intent.setComponent(null);
// do what Market/Store/Finsky should have done in the
// first place
List l = context.getPackageManager()
.queryBroadcastReceivers(intent, 0);
for (ResolveInfo ri : l) {
ActivityInfo ai = ri.activityInfo;
if (ai.enabled && ai.exported &&
ai.packageName.equals(context.getPackageName())) {
String name = ai.name;
// don't call ourselves
if (ai.name.equals(this.getClass().getName())) {
continue;
}
try {
BroadcastReceiver b = (BroadcastReceiver)
Class.forName(name).newInstance();
intent.setClassName(context, name);
b.onReceive(context, intent);
} catch (Throwable t) {
Log.e(LOGTAG, "Error with receiver %s", name), t);
t.printStackTrace();
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment