[英]how to open external links in webview
I've created my simple browser app, and when i click on a link for example in whatsapp, android asks what browser should open that, and I select my browser but it doesn't open that link, instead just goes to homescreen. 我已经创建了一个简单的浏览器应用程序,当我单击whatsapp中的链接时,android会询问应使用哪个浏览器打开该链接,然后选择浏览器,但它不会打开该链接,而是转到主屏幕。 what should I do?
我该怎么办?
I have added this in manifest: 我在清单中添加了它:
<category android:name="android.intent.category.DEFAULT" />
<data android:scheme="http" />
<data android:scheme="https" />
And I guess I need to add this code: Uri url = getIntent().getData(); webview1.loadUrl(url.toString());
我想我需要添加以下代码:
Uri url = getIntent().getData(); webview1.loadUrl(url.toString());
Uri url = getIntent().getData(); webview1.loadUrl(url.toString());
but I don't know where to paste it, I tried it onCreate but failed. 但我不知道将其粘贴到哪里,我在onCreate上尝试了它,但是失败了。
You should load the url from which you get the intent in OnResume() method. 您应该在OnResume()方法中加载用于获取意图的URL。 Please have a look at the code below.
请看下面的代码。
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());
}
}
Also the intent-filters in manifest are as follows 清单中的意图过滤器也如下
<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>
You can find more about Lifecycle Activity over here 您可以在此处找到有关生命周期活动的更多信息
use WebView In your layout 在布局中使用WebView
<WebView
android:id="@+id/external_url_content"
android:layout_width="match_parent"
android:layout_height="match_parent" />
In your activity 在您的活动中
WebView browser;
You can use this inside onCreate or functions based on your needs 您可以在onCreate中使用此功能,也可以根据需要使用功能
browser = (WebView) findViewById(R.id.external_url_content);
browser.getSettings().setJavaScriptEnabled(true);
String url_path="https://stackoverflow.com/questions/48473930/how-to-open-external-links-in-webviewsid"
browser.loadUrl(url_path);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.