관리 메뉴

웹개발자의 기지개

[안드로이드] javascript 의 window.open , window.close 연동하기 본문

안드로이드

[안드로이드] javascript 의 window.open , window.close 연동하기

http://portfolio.wonpaper.net 2021. 7. 13. 07:38

우리가 웹브라우저에 이미 탑재된 기능이라 지나칠수 있는데, 새창 띄우기 기능인 javascript 의 window.open 함수도 안드로이상에서는 따로 만들어 주어야 한다.

 

아래의 소스중에 WebChromeClient 상속받은 MyWebChromeClient 클래스내에 onCreateWindow 함수가 바로 이놈인데 이를 오바라이딩 해주도록 하자.

 

아래 소스는 window.open 창이 Dialog 형태로 그안에  newView 형태로 열리는데,

필자가 엄청 삽질했던 부분이 open 은 잘되는데, close 가 안되어 한참 애먹었었다.

 

결국, 64라인, 65라인이 핵심이다.

 

                        dialog.dismiss();
                        newWebView.loadUrl("javascript:self.close();");

 

dialog 를 닫고,

javascript:self.close() 를 돌려서, 77라인의 onCloseWindow 함수가 돌아가도록 하는것이다.

 

 

[ MainActivity.java ]

 

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
 
public class MainActivity extends AppCompatActivity {
 
    private static final String TAG = "MainActivity";
    private WebView webView1;
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
 
        webView1 = (WebView) findViewById(R.id.webView1);
        webView1.setWebChromeClient(new MyWebChromeClient()); 
        webView1.setWebViewClient(new WebViewClientClass(getApplicationContext()));
        webView1.loadUrl("http://www.naver.com");
    }
 
    // WebView 에서 url 변경시 그대로 이동되도록
    public class WebViewClientClass extends WebViewClient {
        private String TAG = "WebViewClientClass";
        private Context mApplicationContext = null;
        public WebViewClientClass(Context _applicationContext) {
            mApplicationContext = _applicationContext;
        }
 
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
            Log.d("WebViewClient URL : ", request.getUrl().toString());
            view.loadUrl(request.getUrl().toString());
            return true;
            // return super.shouldOverrideUrlLoading(view, request);
        }
    }
 
    public class MyWebChromeClient extends WebChromeClient {
        private final String TAG = "MyWebChromeClient";
 
        @Override
        public void onProgressChanged(WebView view, int newProgress) {
            super.onProgressChanged(view, newProgress);
            Log.i(TAG,"onProgressChanged(view:"+view.toString()+ ", newProgress:"+newProgress+")");
        }
 
        @Override
        public boolean onCreateWindow(WebView view, boolean isDialog, boolean isUserGesture, Message resultMsg) {
            Log.i(TAG, "window.open 협의가 필요합니다.");
            WebView newWebView = new WebView(view.getContext());
            WebSettings webSettings = newWebView.getSettings();
            WebSettings settings = newWebView.getSettings();
            settings.setJavaScriptEnabled(true);
            settings.setJavaScriptCanOpenWindowsAutomatically(true);
            settings.setSupportMultipleWindows(true);
 
            //final Dialog dialog = new Dialog(view.getContext(),R.style.Theme_DialogFullScreen);
            final Dialog dialog = new Dialog(view.getContext()); dialog.setContentView(newWebView);
            dialog.show();
            dialog.setOnKeyListener(new DialogInterface.OnKeyListener() {
                @Override
                public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
 
                    if (keyCode == KeyEvent.KEYCODE_BACK) {
                        dialog.dismiss();
                        newWebView.loadUrl("javascript:self.close();");
                        return true;
                    } else {
                        return false;
                    }
                }
            });
 
            newWebView.setWebViewClient(new WebViewClientClass(view.getContext()));
            newWebView.setWebChromeClient(new MyWebChromeClient() {
                @Override
                public void onCloseWindow(WebView window) {
                    dialog.dismiss();
window.destroy();
                }
            });
 
            WebView.WebViewTransport transport = (WebView.WebViewTransport) resultMsg.obj;
            transport.setWebView(newWebView);
            resultMsg.sendToTarget();
 
            return true;
        }
 
        @Override
        public void onCloseWindow(WebView window) {
            Log.i(getClass().getName(), "onCloseWindow");
            window.setVisibility(View.GONE);
            window.destroy();
            //mWebViewSub=null;
            super.onCloseWindow(window);
        }
 
        @Override
        public boolean onJsAlert(WebView view, String url, String message, final JsResult result) {
            Log.i(getClass().getName(), "onJsAlert() url:"+url+", message:"+message);
            //return super.onJsAlert(view, url, message, result);
 
            new AlertDialog.Builder(view.getContext())
                    .setTitle("")
                    .setMessage(message)
                    .setPositiveButton(android.R.string.ok,
                            new AlertDialog.OnClickListener() {
                                public void onClick(DialogInterface dialog, int which) {
                                    result.confirm();
                                } })
                    .setCancelable(false)
                    .create()
                    .show();
            return true;
        }
 
 
        @Override
        public boolean onJsConfirm(WebView view, String url, String message, final JsResult result) {
            Log.i(getClass().getName(), "onJsConfirm() url:"+url+", message"+message);
            //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;
        }
    }    
}
cs

 

 

참고 : https://helloit.tistory.com/303

 

 

 

 

 

Comments