从结果 google.com 打开应用程序,总是指向 WebView 的主页 Android

Opening the application from the results google.com, always directs to the home page of WebView Android

我为我的英语道歉。

我在重定向到 Google 结果的子页面时遇到问题,SMS 到 WebView Android 应用程序。

示例:地址为https://siteadress.pl/category in the Google results opens the WebView application and shows the homepage (https://siteadress.pl/的页面) LOOK PICTURE

示例:在 Google 结果中具有确切产品 https://siteadress.pl/shop/productxyz 的页面也会打开 WebView 应用程序并显示主页。为什么?

短信中的 link (https://siteadress.pl/shop/productxyz) 也会打开 WebView 应用程序并显示主页。

我想在 WebView 中指向应用程序的确切页面,而不是主页。 :(

我的AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
        package="pl.APP">

    <uses-permission android:name="android.permission.INTERNET" />

    <application
            android:allowBackup="true"
            android:icon="@mipmap/ic_launcher"
            android:label="APP"
            android:roundIcon="@mipmap/ic_launcher_round"
            android:supportsRtl="true"
            android:theme="@style/AppTheme">

        <meta-data
                android:name="asset_statements"
                android:resource="@string/asset_statements" />

        <activity
                android:name=".SplashScreen"
                android:launchMode="singleTop"
                android:noHistory="true"
                android:theme="@style/SplashTheme">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>


            <meta-data
                    android:name="android.app.shortcuts"
                    android:resource="@xml/shortcuts" />
        </activity>


        <activity android:name=".ContactActivity" />
        <activity android:name=".CategoryActivity" />



        <activity
                android:name=".MainActivity"
                android:launchMode="singleTop"
                android:screenOrientation="portrait">
            <intent-filter android:autoVerify="true">
                <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="siteadress.pl" />
            </intent-filter>
        </activity>

    </application>

</manifest>

我的MainActivity.xml

package pl.APP

import android.content.ActivityNotFoundException
import android.content.Intent
import android.net.Uri
import android.os.Build
import android.os.Bundle
import android.support.annotation.RequiresApi
import android.support.v7.app.AppCompatActivity
import android.util.Log
import android.webkit.URLUtil
import android.webkit.WebResourceRequest
import android.webkit.WebView
import android.webkit.WebViewClient
import android.widget.Toast
import kotlinx.android.synthetic.main.activity_main.*



class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?)
    {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)


        webView.webViewClient = MyWebViewClient()
        webView.loadUrl("https://siteadress.pl/")
        webView.settings.javaScriptEnabled = true
    }

    override fun onBackPressed() {
        if (webView.canGoBack()) {
            webView.goBack()
        } else {
            super.onBackPressed()
        }
    }

    inner class MyWebViewClient : WebViewClient()
    {
        override fun shouldOverrideUrlLoading(view: WebView?, url: String?): Boolean
        {
            if(URLUtil.isNetworkUrl(url))
            {
                return false
            }
            try
            {
                val shareIntent= Intent()
                shareIntent.action=Intent.ACTION_VIEW
                shareIntent.data= Uri.parse(url)
                startActivity(shareIntent)
            }
            catch(e: ActivityNotFoundException)
            {
                Toast.makeText(this@MainActivity, "Appropriate app not found", Toast.LENGTH_LONG).show()
                Log.e("AndroidRide",e.toString())
            }
            return true
        }
        @RequiresApi(Build.VERSION_CODES.N)
        override fun shouldOverrideUrlLoading(view: WebView?, request: WebResourceRequest?): Boolean
        {
            val url=request?.url.toString()
            if(URLUtil.isNetworkUrl(url))
            {
                return false
            }
            try
            {
                val shareIntent= Intent()
                shareIntent.action=Intent.ACTION_VIEW
                shareIntent.data= Uri.parse(url)
                startActivity(shareIntent)
            }
            catch(e: ActivityNotFoundException)
            {
                Toast.makeText(this@MainActivity, "Appropriate app not found", Toast.LENGTH_LONG).show()
                Log.e("AndroidRide",e.toString())
            }
            return true

        }

    }


}

感谢您的帮助:)

解决方案是您需要先处理应用程序 link,获取正确的 link,然后将 link 加载到您的 WeView 中:

Uri data = getIntent().getData();

if (data != null) {
    String url = data.toString();
    webView.loadUrl(url);
} else {
    webView.loadUrl("https://siteadress.pl/");
}

完成!此版本在 "Kotlin" 工作:

val url = intent.data.toString()

if (URLUtil.isValidUrl(url)) {
    webView.loadUrl(url)
} else {
    webView.loadUrl("https://siteadress.pl/")
}

感谢帮助:) ufff