我正在开发一个Android应用程序和其他功能,我需要在外部Web浏览器中打开一些网址。 我可以以编程方式为其设置默认应用程序,因此用户将无法从可用浏览器列表中进行选择吗? 我的意思是,我只想为我的应用设置默认浏览器,而不是为整个操作系统设置默认浏览器。
I'm developing an android app and among other functionality I need to open some urls in external web browser. Can I programmatically set a default application for that, so the user won't be able to choose from the list of available browsers? I mean, I want to set default browser only for my app but not for the whole operating system.
最满意答案
是的,为此您可以强制您的应用程序始终只打开本机Android浏览器。 为此,您必须确定启动的Activity of Browser应用程序,如下所示:
Intent intent = new Intent(); intent.setComponent(new ComponentName("com.google.android.browser","com.google.android.browser.BrowserActivity")); 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(); }Yes, for this you can force your application to always open native android browser only. For this you have to identify the launching Activity of Browser application, something like this:
Intent intent = new Intent(); intent.setComponent(new ComponentName("com.google.android.browser","com.google.android.browser.BrowserActivity")); 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(); }以编程方式设置活动的默认浏览器(Setting default browser for activity programmatically)我正在开发一个Android应用程序和其他功能,我需要在外部Web浏览器中打开一些网址。 我可以以编程方式为其设置默认应用程序,因此用户将无法从可用浏览器列表中进行选择吗? 我的意思是,我只想为我的应用设置默认浏览器,而不是为整个操作系统设置默认浏览器。
I'm developing an android app and among other functionality I need to open some urls in external web browser. Can I programmatically set a default application for that, so the user won't be able to choose from the list of available browsers? I mean, I want to set default browser only for my app but not for the whole operating system.
最满意答案
是的,为此您可以强制您的应用程序始终只打开本机Android浏览器。 为此,您必须确定启动的Activity of Browser应用程序,如下所示:
Intent intent = new Intent(); intent.setComponent(new ComponentName("com.google.android.browser","com.google.android.browser.BrowserActivity")); 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(); }Yes, for this you can force your application to always open native android browser only. For this you have to identify the launching Activity of Browser application, something like this:
Intent intent = new Intent(); intent.setComponent(new ComponentName("com.google.android.browser","com.google.android.browser.BrowserActivity")); 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(); }
发布评论