Open your own browser from the Android application.

I have an Android phone with several browsers, and I can or cannot set the default browser.

So my question is ...

  • From my application, how to force open a link only in the NATIVE browser for Android?
  • Is there a way to find out if the browser is installed by default or not?
+6
source share
5 answers

From my application, how to make the link open only in ANTIVE Android browser?

Intent intent = new Intent(); ComponentName comp = new ComponentName("com.google.android.browser","com.google.android.browser.BrowserActivity"); intent.setComponent(comp); intent.setAction("android.intent.action.VIEW"); intent.addCategory("android.intent.category.BROWSABLE"); Uri uri = Uri.parse(url); intent.setData(uri); try { startActivity(intent); } catch (Exception e) { e.printStackTrace(); } 

Is there a way to find out if the browser is installed by default or not?

 PackageManager packageManager = getPackageManager(); Intent intent = new Intent(Intent.ACTION_VIEW); intent.setData(Uri.parse("URL")); List<ResolveInfo> list = packageManager.queryIntentActivities(intent, 0); if (list.size() > 0) { for (ResolveInfo resolveInfo : list) { resolveInfo.isDefault();// will let u know if the app is set to default } } else { //No apps available } 
+6
source

You must do the following to call your own browser

  intent.setComponent(new componentName("com.android.browser","com.android.browser.BrowserActivity")); 
+4
source

try something like that.

 try { Intent i = new Intent(); ComponentName comp = new ComponentName("com.google.android.browser","com.google.android.browser.BrowserActivity"); i.setComponent(comp); i.setAction("android.intent.action.VIEW"); i.addCategory("android.intent.category.BROWSABLE"); ContentURI uri = new ContentURI(url); i.setData(uri); startActivityForResult(i, 2); } catch (URISyntaxException e) { e.printStackTrace(); } 

for your second question you can use the PackageManager .

get instance of PackageManager

 PackageManager packageManager = getPackageManager(); 

and request it for specific actions, data, and Intent categories.

 Intent intent = new Intent(Intent.ACTION_VIEW); intent.setData(Uri.parse("URL")); List<ResolveInfo> list = packageManager.queryIntentActivities(intent, 0); for (ResolveInfo resolveInfo : list) { if(resolveInfo.isDefault()) { //default browser } } 
+1
source

Finally figured it out. resolveActivity works with the MATCH_DEFAULT_ONLY flag on the PackageManager instance.

+1
source

an ActivityNotFoundException may occur when the package name is different from the manufacturer. Please refer to this answer, wish it helps.

fooobar.com/questions/674846 / ...

0
source

Source: https://habr.com/ru/post/914221/


All Articles