Как сделать так, чтобы приложение webveiw открывало ссылки c? - PullRequest
0 голосов
/ 15 января 2020

Я создаю android приложение для веб-просмотра. Приложение должно открывать определенные ссылки при нажатии. Я делаю приложение для школьной газеты, и они часто публикуют в Твиттере ссылки на свой веб-сайт. Мне бы хотелось, чтобы эти ссылки открывались в моем приложении.

Подобно тому, как приложение Twitter открывается при нажав на twitter.com. Есть ли быстрый способ сделать указанную ссылку c открытой в моем приложении? Я новичок в разработке приложений android, так что уточните c. Спасибо!

MainActivity:

package com.________.app;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.os.Bundle;
import android.webkit.CookieManager;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;

public class MainActivity extends Activity {

    private WebView mWebView;

    @SuppressLint("SetJavaScriptEnabled")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mWebView = findViewById(R.id.activity_main_webview);
        mWebView.setWebViewClient(new WebViewClient());
        WebSettings webSettings = mWebView.getSettings();
        webSettings.setJavaScriptEnabled(true);
        webSettings.setDomStorageEnabled(true);
        CookieManager.getInstance().setAcceptThirdPartyCookies(mWebView, true);
        mWebView.setWebViewClient(new MyWebViewClient());
        mWebView.loadUrl("https://website.com");

        // LOCAL RESOURCE
        // mWebView.loadUrl("file:///android_asset/index.html");
    }

    @Override
    public void onBackPressed() {
        if(mWebView.canGoBack()) {
            mWebView.goBack();
        } else {
            super.onBackPressed();
        }
    }
}

MyWebViewClient:

package com.________.app;

import android.content.Intent;
import android.net.Uri;
import android.webkit.WebView;
import android.webkit.WebViewClient;

class MyWebViewClient extends WebViewClient {

    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        Uri uri = Uri.parse(url);
        if (uri.getHost() != null && uri.getHost().contains("____________.com")) {
            return false;
        }
        if (uri.getHost() != null && uri.getHost().contains("accounts.google.com")) {
            return false;
        }
        if (uri.getHost() != null && uri.getHost().contains("wordpress.com")) {
            return false;
        }
        if (uri.getHost() != null && uri.getHost().contains(".google.co")) {
            return false;
        }
        if (uri.getHost() != null && uri.getHost().contains(".google.com")) {
            return false;
        }
        if (uri.getHost() != null && uri.getHost().contains("accounts.google.co.in")) {
            return false;
        }
        if (uri.getHost() != null && uri.getHost().contains("www.accounts.google.com")) {
            return false;
        }
        if (uri.getHost() != null && uri.getHost().contains("oauth.googleusercontent.com")) {
            return false;
        }
        if (uri.getHost() != null && uri.getHost().contains("googleapis.com")) {
            return false;
        }
        if (uri.getHost() != null && uri.getHost().contains("content.googleapis.com")) {
            return false;
        }
        if (uri.getHost() != null && uri.getHost().contains("ssl.gstatic.com")) {
            return false;
        }
        if (uri.getHost() != null && uri.getHost().contains("appleid.apple.com")) {
            return false;
        }


        Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
        view.getContext().startActivity(intent);
        return true;
    }
}

AndroidManifest. xml:

<?xml version="1.0" encoding="utf-8"?>
<manifest
    package="com.________.app"
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools">
    <uses-permission android:name="android.permission.INTERNET" />
    <application
        android:allowBackup="true"
        android:fullBackupContent="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme"
        tools:ignore="GoogleAppIndexingWarning">
        <activity
            android:name="com.________.app.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
</manifest>

1 Ответ

0 голосов
/ 15 января 2020

Вы должны добавить следующие строки в свою деятельность intent-filter в AndroidManifest.xml файле.

<category android:name="android.intent.category.BROWSABLE" />
<data android:host="www.example.com" android:scheme="http" />

Чтобы получить полный ответ, взгляните на этот ответ .

...