「Android/webview/非推奨コード」の版間の差分
提供: 初心者エンジニアの簡易メモ
(→警告: [deprecation] WebViewClientのonReceivedError(WebView,int,String,String)は非推奨になりました) |
|||
行32: | 行32: | ||
==警告: [deprecation] WebViewClientのonReceivedError(WebView,int,String,String)は非推奨になりました== | ==警告: [deprecation] WebViewClientのonReceivedError(WebView,int,String,String)は非推奨になりました== | ||
エラーハンドラーを以下のように変更 | エラーハンドラーを以下のように変更 | ||
+ | |||
@Override | @Override | ||
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) { | public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) { | ||
行39: | 行40: | ||
import android.webkit.WebResourceRequest; | import android.webkit.WebResourceRequest; | ||
import android.webkit.WebResourceError; | import android.webkit.WebResourceError; | ||
+ | @TargetApi(Build.VERSION_CODES.M) | ||
@Override | @Override | ||
public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error) { | public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error) { | ||
− | Log.d(TAG, "failingUrl=" + request.getUrl().toString()); | + | if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { |
+ | Log.d(TAG, error.getErrorCode() + " : " + error.getDescription()); | ||
+ | } | ||
+ | if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { | ||
+ | Log.d(TAG, "failingUrl=" + request.getUrl().toString()); | ||
+ | } | ||
+ | } | ||
+ | @SuppressWarnings("deprecation") | ||
+ | @Override | ||
+ | public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) { | ||
+ | Log.d(TAG, "failingUrl=" + failingUrl); | ||
} | } | ||
2018年8月23日 (木) 12:44時点における版
警告: [deprecation] WebViewClientのshouldOverrideUrlLoading(WebView,String)は非推奨になりました
shouldOverrideUrlLoadingはリンクでブラウザを開く関数(falseを返すと新規でブラウザを開かない)
private class CustomWebViewClient extends WebViewClient { @Override public boolean shouldOverrideUrlLoading(WebView view, String url) { return false; } }
↓ Build.VERSION_CODES.N以降専用のアノテーションを追加する
import android.annotation.TargetApi; import android.os.Build; import android.webkit.WebResourceRequest; private class CustomWebViewClient extends WebViewClient { @TargetApi(Build.VERSION_CODES.N) @Override public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) { // view.loadUrl(request.getUrl().toString()); return false; } @SuppressWarnings("deprecation") @Override public boolean shouldOverrideUrlLoading(WebView view, String url) { return false; } }
参考:https://trueman-developer.blogspot.com/2016/12/webviewclientshouldoverrideurlloading.html
警告: [deprecation] WebViewClientのonReceivedError(WebView,int,String,String)は非推奨になりました
エラーハンドラーを以下のように変更
@Override public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) { Log.d(TAG, "failingUrl=" + failingUrl); }
↓
import android.webkit.WebResourceRequest; import android.webkit.WebResourceError; @TargetApi(Build.VERSION_CODES.M) @Override public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { Log.d(TAG, error.getErrorCode() + " : " + error.getDescription()); } if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { Log.d(TAG, "failingUrl=" + request.getUrl().toString()); } } @SuppressWarnings("deprecation") @Override public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) { Log.d(TAG, "failingUrl=" + failingUrl); }
参考:https://qiita.com/eno314/items/3c7dd6ef7524765f4665
警告: [deprecation] WebChromeClientのonJsTimeout()は非推奨になりました
private class CustomWebChromeClient extends WebChromeClient { @Override public boolean onJsTimeout() { return false; } }
↓
private class CustomWebChromeClient extends WebChromeClient { @SuppressWarnings("deprecation") @Override public boolean onJsTimeout() { return false; } }