I was watching a tutorial video on android development where they were teaching about implicit intents. Its a simple app really, it explains how implicit intent works by opening an URL through the app. Anyways, I follow all of the steps, I even make a toast to see if the edit text view was receiving the url string or not and came up with this:
button = view.findViewById(R.id.button);
url = view.findViewById(R.id.url);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String urlText = url.getText().toString();
Toast.makeText(getActivity(), urlText, Toast.LENGTH_SHORT).show();
//Implicit Intent to open a web page
Uri webpage = Uri.parse(urlText);
Intent intent = new Intent(Intent.ACTION_VIEW, webpage);
if (intent.resolveActivity(getActivity().getPackageManager()) != null) {
startActivity(intent);
}
When I do press the button (hoping it would ask which app to open the link from) it just shows the toast...
I know there might be a silly mistake here but I just can't see it please help!
CodePudding user response:
On Android 11 and above there are limitation to querying other packages. See https://developer.android.com/training/package-visibility
Because of that your intent.resolveActivity(getActivity().getPackageManager())
fails and the startActivity()
is not executed.
I would just delete the if (intent.resolveActivity(getActivity().getPackageManager()) != null)
altogether and just call startActivity()
without any checks. In case there is no app to handle your Intent, ActivityNotFoundException
gets thrown. You can add a try-catch for that.