3

In my game, which is done for both Android and IOS using cocos2dx, I have to show video(for Android). I am planning to show it in Dialog(on top of game view). Problem is that, I don't have any Activity referenced to show Dialog(as Dialogs can only be shown in Activities). Even though, In cocos2dx lib folder, there is a Cocos2dxActivity but I am not getting how to make use of it. From C++ code, I am calling a static method from Java class as below

void LMJNICommunicator::showVideo()
{
     LOGD("initialiseDatabase inside LMJNICommunicator");

     jmethodID methodID = 0;
     JNIEnv *pEnv = 0;
     pEnv = getJNIEnv();
     jclass ret = pEnv->FindClass("com/mobinius/lostmonstersclass/LMDatabaseDataManager");
     methodID = pEnv->GetStaticMethodID(ret, "showVideo", "()V");

     if (! methodID)
     {
          LOGD("Failed to find static method id of %s", "showVideo");
          return;
     }

     pEnv->CallStaticVoidMethod(ret,methodID);
     pEnv->DeleteLocalRef(ret);

}

Static method(which is in normal Java class) which I am calling from C++ code

Class LMDatabaseDataManager {

    public static void showVideo() {

         Dialog dialog = new Dialog(Cocos2dxActivity.getInstance());
         dialog.show();
        // getting Can't create handler inside thread that has not called Looper.prepare() error
    }
}

I tried to make use of Handler like this but did not get result(got same error in that post). Also tried to get a static Context like this.

So, is my way correct? If not correct, please suggest a way how can I implement the same. Thanks.

Edit:

Finally got answer for this. Earlier I tried running on UI thread with Application static context as in this link but did not get... with Cocos2dxActivity activity instance I got it.

Class LMDatabaseDataManager {

    public static void showVideo() {        

    Cocos2dxActivity.getInstance().runOnUiThread(new Runnable() {

        @Override
        public void run() {
            // TODO Auto-generated method stub
            Dialog dialog = new Dialog(Cocos2dxActivity.getInstance());
            dialog.show();            
        }
    });

    }
}
Community
  • 1
  • 1
Braj
  • 2,164
  • 2
  • 26
  • 44
  • Are you not overlaying your video on an Activity? – ajacian81 Nov 15 '12 at 13:01
  • no actually... as I explained in question, thr is a activity named Cocos2dxActivity in which GLSurfaceView n all are running... I dont know whether i can make use of it or not – Braj Nov 15 '12 at 13:13

1 Answers1

6

Try adding the appropriate lines in Cocos2dxActivity:

public class Cocos2dxActivity extends Activity {
    private static Cocos2dxActivity instance = null;
   @Override public void onCreate(Bundle b) {
     ...
     this.instance = this;
     }

     public static Cocos2dxActivity getInstance() {
        return instance;
     }



}

When you want to create your dialog:

if (Cocos2dxActivity.getInstance() != null)  {
    AlertDialog dialog = new AlertDialog(Cocos2dxActivity.getInstance());
    // rest of your dialog code goes here
}
ajacian81
  • 7,419
  • 9
  • 51
  • 64
  • thanks for d response... once i tried above by getting Application global context as shown in my question second link but did not get result... anyway, i wl try your answer n let u know – Braj Nov 15 '12 at 13:43
  • I tried your answer... its throwing error "cant create handler inside a thread that has not called Looper.prepare()" – Braj Nov 16 '12 at 06:42
  • Thank u very much for your time... I solved the problem. Earlier I tried running on UI thread with Application static context as in question second link but did not get... with Cocos2dActivity activity instance I got it... – Braj Nov 16 '12 at 07:32
  • I think that **this.instance = null;** should be called from within the **onDestroy()** method. Otherwise a memory leak will occur once the activity is dead, because a reference to a dead activity will be kept. More info: [link](http://android-developers.blogspot.bg/2009/01/avoiding-memory-leaks.html) – ra3o.ra3 Mar 21 '16 at 11:34