как открыть другие ссылки в браузере, а не в webview? - PullRequest
0 голосов
/ 09 марта 2020

привет, у меня есть приложение для веб-просмотра android. Я хочу, чтобы мой веб-сайт открывался только в режиме просмотра веб-страниц, и если кто-то щелкнет ссылку, не связанную с моим веб-сайтом, откройте его в браузере. Пример: если мой веб-сайт little.com и кто-то нажимает на ссылку facebook.com или google.com, то в браузере открывается facebbok или google.

androidmainfest. xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.little.example">
    <uses-permission android:name="android.permission.INTERNET" />
    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">

        <!-- The below is for the splash screen and we need no action bar and the default theme -->
        <activity android:name=".homeActivity"
            android:theme="@style/AppTheme.NoActionBar"
            android:noHistory="true">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>


        <activity
            android:configChanges="keyboardHidden|orientation|screenSize"
            android:name=".MainActivity">

        </activity>

    </application>

</manifest>

mainactivity.kt

    package com.little.example
    import android.content.Intent
    import androidx.appcompat.app.AppCompatActivity
    import android.os.Bundle
    import android.os.Handler
    import android.webkit.WebBackForwardList
    import android.webkit.WebResourceRequest
    import android.webkit.WebView
    import android.webkit.WebViewClient
    import com.little.example.R
    import kotlinx.android.synthetic.main.activity_main.*
    import androidx.swiperefreshlayout.widget.SwipeRefreshLayout
    import androidx.core.app.ComponentActivity
    import androidx.core.app.ComponentActivity.ExtraData
    import androidx.core.content.ContextCompat.getSystemService
    import android.icu.lang.UCharacter.GraphemeClusterBreak.T




    class MainActivity : AppCompatActivity() {

        override fun onCreate(savedInstanceState: Bundle?) {
            super.onCreate(savedInstanceState)
            setContentView(R.layout.activity_main)
            webview.settings.javaScriptEnabled = true

            webview.loadUrl("https://little.com")
             webview.webViewClient = MyWebViewClient()



    }

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

    private inner class MyWebViewClient : WebViewClient() {

        override fun shouldOverrideUrlLoading(view: WebView, url: String): Boolean {
            if (!url.contains("little.com")) {//for example
                val intent = Intent(Intent.ACTION_VIEW, Uri.parse(url))
                startActivity(intent)
                return true
            }

            view.loadUrl(url)
            return false

        }}

}

Ответы [ 3 ]

2 голосов
/ 09 марта 2020

вы можете обрабатывать каждый URL, когда вы sh просто настраиваете свое веб-представление через настроенный WebViewClient

mWebView.webViewClient = MyWebViewClient()

}

private inner class MyWebViewClient : WebViewClient() {

    override fun shouldOverrideUrlLoading(view: WebView, url: String): Boolean {
        if (!url.contains("little.com")) {//for example
            val intent = Intent(Intent.ACTION_VIEW, Uri.parse(url))
            startActivity(intent)
            return true
        }

        mWebView.loadUrl(url)
        return false
0 голосов
/ 09 марта 2020

с помощью пользовательских вкладок в AndroidXX

CustomTabsIntent.Builder builder = new CustomTabsIntent.Builder();
            CustomTabsIntent customTabsIntent = builder.build();
            customTabsIntent.launchUrl(context,Uri.parse("http://www.google.com"));
0 голосов
/ 09 марта 2020
Intent browserIntent = new Intent(Intent.ACTION_VIEW, 
Uri.parse("http://www.google.com"));
startActivity(browserIntent);

вы можете использовать этот код для открытия страницы в браузере

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...