【问题标题】:Deep linking the right page inside the webview在 webview 中深度链接正确的页面
【发布时间】:2017-10-10 08:32:03
【问题描述】:

我为我们的网站制作了一个 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。 我像这样加载网络视图(三种语言)。

 //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");
    }

【问题讨论】:

    标签: android webview deep-linking


    【解决方案1】:

    您必须使用您的活动在通过深层链接打开时收到的意图数据。 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())
    }
    

    【讨论】:

    • 就像现在你可以在上面的编辑中看到的那样,我在我的代码中添加了一些东西 (developer.android.com/training/app-links/…) 但它不起作用。
    • 我刚刚尝试使用 adb shell am start -W -a android.intent.action.VIEW -d "example.com" 并且它有效。我得到了意图选择器并启动了我的应用程序而不是 chrome
    • 我必须在哪里添加它?你能给我看看你的例子吗?
    • 但我再次重复我上面写的内容。意图选择器出现,我可以选择我的应用程序,但它正常启动,而不是正确/相关页面。
    【解决方案2】:

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

    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());
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-07-28
      • 2018-01-20
      • 2017-07-08
      • 2017-02-13
      • 2013-06-25
      • 1970-01-01
      相关资源
      最近更新 更多