深入链接webview中的右侧页面

时间:2017-10-10 08:32:03

标签: android webview deep-linking

我为我们的网站制作了一个android webview应用程序。现在我想添加到应用程序的深层链接,比如当有人点击我们网站的链接时,您可以使用webview应用程序而不是Chrome网络浏览器打开它。我将这些添加到我的清单中:

<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="https"
          android:host="www.example.com" />

现在,当我点击与我们相关的链接时,它会打开webview应用程序,但它没有打开正确的页面,它只是再次启动应用程序。但我想在webview应用程序中直接打开右侧页面。

编辑:

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

    //added
    Intent intent = getIntent();
    String action = intent.getAction();
    Uri data = intent.getData();

但似乎没有使用变量action和data。 我加载了这样的webview(三种语言)。

 //loads the main website
 //sets the matching language
 web2.clearCache(true);
    String loc = Locale.getDefault().toString();
    if (loc.startsWith("de")) {
        web2.loadUrl("https://www.example.de");
    } else if (loc.startsWith("nl")) {
        web2.loadUrl("https://www.example.nl");
    } else {
        web2.loadUrl("https://www.example.com");
    }

2 个答案:

答案 0 :(得分:0)

您必须使用活动通过深层链接打开时收到的意图数据。 Kotlin中的例子:

class WebViewActivity : AppCompatActivity() {

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_webview)
    val uri = intent.data
    webView.loadUrl(uri.toString())
}

答案 1 :(得分:0)

现在我这样做了。这就是解决方案。

Intent intent = getIntent();
Uri data = intent.getData();

String loc = Locale.getDefault().toString();

    if (data==null && loc.startsWith("de")) {
        web2.loadUrl("https://www.example.de");
    } else if (data==null && loc.startsWith("nl")) {
        web2.loadUrl("https://www.example.nl");
    } else if (data==null && !loc.startsWith("de") && !loc.startsWith("nl")){
        web2.loadUrl("https://www.example.com");
    } else {
        web2.loadUrl(data.toString());
    }