Hi all,
how can I check the existence of some application on Android? How can I decide, is the Facebook app installed on the user’s device?
I can open our company facebook page with the cc.sys.OpenURL
using the fb://page/xxxxxxxxxxx command, and it works fine if the Facebook app is installed, but nothing happens when it is not installed. In that case I could open the page with the default browser, but the cc.sys.OpenURL has no return value and it does not raise an exception to catch. What can I do?
Best regards,
Zsolt
2 Likes
I don’t know much about cocos creator, but in Cocos2d-x i have to use jni that call java function from cpp.
Thanks for the answer! I have no experience with jni or java, can you provide some sample code please?
Best regards
Zsolt
try adding this in AppActivity:
Activity activity;
and init activity in onCreate(){activity = this;}
then:
public static boolean isAppInstalled(String packageName) {
PackageManager pm = activity.getPackageManager();
try {
pm.getApplicationInfo(packageName, 0);
return true;
}
catch (NameNotFoundException e) {
return false;
}
}
let’s say your package is com.pzsolt.mygame
in cpp:
bool isAppInstalled(const char* packagename)
{
//will error in windows editor, so add #if android
return JniHelper::callStaticBooleanMethod("com/pzsolt/mygame/AppActivity", "isAppInstalled", packagename);
}
dont forget to include JniHelper, in cocos2d/cocos/platform/android/jni
2 Likes
Thanks, I’ll try it soon!
in cocos creator for this , really no need to jni and cpp to call java
just need use jsb to call a function in java …
read this from documentation :
http://cocos2d-x.org/docs/editors_and_tools/creator-chapters/advanced-topics/java-reflection/index.html
Hi all,
I just want to share the final and working solution. Based on @sevent7’s instructions I modified the AppActivity.java in Android Studio as follows:
private static AppActivity app = null;
public static boolean isFacebookInstalled() {
try{
ApplicationInfo info = app.getPackageManager().getApplicationInfo("com.facebook.katana", 0 );
return true;
} catch( PackageManager.NameNotFoundException e ){
return false;
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
SDKWrapper.getInstance().init(this);
app = this;
}
At the top I had to add the following imports:
import android.content.pm.PackageManager;
import android.content.pm.ApplicationInfo;
And finally I use the following line in my game source:
var result = jsb.reflection.callStaticMethod("org/cocos2dx/javascript/AppActivity", "isFacebookInstalled", "()Z");
It works like a charm. And if the facebook app is installed I can open our facebook page in the native application, if not I can open it in a browser. Ta-taaaaam!
Many thanks for all of your help!
Best regards,
Zsolt
1 Like
What does katana
mean? Is this the name of the version?
What if the user has a newer version?
I don’t know why it is “katana”, but it is the name of the facebook app on Android.
It is unlikely that they change this ID.
True, the name of the package is constant, if they need a new version then they will just update the manifest.
Where was my mind when I asked?.. Nevermind.
Thanks for the answer!
1 Like