Skip to content

Instantly share code, notes, and snippets.

@viperwarp
Created July 29, 2016 03:31
Show Gist options
  • Save viperwarp/4785aca27aa519e308474a70879f6f08 to your computer and use it in GitHub Desktop.
Save viperwarp/4785aca27aa519e308474a70879f6f08 to your computer and use it in GitHub Desktop.
An Android Service that catches an implicit service intent and forwards to all services implementing that intent filter.
import android.app.IntentService;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.util.Log;
import java.util.List;
public class MyDispatchService extends IntentService {
public MyDispatchService() {
super("MyDispatchService");
}
@Override
protected void onHandleIntent(Intent intent) {
Log.d("DispatchService", "Dispatch Service Started");
if (intent != null) {
PackageManager packageManager = getPackageManager();
List<ResolveInfo> services = packageManager.queryIntentServices(intent, 0);
for (ResolveInfo info : services) {
String className = info.serviceInfo.packageName + info.serviceInfo.name;
if (!className.contains(MyDispatchService.class.getSimpleName())) {
Intent newIntent = new Intent(intent);
newIntent.setClassName(info.serviceInfo.packageName, info.serviceInfo.name);
Log.d("DispatchService", "Starting: " + info.serviceInfo.name);
startService(newIntent);
}
}
}
stopSelf();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment