Created
July 30, 2012 03:30
-
-
Save rogerbinns/3204034 to your computer and use it in GitHub Desktop.
Chaining install broadcast receivers
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@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