Android 如何使webveiw应用程序打开特定链接?

Android 如何使webveiw应用程序打开特定链接?,android,web,webview,hyperlink,Android,Web,Webview,Hyperlink,我正在构建一个android webview应用程序。应用程序必须在单击时打开某些链接。我正在为一家校报制作应用程序,他们经常在twitter上发布他们网站的链接,如果这些链接在我的应用程序中打开,我会很高兴 类似于点击Twitter.com时Twitter应用程序的打开方式。有没有一种快速的方法可以打开我的应用程序中的特定链接?我是android应用开发新手,所以请具体说明。谢谢 主要活动: package com.________.app; import android.annotatio

我正在构建一个android webview应用程序。应用程序必须在单击时打开某些链接。我正在为一家校报制作应用程序,他们经常在twitter上发布他们网站的链接,如果这些链接在我的应用程序中打开,我会很高兴

类似于点击Twitter.com时Twitter应用程序的打开方式。有没有一种快速的方法可以打开我的应用程序中的特定链接?我是android应用开发新手,所以请具体说明。谢谢

主要活动:

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>

您必须将以下行添加到
AndroidManifest.xml
文件中的活动
intent filter

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


要获得完整答案,请查看此代码。

我尝试了此代码,但我的应用程序启动时没有使用该代码。我将我的AndroidManifest.xml放在这个页面上,以便更好地说明它已经做了什么。替换或添加该代码使应用程序无法启动。好的,我得到了它,因此它打开了链接。我添加了一个新的活动和意图过滤器。但当我通过链接打开它时,应用程序打开并崩溃。