4

Below 30 API there were two methods I could use to check if intent can be handled by some app activity:

fun Intent.canBeHandled(packageManager: PackageManager): Boolean {
    return resolveActivity(packageManager) != null
}

fun PackageManager.isIntentCanBeHandled(intent: Intent): Boolean {
    val infos = queryIntentActivities(intent, 0)
    return infos.isNotEmpty()
}

But when I test on Pixel 3 API 30 Emulator it doesn't work as expected, e.g. queryIntentActivities() returns usually 0 or 1 (e.g. 1 for send intent when it should have been 7, as it was with API 29-)

Basically Intent.createChooser(intent, "") works correctly and suggests 2 apps (for example) but ext funcs Intent.canBeHandled() & PackageManager.isIntentCanBeHandled() return false

user924
  • 8,146
  • 7
  • 57
  • 139

1 Answers1

-2

Intent needs to be created as a chooser - Please refer to last few lines of following example:

public class MainActivity extends AppCompatActivity {
private static final String cstLogTag = "Implicit Intents";
private enum enuShare {shareCompat, shareSheet, intentResolver}
@Override protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
}
public void buttonClicked(View view) {
    EditText editText;
    String strText, strButton;
    Uri uri;
    Intent chooser, intent;
    chooser = intent = null;
    switch (view.getId()) {
        case R.id.btnWebsite:
            strButton = "btnWebsite";
            editText = findViewById(R.id.edtWebsite);
            strText = editText.getText().toString();
            uri = Uri.parse(strText);
            intent = new Intent(Intent.ACTION_VIEW, uri);
            break;
        case R.id.btnLocation:
            strButton = "btnLocation";
            editText = findViewById(R.id.edtLocation);
            strText = editText.getText().toString();
            uri = Uri.parse("geo:0,0?q=" + strText);
            intent = new Intent(Intent.ACTION_VIEW, uri);
            break;
        case R.id.btnShareText:
            strButton = "btnShareText";
            editText = findViewById(R.id.edtShareText);
            strText = editText.getText().toString();
            intent = new Intent(Intent.ACTION_SEND);
            intent.setType("text/plain");
            intent.putExtra(Intent.EXTRA_TEXT, strText);
            intent.putExtra(Intent.EXTRA_SUBJECT, "Just Testing");
            break;
        default:
            Log.d(cstLogTag, "Button not recognised");
            return;
    }
    //intent.resolveActivity(getPackageManager()) returns null so...
    chooser = Intent.createChooser(intent, "Choose");
    if(chooser.resolveActivity(getPackageManager()) == null)
        Log.d(cstLogTag, "Unresolved activity for " + strButton);
    else
        startActivity(chooser);
}

}