**FrameLayout 으로 구성하여 addview 해줘야 함..
import androidx.appcompat.app.AppCompatActivity;
import android.Manifest;
import android.annotation.SuppressLint;
import android.annotation.TargetApi;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.ActivityNotFoundException;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.graphics.Bitmap;
import android.net.Uri;
import android.net.http.SslError;
import android.os.Build;
import android.os.Bundle;
import android.os.Message;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.JsResult;
import android.webkit.PermissionRequest;
import android.webkit.SslErrorHandler;
import android.webkit.ValueCallback;
import android.webkit.WebChromeClient;
import android.webkit.WebResourceError;
import android.webkit.WebResourceRequest;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.FrameLayout;
import org.json.JSONException;
import org.json.JSONObject;
import java.net.URISyntaxException;
import java.util.List;
import im.delight.android.webview.AdvancedWebView;
public class MainActivity extends AppCompatActivity {
private WebView mWebView;
FrameLayout root;
ValueCallback<Uri> mUploadMessage;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
root = (FrameLayout) findViewById(R.id.root);
mWebView = (WebView) findViewById(R.id.webview);
// mWebView.setListener(this, this);
//mWebView.setMixedContentAllowed(false);
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.getSettings().setDomStorageEnabled(true);
mWebView.getSettings().setSupportMultipleWindows(true);
mWebView.getSettings().setDatabaseEnabled(true);
mWebView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
mWebView.getSettings().setBuiltInZoomControls(true);
this.mWebView.setWebChromeClient(new MyWebChromeClient());
this.mWebView.setWebViewClient(new MyWebViewClient(getApplicationContext()));
mWebView.loadUrl("https://www.xxx.yyyy/");
}
public class MyWebChromeClient extends WebChromeClient {
@Override
public void onProgressChanged(WebView view, int newProgress) {
super.onProgressChanged(view, newProgress);
}
@Override
public boolean onCreateWindow(WebView view, boolean isDialog, boolean isUserGesture, Message resultMsg) {
WebView newWebView = new WebView(MainActivity.this);
newWebView.getSettings().setJavaScriptEnabled(true);
newWebView.setWebChromeClient(new WebChromeClient() {
@Override
public void onCloseWindow(WebView window) {
window.setVisibility(View.GONE);
window.destroy();
super.onCloseWindow(window);
}
});
WebView.WebViewTransport transport = (WebView.WebViewTransport) resultMsg.obj;
transport.setWebView(newWebView);
resultMsg.sendToTarget();
if (!view.getUrl().equals("https://xxx.yyyy/")) {
root.addView(newWebView);
}
return true;
}
@Override
public boolean onJsConfirm(WebView view, String url, String message, final JsResult result) {
//return super.onJsConfirm(view, url, message, result);
new AlertDialog.Builder(view.getContext())
.setTitle("")
.setMessage(message)
.setPositiveButton(android.R.string.ok,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
result.confirm();
}
})
.setNegativeButton(android.R.string.cancel,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
result.cancel();
}
})
.create()
.show();
return true;
}
@Override
public void onPermissionRequest(PermissionRequest request) {
request.grant(request.getResources());
}
@Override
public boolean onShowFileChooser(WebView webView, ValueCallback filePathCallback, FileChooserParams fileChooserParams) {
mFilePathCallback = filePathCallback;
Log.d("myLog", "onShowFileChooser!@!!" + filePathCallback);
return true;
}
// For 4.1 <= Android Version < 5.0
@Deprecated
public void openFileChooser(ValueCallback<Uri> uploadFile, String acceptType, String capture) {
Log.d("myLog", "openFileChooser!@!!" + uploadFile);
mUploadMessage = uploadFile;
Intent i = new Intent(Intent.ACTION_GET_CONTENT);
i.addCategory(Intent.CATEGORY_OPENABLE);
i.setType("image/*");
Intent chooserIntent = Intent.createChooser(i, "Image Chooser");
// chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, new Parcelable[]{cameraIntent});
startActivityForResult(chooserIntent, FILECHOOSER_NORMAL_REQ_CODE);
// getPhoto();
}
}
final int FILECHOOSER_NORMAL_REQ_CODE = 1111;
public class MyWebViewClient extends WebViewClient {
private Context mApplicationContext = null;
public MyWebViewClient(Context _applicationContext) {
mApplicationContext = _applicationContext;
}
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
super.shouldOverrideUrlLoading(view, url);
// 전화번호 처리
if (url.startsWith("tel:")) {
startActivity(new Intent(Intent.ACTION_DIAL, Uri.parse(url)));
return true;
}
if (url.startsWith("sms:")) {
startActivity(new Intent(Intent.ACTION_SENDTO, Uri.parse(url)));
return true;
}
if (url.startsWith("mailto:")) {
startActivity(new Intent(Intent.ACTION_SENDTO, Uri.parse(url)));
return true;
}
if (url.startsWith("https://m.facebook.com")) {
Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
startActivity(browserIntent);
return true;
}
// intent 처리 추가. 카카오링크 등
if (url.startsWith("intent:")) {
try {
Intent intent = Intent.parseUri(url, Intent.URI_INTENT_SCHEME);
Intent existPackage = getPackageManager().getLaunchIntentForPackage(intent.getPackage());
if (existPackage != null) {
startActivity(intent);
} else {
Intent marketIntent = new Intent(Intent.ACTION_VIEW);
marketIntent.setData(Uri.parse("market://details?id=" + intent.getPackage()));
startActivity(marketIntent);
}
} catch (Exception e) {
}
return true;
}
if (url.contains("http://market.android.com") ||
url.contains("http://m.ahnlab.com/kr/site/download") ||
url.endsWith(".apk")) {
return urlSchemeProc(view, url);
}else{
view.loadUrl(url);
}
return true;
}
@Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
Log.d("myLog", "onPageStarted " + url);
}
@Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
Log.d("myLog", "onPageFinished " + url);
lastUrl = url;
}
@Override
public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error) {
super.onReceivedError(view, request, error);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
onReceivedError(error.getErrorCode(), String.valueOf(error.getDescription()));
}
}
@Override
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
super.onReceivedError(view, errorCode, description, failingUrl);
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) {
onReceivedError(errorCode, description);
}
}
private void onReceivedError(int errorCode, String description) {
switch (errorCode) {
case WebViewClient.ERROR_TIMEOUT: //연결 시간 초과
case WebViewClient.ERROR_CONNECT: //서버로 연결 실패
//case WebViewClient.ERROR_UNKNOWN: // 일반 오류
case WebViewClient.ERROR_FILE_NOT_FOUND: //404
case WebViewClient.ERROR_HOST_LOOKUP:
case WebViewClient.ERROR_UNSUPPORTED_AUTH_SCHEME:
case WebViewClient.ERROR_AUTHENTICATION:
case WebViewClient.ERROR_PROXY_AUTHENTICATION:
case WebViewClient.ERROR_IO:
case WebViewClient.ERROR_REDIRECT_LOOP:
case WebViewClient.ERROR_UNSUPPORTED_SCHEME:
case WebViewClient.ERROR_FAILED_SSL_HANDSHAKE:
case WebViewClient.ERROR_BAD_URL:
case WebViewClient.ERROR_FILE:
case WebViewClient.ERROR_TOO_MANY_REQUESTS:
case WebViewClient.ERROR_UNSAFE_RESOURCE:
break;
}
}
}
@Override
public void onBackPressed() {
// Check if the WebView can go back in its history
if (mWebView.canGoBack()) {
// If yes, go back in WebView history
mWebView.goBack();
} else {
// If no, proceed with the default behavior
super.onBackPressed();
}
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
Log.d("myLog", "onActivityResult, requestCode:" + requestCode + ", resultCode:" + resultCode + ", data:" + data);
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
case FILECHOOSER_NORMAL_REQ_CODE:
try {
Uri result;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { //5.0이상일때 처리
if (data == null) {
result = mCameraImagePath;
mFilePathCallback.onReceiveValue(new Uri[]{result});
} else {
result = data.getData();
mFilePathCallback.onReceiveValue(new Uri[]{result});
}
} else {
updatePhotos();
if (data == null) {
mUploadMessage.onReceiveValue(null);
result = mCameraImagePath;
Log.d("myLog", "onActivityResult, data null : " + mCameraImagePath);
mUploadMessage.onReceiveValue(result);
} else {
result = data.getData();
Log.d("myLog", "onActivityResult, data.getData() :" + mCameraImagePath);
mUploadMessage.onReceiveValue(result);
}
}
} catch (Exception e) {
Log.d("myLog", "Exception " + e.toString());
}
break;
}
}
String lastUrl = "";
public ValueCallback<Uri[]> mFilePathCallback;
private void updatePhotos() {
Intent intent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
intent.setData(mCameraImagePath);
sendBroadcast(intent);
}
Uri mCameraImagePath;
private boolean urlSchemeProc(WebView view, String url) {
if (url != null &&
(
url.contains("cloudpay")
|| url.contains("hanaansim")
|| url.contains("citispayapp")
|| url.contains("citicardapp")
|| url.contains("mvaccine")
|| url.contains("com.TouchEn.mVaccine.webs")
|| url.contains("market://")
|| url.contains("smartpay")
|| url.contains("com.ahnlab.v3mobileplus")
|| url.contains("droidxantivirus")
|| url.contains("v3mobile")
|| url.endsWith(".apk")
|| url.contains("market://")
|| url.contains("ansimclick")
|| url.contains("market://details?id=com.shcard.smartpay")
|| url.contains("shinhan-sr-ansimclick://")
|| url.contains("http://m.ahnlab.com/kr/site/download")
|| url.contains("com.lotte.lottesmartpay")
|| url.startsWith("lottesmartpay://")
|| url.contains("http://market.android.com")
|| url.contains("vguard")
|| url.contains("smhyundaiansimclick://")
|| url.contains("smshinhanansimclick://")
|| url.contains("smshinhancardusim://")
|| url.contains("smartwall://")
|| url.contains("appfree://")
|| url.startsWith("kb-acp://")
|| url.startsWith("intent://")
|| url.startsWith("ahnlabv3mobileplus")
|| url.contains("smhyundaiansimclick")
|| url.contains("smshinhanansimclick")
|| url.contains("shinhan-sr-ansimclick")
|| url.contains("vguardstart")
|| url.contains("vguardend")
|| url.contains("droidx3host")
|| url.contains("mpocket.online.ansimclick")
|| url.contains("hdcardappcardansimclick")
|| url.contains("nhappcardansimclick")
|| url.contains("nonghyupcardansimclick")
|| url.contains("tswansimclick")
|| url.contains("payco")
|| url.contains("samsungpay")
//2017.07.31 하나 은행 추가 내용,
//하나카드 요청 내용
//① 안드로이드 : "bhcs://cardpotal.hanaskcard.com"
//② 마켓 연결 및 패키지명 : market://details?id=com.hanaskcard.rocomo.potal
|| url.contains("hhcs") || url.contains("bhcs")
)) {
try {
Intent intent = null;
try {
intent = Intent.parseUri(url, Intent.URI_INTENT_SCHEME);
Log.i("myLog", "intent getScheme +++===>" + intent.getScheme());
Log.i("myLog", "intent getDataString +++===>" + intent.getDataString());
} catch (URISyntaxException ex) {
Log.e("Browser", "Bad URI " + url + ":" + ex.getMessage());
return false;
}
//chrome 버젼 방식 : 2014.01 추가
if (url.startsWith("intent")) { // chrome 버젼 방식
// 앱설치 체크를 합니다.
if (getPackageManager().resolveActivity(intent, 0) == null) {
String packagename = intent.getPackage();
if (packagename != null) {
Uri uri = Uri.parse("market://search?q=pname:" + packagename);
intent = new Intent(Intent.ACTION_VIEW, uri);
startActivity(intent);
return true;
}
}
Uri uri = Uri.parse(intent.getDataString());
intent = new Intent(Intent.ACTION_VIEW, uri);
startActivity(intent);
} else { // 구 방식
// 2017-08-04 하나카드 추가 내용 적용
if (url.startsWith("bhcs")) {
String packageName = "com.hanaskcard.rocomo.potal";
String appUrl = "bhcs://cardpotal.hanaskcard.com";
String marKet = "market://details?id=com.hanaskcard.rocomo.potal";
PackageManager pm = getPackageManager();
PackageInfo info = null;
try {
info = pm.getPackageInfo(packageName, PackageManager.GET_ACTIVITIES);
} catch (Exception e) {
return false;
}
try {
if (info != null) {
Uri uri;
if(url.contains(appUrl)){
uri = Uri.parse(url);
}else{
uri = Uri.parse(appUrl);
}
Intent hanaIntent = new Intent(Intent.ACTION_VIEW, uri);
intent.addCategory(Intent.CATEGORY_BROWSABLE);
startActivity(hanaIntent);
}else{
Uri uri = Uri.parse(marKet);
Intent hanaIntent = new Intent(Intent.ACTION_VIEW, uri);
intent.addCategory(Intent.CATEGORY_BROWSABLE);
startActivity(hanaIntent);
}
} catch (Exception e) {
return false;
}
}else{
Uri uri = Uri.parse(url);
intent = new Intent(Intent.ACTION_VIEW, uri);
startActivity(intent);
}
}
} catch (ActivityNotFoundException e) {
Log.e("error ===>", e.getMessage());
e.printStackTrace();
if (url.startsWith("vguardend://")) {
return true;
}else{
return false;
}
}
} else {
view.loadUrl(url);
return false;
}
return true;
}
}
'안드로이드' 카테고리의 다른 글
카카오내비 길찾기 자바 (0) | 2024.03.28 |
---|---|
android 타임아웃 주고 현재위치 찾기 (0) | 2024.02.28 |
안드로이드 다른앱 위에 그리기(노티알림) (0) | 2024.01.23 |
WorkerManager android java (0) | 2024.01.17 |
안드로이드 문자 , 카카오톡 알림 캐치 (0) | 2024.01.16 |