Last active
August 29, 2015 14:17
-
-
Save JonghyuKim/a75411f62e738f3c2e84 to your computer and use it in GitHub Desktop.
[android]javascriptInterface
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class WebViewTestActivity extends ActionBarActivity { | |
private final Handler handler = new Handler(); | |
private WebView webView; | |
private class HyuJSIntreface { | |
@JavascriptInterface | |
public void JStoAndroidSendMessage(final String arg) { // must be final | |
handler.post(new Runnable() { | |
@Override | |
public void run() { | |
webView.setVisibility(View.INVISIBLE); | |
Intent intent = new Intent(); | |
intent.putExtra("data", arg); | |
setResult(0, intent); | |
finish(); | |
} | |
}); | |
} | |
} | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_main_activity2); | |
webView = (WebView) findViewById(R.id.webView); | |
webView.getSettings().setJavaScriptEnabled(true); | |
webView.addJavascriptInterface(new HyuJSIntreface(), "HyusJS"); | |
Intent intent = getIntent(); | |
String targetURL = intent.getStringExtra("url"); | |
if (targetURL.equals("")) { | |
targetURL = "file:///android_asset/javapage.html"; | |
} | |
// CookieSyncManager cookieSyncManager = CookieSyncManager.createInstance(this); | |
// CookieManager cookieManager = CookieManager.getInstance(); | |
// cookieManager.setAcceptCookie(true); | |
// cookieManager.removeSessionCookie(); | |
// cookieSyncManager.sync(); | |
//version 21 | |
// cookieManager.removeAllCookies(new ValueCallback<Boolean>() { | |
// @Override | |
// public void onReceiveValue(Boolean value) { | |
// Log.d("hyu","ret = " + value); | |
// } | |
// }); | |
// cookieManager.flush(); | |
webView.loadUrl(targetURL); | |
webView.setWebViewClient(new WebViewClient() { | |
@Override | |
public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) { | |
Log.d("hyu",error.toString()); | |
handler.proceed(); // ssl error pass | |
} | |
public boolean shouldOverrideUrlLoading(WebView view, String url) { | |
view.loadUrl(url); | |
return true;//Application use url | |
} | |
}); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment