简体   繁体   English

从搜索结果google.com打开应用,总是指向Android的WebView首页

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

I apologize for my English.我为我的英语道歉。

I have a problem with redirecting to a subpage of Google results, SMS to WebView Android applications.我在重定向到 Google 结果的子页面、SMS 到 WebView Android 应用程序时遇到问题。

Example : The page with the address 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/category的页面打开 WebView 应用程序并显示主页 ( https://siteadress.pl/ ) LOOK PICTURE

Example : A page with the exact product https://siteadress.pl/shop/productxyz in Google results also opens the WebView application and shows the homepage.示例:Google 结果中包含确切产品https://siteadress.pl/shop/productxyz 的页面也会打开 WebView 应用程序并显示主页。 Why?为什么?

The link ( https://siteadress.pl/shop/productxyz ) from the sms message also opens the WebView application and shows the main page.短信中的链接 ( https://siteadress.pl/shop/productxyz ) 也会打开 WebView 应用程序并显示主页。

I want to point to the exact page of the application in WebView, not the homepage.我想指向 WebView 中应用程序的确切页面,而不是主页。 :( :(

My AndroidManifest.xml我的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>

My MainActivity.xml我的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

        }

    }


}

Thanks for help :)感谢帮助 :)

The solution is you need to handle app link first, get the correct link and then load that link into your WeView:解决方案是您需要先处理应用程序链接,获取正确的链接,然后将该链接加载到您的 WeView 中:

Uri data = getIntent().getData();

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

Done!完毕! This version in "Kotlin" work: “Kotlin”中的这个版本工作:

val url = intent.data.toString()

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

Thanks for help :) ufff感谢您的帮助:) ufff

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM