android - 如何让 webveiw 应用打开特定链接?
问题描述
我正在构建一个 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>
解决方案
您必须将以下行添加到文件中的活动intent-filter
中AndroidManifest.xml
。
<category android:name="android.intent.category.BROWSABLE" />
<data android:host="www.example.com" android:scheme="http" />
如需完整答案,请查看此答案。
推荐阅读
- python - sqlalchemy.exc.ProgrammingError:您的 SQL 语法有错误;检查与您的 MariaDB 服务器版本相对应的手册
- angular - convert complex/nested json object to excel table
- google-cloud-platform - Google Cloud Platform 创建自定义 IAM 角色并限制对存储桶的访问
- swift - 无法从 TabBarController 访问 UIView 子类 IBOutlets
- reactjs - aws-amplify忘记密码功能,反应js发送短信而不是电子邮件
- php - 如何获取资产文件夹的绝对路径?
- c++ - Base64 编码的字符串太大,C++ 中截断尾随字符
- string - 在 Racket 中,我如何使用一个字符串句子并创建一个包含该句子中每个单词的字符串列表?
- python - 通过比较两个列表删除特定单词
- laravel - 在 localhost 上使用 Apache 的项目的自定义 URL