Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Open URL in browser even though my app registered an intent-filter for it

My app registers an intent filter for certain URLs because it can handle the data from those URLs.

However, inside the app I would like to provide a button to open such an URL in the browser. That is, open it in the default browser if one is set, otherwise provide a chooser - just like normal.

Now when my app is set as default for those URLs and I press the button, naturally I just get the same activity in my app again and again and again.

Any ideas?

like image 891
AndreKR Avatar asked Apr 09 '15 03:04

AndreKR


People also ask

How do I open a URL from an intent?

To open a URL/website you do the following: String url = "http://www.example.com"; Intent i = new Intent(Intent. ACTION_VIEW); i.

What is meant by intent filter?

An intent filter is an expression in an app's manifest file that specifies the type of intents that the component would like to receive. For instance, by declaring an intent filter for an activity, you make it possible for other apps to directly start your activity with a certain kind of intent.

What is difference between intent and intent filter in Android?

An intent is an object that can hold the os or other app activity and its data in uri form.It is started using startActivity(intent-obj).. \n whereas IntentFilter can fetch activity information on os or other app activities.


2 Answers

What I ended up doing is:

Intent i = new Intent(Intent.ACTION_VIEW,
        Uri.parse("https://website.com/entries/" + entry_id));

// Check what is the default app to handle these entry URLs

ResolveInfo defaultResolution = getPackageManager().resolveActivity(i,
        PackageManager.MATCH_DEFAULT_ONLY);

// If this app is the default app for entry URLs, use the browser instead

if (defaultResolution.activityInfo.packageName.equals(getPackageName())) {

    Intent browseIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("https://"));

    ResolveInfo browseResolution = getPackageManager().resolveActivity(browseIntent,
            PackageManager.MATCH_DEFAULT_ONLY);

    i.setComponent(new ComponentName(
            browseResolution.activityInfo.applicationInfo.packageName,
            browseResolution.activityInfo.name));

}

startActivity(i);
like image 184
AndreKR Avatar answered Nov 08 '22 07:11

AndreKR


You can do one of two things:

  1. Create an explicit Intent for the browser if you know that one (Chrome, for example) will be installed on the device
  2. Create a chooser by using

    Intent i = new Intent(Intent.ACTION_VIEW, Uri.parse("google.com")); Intent chooserIntent = Intent.createChooser(i, "Title for chooser"); startActivity(chooserIntent);

You can read more about createChooser here

like image 22
A J Avatar answered Nov 08 '22 09:11

A J