Created
July 29, 2016 03:31
-
-
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.
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
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