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



我为我的英语感到抱歉。

我在重定向到谷歌结果的子页面、短信到WebView Android应用程序时遇到了问题。

示例:包含地址的页面https://siteadress.pl/category在Google结果中,打开WebView应用程序并显示主页(https://siteadress.pl/)外观图片

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

链接(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
}
}

}

感谢帮助:(

解决方案是,您需要首先处理应用程序链接,获得正确的链接,然后将该链接加载到您的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

最新更新