Search code examples
androidc++facebookjava-native-interfacecocos2d-x

JNI C++ call onCreate JAVA - Facebook


I create a game on cocos2dx with C++, now I want to connect it with Facebook, to accomplish this I do it with JAVA and then connect it with JNI. Both things works perfectly, but to connect it with Facebook I need to @override the method onCreate(), but I don't know how to call that method from C++ JNI.

When I move my Facebook code to a new function I need to execute this

Session.openActiveSession(this, true, new Session.StatusCallback() {}

But "this" doesn't exist outside onCreate(). Any ideas?

1-. How to login with Facebook, without calling something on the method onCreate() {}

2-. How to call from JNI C++ to the method onCreate?

My JNI code:

It call FacebookLogin, and works fine, but no with onCreate

if(JniHelper::getStaticMethodInfo(minfo,
                                      "com/company/game/Facebook",
                                      "facebookLogin",
                                      "()V"))
    {
        jstring StringArg1 = minfo.env->NewStringUTF(msg);
        minfo.env->CallStaticVoidMethod(minfo.classID, minfo.methodID);
        minfo.env->DeleteLocalRef(minfo.classID);
    }

Thanks :D


Solution

  • I solved it.

    The problem is that You need to put it on the main Actividty from Cocos2dxActivity and assign onEnter act = this. Where act is Cocos2dxActivity, and in any place you call this, you will place act

    Code:

     public class game extends Cocos2dxActivity{
    
        static Cocos2dxActivity act;
    
    
    protected void onCreate(Bundle savedInstanceState){
            super.onCreate(savedInstanceState);
            System.out.println("Super init");
            act = this;
    }
    
    public static void FacebookLogin() {
            Session.openActiveSession(act, true, new Session.StatusCallback() {
    
                // callback when session changes state
                @Override
                public void call(final Session session2, SessionState state, Exception exception) {
                    if (session2.isOpened()) {
    
                        Request.newMeRequest(session2, new Request.GraphUserCallback() {
    
                              // callback after Graph API response with user object
                              @Override
                              public void onCompleted(GraphUser user, Response response) {
                                if (user != null) {
                                  Log.d("Facebook", "Hello " + user.getName() + "! Id: "+user.getId());
                                  isLoggedIn(); 
                                  String mail = (String)response.getGraphObject().getProperty("email");
                                  if (mail == null) {
                                      mail = "null";
                                  }
                                  userID(user.getId(), mail, user.getName());
                                  session = session2;
                                }
                              }
                        }).executeAsync();
    
                    }
                }
              });
        }
    }