【问题标题】:Add my browser in the default browser selection list in android?在android的默认浏览器选择列表中添加我的浏览器?
【发布时间】:2019-04-24 21:04:09
【问题描述】:
遵循How to add my browser in the default browser selection list in android? 的建议。我在manifest 文件中指定了我的Intent:
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<data android:scheme="http"/>
<data android:scheme="https"/>
</intent-filter>
我也添加了权限:
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
但我的浏览器仍然没有显示在设置中浏览器类别的默认应用程序选项中。
我是否需要做任何其他事情才能让我的应用显示在浏览器的默认选项中?
【问题讨论】:
标签:
java
android
android-intent
kotlin
default
【解决方案1】:
尝试在您的目标活动的intent-filter 中包含<category android:name="android.intent.category.BROWSABLE" />,正如developer documentation 所说:
如果用户正在查看网页或电子邮件并单击文本中的链接,则生成的执行该链接的 Intent 将需要 BROWSABLE 类别,因此只有支持此类别的活动才会被视为可能的行动。
为了使intent-filter 可以从可点击的链接访问,这是必需的。没有它,单击链接将无法解析到您的应用。
<activity ...>
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<data android:scheme="http" />
<data android:scheme="https" />
</intent-filter>
</activity>
.
附加提示:(如果你想强制你的应用成为默认浏览器)
Android App Links 在 Android 6.0(API 级别 23)及更高版本上允许应用将自己指定为给定类型链接的默认处理程序。如果用户不希望应用成为默认处理程序,他们可以从设备的系统设置中覆盖此行为。
要为您的应用启用链接处理验证,请在intent-filter 标签中设置android:autoVerify="true":
<activity ...>
<intent-filter android:autoVerify="true">
...
</intent-filter>
</activity>
【解决方案2】:
您需要考虑可能适用的各种情况。
请参考下面的意图过滤器。最后还提供了链接。
<activity android:name="BrowserActivity"
android:label="@string/application_name"
android:launchMode="singleTask"
android:alwaysRetainTaskState="true"
android:configChanges="orientation|keyboardHidden"
android:theme="@style/BrowserTheme"
android:windowSoftInputMode="adjustResize" >
<intent-filter>
<action android:name="android.speech.action.VOICE_SEARCH_RESULTS" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
<!-- For these schemes were not particular MIME type has been
supplied, we are a good candidate. -->
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<data android:scheme="http" />
<data android:scheme="https" />
<data android:scheme="about" />
<data android:scheme="javascript" />
</intent-filter>
<!-- For these schemes where any of these particular MIME types
have been supplied, we are a good candidate. -->
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.BROWSABLE" />
<category android:name="android.intent.category.DEFAULT" />
<data android:scheme="http" />
<data android:scheme="https" />
<data android:scheme="inline" />
<data android:mimeType="text/html"/>
<data android:mimeType="text/plain"/>
<data android:mimeType="application/xhtml+xml"/>
<data android:mimeType="application/vnd.wap.xhtml+xml"/>
</intent-filter>
<!-- We are also the main entry point of the browser. -->
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.LAUNCHER" />
<category android:name="android.intent.category.BROWSABLE" />
</intent-filter>
<!-- The maps app is a much better experience, so it's not
worth having this at all... especially for a demo!
<intent-filter android:label="Map In Browser">
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<data android:mimeType="vnd.android.cursor.item/postal-address" />
</intent-filter>
-->
<intent-filter>
<action android:name="android.intent.action.WEB_SEARCH" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<data android:scheme="" />
<data android:scheme="http" />
<data android:scheme="https" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.MEDIA_SEARCH" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.SEARCH" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
<meta-data android:name="android.app.searchable"
android:resource="@xml/searchable" />
</activity>
查看您可能需要的不同类型的意图过滤器以涵盖所有可能的情况。
参考这个link - froyo 浏览器的完整清单文件。
【解决方案3】:
考虑将CATEGORY_APP_BROWSER 与您的主过滤器一起使用:
与ACTION_MAIN 一起使用以启动浏览器应用程序。该活动应该能够浏览互联网。
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
<category android:name="android.intent.category.APP_BROWSER" />
</intent-filter>
【解决方案4】:
您可以使用Intent-Filter 来做到这一点,方法是让您的Activity 可浏览。在Android 清单文件中使用以下代码。
您可以通过Launcher Activity 来实现。
<activity android:name="BrowsableActivity">
<intent_filter>
<action android:name="android.intent.action.VIEW"/>
<category android:name="android.intent.category.BROWSABLE"/>
<category android:name="android.intent.category.DEFAULT"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent_filter>
</activity>
并且您必须提供scheme 为http 和https 才能在您的应用程序中打开链接。
【解决方案5】:
如文档中提到的 developer.android.com
你需要如下设置intent filer
<activity ...>
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<!-- Include the host attribute if you want your app to respond
only to URLs with your app's domain. -->
<data android:scheme="http" android:host="www.example.com" />
<category android:name="android.intent.category.DEFAULT" />
<!-- The BROWSABLE category is required to get links from web pages. -->
<category android:name="android.intent.category.BROWSABLE" />
</intent-filter>
</activity>
【解决方案6】:
您可以通过编程方式实现这一点,如下所示:
Intent share = new Intent(android.content.Intent.ACTION_SEND);
share.setType("text/plain");
share.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
share.setPackage("your.app.package.name");
startActivity(share);
如果您在手机上检查应用程序是否存在,那就太好了。
快乐编码:)