Skip to content

Instantly share code, notes, and snippets.

@JonghyuKim
Last active August 29, 2015 14:17
Show Gist options
  • Save JonghyuKim/a75411f62e738f3c2e84 to your computer and use it in GitHub Desktop.
Save JonghyuKim/a75411f62e738f3c2e84 to your computer and use it in GitHub Desktop.
[android]javascriptInterface
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