Call Android Java from Cocos2d-x Lua, open a Facebook page through an app deep link when available, and fall back to the browser URL when necessary.
A product requirement asked the game to open its Facebook page. Calling device.openURL() opened the browser rather than the Facebook application, so I used the Lua-to-Java bridge on Android.
Lua Bridge Call
1 2 3 4 5 6 7 8 9 10 11 12
functionAppInfo.openFacebook(name) if device.platform == "Android"then local signature = "(Ljava/lang/String;)V" local arguments = {name} luaj.callStaticMethod( AppInfo.JAVA_CLASSNAME, "openFacebook", arguments, signature ) end end
The Java method checks whether the Facebook package is installed, attempts an app deep link, and falls back to the HTTPS page. Deep-link formats can change between Facebook app versions, so verify the current supported link format when maintaining this code.