簡體   English   中英

從搜索結果google.com打開應用,總是指向Android的WebView首頁

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

我為我的英語道歉。

我在重定向到 Google 結果的子頁面、SMS 到 WebView Android 應用程序時遇到問題。

示例:Google 結果中地址為https://siteadress.pl/category的頁面打開 WebView 應用程序並顯示主頁 ( https://siteadress.pl/ ) LOOK PICTURE

示例:Google 結果中包含確切產品https://siteadress.pl/shop/productxyz 的頁面也會打開 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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM