4
4
4ainik2020-11-01 12:08:43
Android
4ainik, 2020-11-01 12:08:43

How to open URL programmatically?

The code is like this:

public void open_browser(String url){
    PackageManager packageManager = getPackageManager();
    Intent intent = new Intent(Intent.ACTION_VIEW);
    intent.setData(Uri.parse(url));
    List<ResolveInfo> list = packageManager.queryIntentActivities(intent, 0);
    for (ResolveInfo resolveInfo : list) {
      ComponentName comp = new ComponentName(resolveInfo.activityInfo.packageName, "com.android.browser.BrowserActivity");
      intent.setComponent(comp);
      startActivity(intent);
      break;
    }
  }

on android 4 it works with a bang, and on 9 the list consists of only one packageName=ru.yandexsearchplugin - it doesn’t work and where did the chrome go, too?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
O
Oleg, 2020-11-01
@402d

private void openExtUrl(String url) {
        final Uri parsedUri = Uri.parse(url);
        PackageManager packageManager = getPackageManager();
        Intent browseIntent = new Intent(Intent.ACTION_VIEW).setData(parsedUri);
        // Create intent to show chooser
        Intent chooser = Intent.createChooser(browseIntent, "Open url with ..");
        if (browseIntent.resolveActivity(packageManager) != null) {
            startActivity(chooser);
        }

    }

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question