Suppose there is an application with package name com.example
.
The application has an activity which has an intent filter android.intent.action.SEND
Now I want to programatically find the component class that supports the above intent filter.
This code filters all the classes that matches ACTION_SEND.
Intent sendIntent = new Intent();
sendIntent.setAction(Intent.ACTION_SEND);
sendIntent.putExtra(Intent.EXTRA_TEXT, "This is my text to send.");
sendIntent.setType("text/plain");
startActivity(sendIntent);
But, I want to choose the activity only from the package that matches com.example
.
This is how I achieved it. Get the list of all the packages that support the intent.
Create share intent
The PackageManager can be retrieved with Context.getPackageManager() and queryIntentActivities with the intent you are considering will return the ResolveInfo of Activities that can perform that intent.
ResolveInfo.activityInfo will give you the ActivityInfo which has packageName which is what you are looking to filter on.
Once you have selected the target you want, you can make the ComponentName for that activity using the package name and class and setComponent() on the desired intent to explicitly target the activity you want.