Multiple actions competing with one intention

I have an interview question .....

How to specify which activity should be triggered from an implicit intent when there are several actions competing for the fulfillment of the intent without user intervention.

My answer to this question is to use the correct intent filter within each action, but it just sounds wrong.

Thanks in advance!

+4
source share
1 answer

When creating an Intent, you can pass the explicit name of the component. i.e. class name . Only this component will now receive the intention.

Example:

Intent myIntent = new Intent(getApplicationContext(),RequiredActivity.class); startActivity(myIntent); 

If you do not specify the exact component, Android will allow the user to select one of the components that processes the intent.

Example:

  Intent myIntent = new Intent(Intent.ACTION_VIEW); startActivity(myIntent); 

If you want to go through all the components that handle the intent, you yourself, instead of letting users show Android versions, you can also do this:

Example:

  Intent myIntent = new Intent(Intent.ACTION_VIEW); List<ResolveInfo> infoList = getPackageManager().queryIntentActivities(myIntent, 0); for (ResolveInfo ri : infoList){ ActivityInfo ai = ri.activityInfo; String packageName = ai.packageName; String componentName = ai.name; // you can pick up appropriate activity to start // if(isAGoodMatch(packageName,componentName)){ // myIntent.setComponent(new ComponentName(packageName,componentName)); // startActivity(myIntent); // break; // } } 

I got six activity matches for the code above:

enter image description here

+7
source

Source: https://habr.com/ru/post/1498800/


All Articles