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?
To open a URL/website you do the following: String url = "http://www.example.com"; Intent i = new Intent(Intent. ACTION_VIEW); i.
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.
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.
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);
You can do one of two things:
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With