★ WebView 통신 Point ★
webView.addJavascripInterface(new AndroidBridge(),"android");
android => window.android
(ANDROID) (JS)
안드로이드에서 @JavascriptInterface로 콜백 받음
xml
<WebView android:id="@+id/address_webview"
android:isScrollContainer="true"
android:layout_width="match_parent"
android:layout_height="match_parent">
</WebView>
AddressFragment
public class AddressFragment extends DialogFragment {
private WebView webView;
private Handler handler;
private OnAddressFragmentInteractionListener mListener;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_address, container, false);
webView = view.findViewById(R.id.address_webview);
init_webView();
handler = new Handler();
return view;
}
public void init_webView() {
webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setDefaultTextEncodingName("UTF-8");
// JavaScript의 window.open 허용
webView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
// JavaScript이벤트에 대응할 함수를 정의 한 클래스를 붙여줌
// 두 번째 파라미터는 사용될 php에도 동일하게 사용해야함
webView.addJavascriptInterface(new AndroidBridge(), "android");
// web client 를 chrome 으로 설정
webView.setWebChromeClient(new WebChromeClient() {
@Override
public void onProgressChanged(WebView view, int progress) {
if (view.getProgress() == 100) {
webView.postDelayed(() -> webView.scrollTo(0, 0), 300);
}
}
});
webView.loadUrl("http://kderp.co.kr/jusoMobilePopup.php");
InputMethodManager immhide = (InputMethodManager) getActivity().getSystemService(Activity.INPUT_METHOD_SERVICE);
immhide.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0);
}
// TODO: Rename method, update argument and hook method into UI event
public void sendAddress(AddressModel address) {
if (mListener != null) {
mListener.onAddressFragmentInteraction(address);
}
}
@Override
public void onAttach(Context context) {
super.onAttach(context);
if (context instanceof OnAddressFragmentInteractionListener) {
mListener = (OnAddressFragmentInteractionListener) context;
} else {
throw new RuntimeException(context.toString()
+ " must implement OnFragmentInteractionListener");
}
}
@Override
public void onDetach() {
super.onDetach();
mListener = null;
}
public interface OnAddressFragmentInteractionListener {
// TODO: Update argument type and name
void onAddressFragmentInteraction(AddressModel model);
}
private class AndroidBridge {
@JavascriptInterface
public void jusoCallBack(final String roadAddrPart1, final String roadAddrPart2, final String addrDetail, final String zipNo) {
handler.post(() -> {
init_webView();
sendAddress(new AddressModel(roadAddrPart1, roadAddrPart2, addrDetail, zipNo));
dismiss();
});
}
}
}
서버 jusoMobilePopup.php 파일 ( UTF-8 인코딩 확인 )
function init() {
var url = location.href;
var confmKey = "U01TX0FVVEgyMDE5MDMxMDExMjAyNjEwODU2Njk=";
var resultType = "4"; // 도로명주소 검색결과 화면 출력내용, 1 : 도로명, 2 : 도로명+지번, 3 : 도로명+상세건물명, 4 : 도로명+지번+상세건물명
var inputYn = "<?=$ADDR['inputYn']?>";
if (inputYn != "Y") {
document.form.confmKey.value = confmKey;
document.form.returnUrl.value = url;
document.form.resultType.value = resultType;
document.form.action = "http://www.juso.go.kr/addrlink/addrMobileLinkUrl.do"; //모바일 웹인 경우, 인터넷망
document.form.submit();
} else {
window.android.jusoCallBack("<?=$ADDR[roadAddrPart1]?>", "<?=$ADDR[roadAddrPart2]?>", "<?=$ADDR[addrDetail]?>", "<?=$ADDR[zipNo]?>");
}
}
도로명 주소 리턴
@Override
public void onAddressFragmentInteraction(AddressModel model) {
binding.editRZipCd.setText(model.getZipNo());
roadAddrPart1 = model.getRoadAddrPart1();
roadAddrPart2 = model.getRoadAddrPart2();
addrDetail = model.getAddrDetail();
binding.editAddress.setText(roadAddrPart1 + ", " + addrDetail + " " + roadAddrPart2);
}
'Android' 카테고리의 다른 글
Room Library (0) | 2019.05.21 |
---|---|
showToast : Toast Custom (0) | 2019.04.19 |
RecyclerView SwipeToDelete (0) | 2019.04.19 |
ScrollView PullToRefreshLayout (0) | 2019.04.19 |
Retrofit 통신 (0) | 2019.04.19 |