Here’s what I want in my android app.
I want a button in webview call a method in my java. This method should call facebook sdk’s authorize() function and do SSO/Dialog way of authentication. The access token and the expire token are returned back to webview when i call a javascript method in webview.
Here’s what I’ve created already.
In my onCreate() of activity I’m initializing the webview.
mFB = new Facebook(APP_ID);
wv = (WebView) findViewById(R.id.web_view);
wv.getSettings().setJavaScriptEnabled(true);
wv.addJavascriptInterface(new JSInterface(this), "JAVA");
wv.loadUrl("file:///android_asset/test.html");
The test.html in my assets folder is this –
<script type="text/javascript">
function authorizeFacebook() {
JAVA.authorizeFacebook();
}
function showData(token, expire) {
document.getElementById('result').innerHTML = token + " >>>> " + expire;
}
</script>
The interfacing between JS and Java are working fine. That I’m sure of. My JSInterface is –
public class JSInterface {
public Context mContext;
JSInterface(Context c) {
mContext = c;
}
public void authorizeFacebook() {
Log.e("FB", "authorizeFacebook() interface called");
authorizeFacebookSSO();
}
}
public void authorizeFacebookSSO() {
mFB.authorize(FBCMTestActivity.this, new DialogListener() {
@Override
public void onFacebookError(FacebookError e) {
Log.e("FBAUTH", "FB failed + " + e.getErrorCode());
Toast.makeText(getApplicationContext(), "FBFAIL:" + e.getMessage(), Toast.LENGTH_LONG).show();
}
@Override
public void onError(DialogError e) {
Log.e("FBAUTH", "FB failed + " + e.getMessage());
Toast.makeText(getApplicationContext(), "FBFAIL:" + e.getMessage(), Toast.LENGTH_LONG).show();
}
@Override
public void onComplete(Bundle values) {
Log.e("FBAUTH", "SUCCESS");
Log.e("FBAUTH:", mFB.getAccessToken() + " " + mFB.getAccessExpires());
wv.loadUrl("javascript:showData( '" + mFB.getAccessToken() + "' , '" + mFB.getAccessExpires() + "');");
}
@Override
public void onCancel() {
}
});
}
When I have the facebook app, this works great.
But when there is no facebook app, it should ideally show a Dialog with webview. But it fails and stops at – ‘Loading…’ screen.

It just stays there and doesn’t even crash. There are no logs. After a while I get to either force close it or wait for it. Has anyone faced this issue before?
UPDATE
My onActivityResultCode() –
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
mFB.authorizeCallback(requestCode, resultCode, data);
}
Well I figured this out myself.
When I call a Java function from WebView’s javascript, the function runs in webview’s thread. So making it run on UI thread fixed everything for me 🙂
Hope that helps others.
Here’s the only change that I’ve made in the above code.