gpt4 book ai didi

java - 如何在webview中打开外部链接

转载 作者:行者123 更新时间:2023-12-02 11:42:08 25 4
gpt4 key购买 nike

我已经创建了我的简单浏览器应用程序,当我点击一个链接(例如在whatsapp中)时,android会询问应该用什么浏览器打开它,我选择我的浏览器,但它不会打开该链接,而是转到主屏幕。我该怎么办?

我已将其添加到 list 中:

   <category android:name="android.intent.category.DEFAULT" />
<data android:scheme="http" />
<data android:scheme="https" />

我想我需要添加以下代码: Uri url = getIntent().getData();
webview1.loadUrl(url.toString());

但我不知道粘贴到哪里,我在Create上尝试过但失败了。

最佳答案

您应该在 OnResume() 方法中加载从中获取 Intent 的 url。请看下面的代码。

private WebView webView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

webView = findViewById(R.id.external_url_content);
webView.getSettings().setJavaScriptEnabled(true);
}

@Override
protected void onResume() {
super.onResume();
Uri url = getIntent().getData();
if (url != null) {
Log.d("TAG", "URL Foud");
Log.d("TAG", "Url is :" + url);
webView.loadUrl(url.toString());
}
}

list 中的 Intent 过滤器如下

<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>

<intent-filter>
<action android:name="android.intent.action.SEND"/>
<category android:name="android.intent.category.DEFAULT"/>
<data android:mimeType="text/plain"/>
</intent-filter>

<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>

您可以通过here找到有关生命周期 Activity 的更多信息。

关于java - 如何在webview中打开外部链接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48473930/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com