3

I want to retrieve all services matching a specific intent-filter. I have the following code :

AndroidManifest.xml of a TestService :

    <service
        android:name=".TestService"
        android:exported="true"
        android:permission="com.example.MY_PERMISSIONS"
        >
        <intent-filter>
            <action android:name="com.example.myaction" />

            <data
                android:host="myhost"
                android:scheme="https" />

            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
    </service>

Querying the PackageManager in the application :

    PackageManager packageManager = getPackageManager();
    Intent intent = new Intent("com.example.myaction");
    List<ResolveInfo> resolveInfos = packageManager.queryIntentServices(intent, 0);
    Log.d(TAG,String.valueOf(resolveInfos.size()));

This returns a value = 0. I do not know what is the problem ?

devashish jasani
  • 413
  • 1
  • 4
  • 15

1 Answers1

2

Your Intent does not match the <intent-filter>.

That is mostly a problem with the <intent-filter>, IMHO, as <data> and <category> are not usually used with service <intent-filter> definitions.

That being said, if you want to keep the <intent-filter> as-is, you need to have an Intent that matches the <data> and <category> as well as the <action>. Note that Intents used with services do not automatically get CATEGORY_DEFAULT added to them, the way Intents are when used with activities.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491