Belvi Nosakhare
Belvi Nosakhare

Reputation: 3255

Can not list app using intent filter in Broadcast Receiver

I want to list apps (with same intent filter). I was able to achieve this by adding intent filter to an activity

         <activity
            android:name=".Activities.MainActivity"
            android:launchMode="singleTask"
            android:screenOrientation="portrait"
            android:windowSoftInputMode="stateHidden|adjustNothing">

            <intent-filter>
                <action android:name="com.example.identifier" />
                <category android:name="android.intent.category.DEFAULT" />
                <data android:scheme="isApp" />
            </intent-filter>
        </activity>

and i could retrieve all apps having this intent with

        String uri = "isApp:";
        Intent intent = new Intent("com.example.identifier",
                Uri.parse(uri));
        PackageManager manager = getPackageManager();
        List<ResolveInfo> list = manager.queryIntentActivities(intent, 0);

However, this launches the activity when shown in intentChoose using this snippet:

Intent zoneIntent = new Intent("com.example.identifier",
                    Uri.parse(uri));
            Intent openInChooser = Intent.createChooser(zoneIntent, "Complete Action with").setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            context.startActivity(openInChooser);

But i would want this to call a broadcast receiver. So, i moved the intent to a broadcast receiver in AndroidManifest.xml like:

<receiver
            android:name=".ExampleReceiver"
            android:enabled="true">
            <intent-filter>
                <action android:name="com.example.identifier" />
                <category android:name="android.intent.category.DEFAULT" />
                <data android:scheme="isApp" />
            </intent-filter>
        </receiver>

and the snippet that returns the number of apps with this intent returns 0 now even when the app is still on this device. Could this be done with Broadcast receiver or should i consider with another approach. Thanks.

Upvotes: 3

Views: 168

Answers (1)

David Wasser
David Wasser

Reputation: 95578

Calling queryIntentActivities() will only return Activitys. It won't return BroadcastReceivers. If you want to do this with BroadcastReceivers, then you need to call queryBroadcastReceivers() instead.

Upvotes: 1

Related Questions