Sma*_*ajl 7 android kotlin android-customtabs
我有一个 Android/Kotlin 应用程序,我想为某些页面配置深层链接以在我的应用程序中打开(以看起来原生)。
目前,我有意图过滤器,可以将用户重定向到带有 WebView 的活动,在其中打开所需的 url:
<activity android:name=".activity.WebViewActivity">
<intent-filter android:label="Futurity">
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<data
android:host="api.example.com"
android:pathPrefix="/auth/confirm"
android:scheme="https" />
</intent-filter>
</activity>
class WebViewActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_web_view)
val data = intent.data
// construct url
val url = if (intent.data != null ) {
"https://" + data.host + data.path + "?" + data.query
}
appWebView.webViewClient = WebViewClient()
appWebView.loadUrl(url)
}
}
Run Code Online (Sandbox Code Playgroud)
这工作正常,但出于安全原因我想使用 Chrome 自定义选项卡。
但是,当我尝试配置自定义选项卡而不是 WebView 时,我在页面(在 chrome 选项卡中启动)和意图过滤器之间出现无限循环的重定向,该过滤器立即将用户重定向回活动:
CustomTabsIntent.Builder builder = new CustomTabsIntent.Builder();
CustomTabsIntent customTabsIntent = builder.build();
customTabsIntent.launchUrl(this, Uri.parse(url));
Run Code Online (Sandbox Code Playgroud)
如何实现与 webview 相同的行为但不修改 url?它甚至可行吗?
我做了一些挖掘,发现您应该搜索支持“变暖服务”的软件包。如果返回一个包,那么您可以将包名称分配给 Intent。Google 有一个 GitHub 存储库,其中包含一些帮助程序类。有问题的是CustomTabHelper#getPackageNameToUse()。
所以在你的情况下:
CustomTabsIntent.Builder builder = new CustomTabsIntent.Builder();
CustomTabsIntent customTabsIntent = builder.build();
String packageName = CustomTabsHelper.getPackageNameToUse(getContext());
if (packageName != null) {
customTabsIntent.intent.setPackage(packageName);
}
customTabsIntent.launchUrl(this, Uri.parse(url));
Run Code Online (Sandbox Code Playgroud)
如果找不到包,那么您将需要一些后备代码,否则无限循环将继续发生。
归档时间: |
|
查看次数: |
3492 次 |
最近记录: |