如果你看一下开发人员的网站,就会清楚地提到
如果你的应用目标是Android 7.1(API级别25)或更低版本,你可以选择WebView对象不检查URL
Google Safe Browsing's
应用程序清单文件的元素:
<manifest>
<application>
<meta-data android:name="android.webkit.WebView.EnableSafeBrowsing"
android:value="false" />
...
</application>
</manifest>
对于Android 8.0,它明确提到
级别不能使用setSafeBrowsingEnabled(),应更改该值
对于WebView的所有实例。
如果您的目标是Android 8.1(API级别27)或更高版本,则可以通过编程方式定义应用程序如何响应已知威胁:
-
-
你可以让你的应用程序自动执行一个特定的动作,比如
每次它遇到一个
被列为已知威胁。
我的网络活动.java
private WebView mSuperSafeWebView;
private boolean mSafeBrowsingIsInitialized;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
mSuperSafeWebView = new WebView(this);
mSuperSafeWebView.setWebViewClient(new MyWebViewClient());
mSafeBrowsingIsInitialized = false;
mSuperSafeWebView.startSafeBrowsing(this, new ValueCallback<Boolean>() {
@Override
public void onReceiveValue(Boolean success) {
mSafeBrowsingIsInitialized = true;
if (!success) {
Log.e("MY_APP_TAG", "Unable to initialize Safe Browsing!");
}
}
});
}
用于启用或禁用安全浏览。使用以下方法。
mSuperSafeWebView.getSettings().setSafeBrowsingEnabled(true);
MyWebViewClient.java
public class MyWebViewClient extends WebViewClient {
// Automatically go "back to safety" when attempting to load a website that
// Google has identified as a known threat. An instance of WebView calls
// this method only after Safe Browsing is initialized, so there's no
// conditional logic needed here.
@Override
public void onSafeBrowsingHit(WebView view, WebResourceRequest request,
int threatType, SafeBrowsingResponse callback) {
// The "true" argument indicates that your app reports incidents like
// this one to Safe Browsing.
callback.backToSafety(true);
Toast.makeText(view.getContext(), "Unsafe web page blocked.",
Toast.LENGTH_LONG).show();
}
}
如果你想知道
WebView security version by version
.