Launching a URL link from Cocos2d-x on android?

Thanks … I found the solution …
I have to write the function this way:

@ public static void DisplayToast(final String url) {
System.out.println(url);
m2.runOnUiThread(new Runnable() {
public void run() {
Toast.makeText(m2,url,Toast.LENGTH_LONG).show();
}
});
}@

Thanks a lot Guys :slight_smile: helped me at right time thank you soo much

I followed 5 steps.
I did the same. I was successfully got compiled my UrlJni files.
I also declare public static openURL(String s) method in org/cocos2dx/lib/Cocos2dxActivity.

But it gives me error and getting crashed :

java.lang.NoSuchMethodError: no static method with name=‘openURL’ signature=‘(Ljava/lang/String;)V’ in class Lorg/cocos2dx/lib/Cocos2dxActivity;

please help me :frowning:

Hi Victor!

make sure you have ‘openURL’ static method on your Cocos2dActivity. or kindly post your Cocos2dxActivity codes here. :smiley:

Hi,

its giving me an error

cocos2d-2.1beta3-x-2.1.0/supermom2/proj.android/jni/…/…/Classes/MainScene.cpp:764: undefined reference to `openURLJNI’

Pls help

Thanks

I have made it more simple:

Header File

#ifndef CC_HELPER_H
#define CC_HELPER_H

class CCHelper
{
public:
static void OpeUri(const char* pszUri);
};
#endif

CCP File:

#include “CCHelper.h”
#include “cocos2d.h”

#if(CC_TARGET_PLATFORM CC_PLATFORM_ANDROID)
#include “…/…/cocos2dx/platform/android/jni/JniHelper.h”
#include “…/…/cocos2dx/platform/CCCommon.h”
#endif

USING_NS_CC;

void CCHelper::OpeUri(const char* pszUri)
{
#if(CC_TARGET_PLATFORM CC_PLATFORM_WIN32)
{
ShellExecuteA(NULL, “open”, pszUri, NULL, NULL, SW_SHOWNORMAL);
}
#elif(CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
{
JniMethodInfo minfo;

if(JniHelper::getStaticMethodInfo(minfo, “com/util/Helper”, “openUri”, “(Ljava/lang/String;)V”))
{
jstring url = minfo.env~~>NewStringUTF;
minfo.env~~>CallStaticVoidMethod(minfo.classID, minfo.methodID, url);
minfo.env~~>DeleteLocalRef;
minfo.env~~>DeleteLocalRef(minfo.classID);
}
}
#endif
}

Java File:

package com.util;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;

public class Helper
{
static Activity mActivity;
public Helper(Activity activity)
{
mActivity = activity;
}

public static void openUri(String uri)
{
Intent i = new Intent(Intent.ACTION_VIEW);
i.setData(Uri.parse(uri));
mActivity.startActivity(i);
}
}

Using in your Activity:

package com.mycompany.mygame;

import org.cocos2dx.lib.Cocos2dxActivity;
import android.os.Bundle;
import com.util.Helper;
public class MyGame extends Cocos2dxActivity{

protected Helper mHelper;
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
mHelper = new Helper(this);
}

static {
System.loadLibrary(“game”);
}
}

Usage in your game:

CCHelper::OpeUri("http://www.google.com");

@Vikas Patidar May u explain more detail? I don’t know which portion of code belongs to which class or add in to which class. Thank you for sharing.

KS Lam wrote:

`Vikas Patidar May u explain more detail? I don’t know which portion of code belongs to which class or add in to which class. Thank you for sharing.

You need to put CCHelper.h and CCHelper.ccp inside your class folder.
Then in eclipse Android Project you need to create a package com.util and put Helper.Java file there.

Then open Android.mk file and add reference of CCHelper.cpp for compilation.

You just need to paste below given line of code:

*` …/…/Classes/CCHelper.cpp

After this you need to initialize your helper class in your GameActivity.
I have attached GameActivity.java file for reference.

Now you can simply use static method to open url from your cocos2dx code:

CCHelper::OpenUrl("http://www.google.co.in");

I have attached all source files in a zip file. Hope this will Help everyone.

@Alan Ide

Check out this message. It might helpful for you:

http://www.cocos2d-x.org/boards/6/topics/3769?r=30206#message-30206

I think you should never modify the cocos2d-x code to get your applications working.

A better way is to do it like this.

Add this line to any of your .cpp files that you wish to call java code from.

#include "platform/android/jni/JniHelper.h"

Then you can call methods from you own java classes like this.

from .cpp

    JniMethodInfo t;
    if (JniHelper::getStaticMethodInfo(t, "com/mypackage/myapp/MyActity"
                    ,"myStaticMethod"
                    ,"()V"))
    {
        CCLOG("[MyScene::init] CallStaticVoidMethod");
        t.env->CallStaticVoidMethod(t.classID,t.methodID);
        CCLOG("[MyScene::init] CallStaticVoidMethod DONE!");
    } else
        CCLOG("[MyScene::init] CallStaticVoidMethod CAN'T DO!");

from .java

package com.mypackage.myapp

// Lots of imports!

public class MyActity extends Cocos2dxActivity {

// Lots of code

    public static void myStaticMethod() {
        Log.d(LOG_TAG, "myStaticMethod()");
    }

// Lots of code

}

Logcat Output

D/cocos2d-x debug info(18742): [MyScene::init] CallStaticVoidMethod
D/MyApp(18742): myStaticMethod()
D/cocos2d-x debug info(18742): [MyScene::init] CallStaticVoidMethod DONE!

hi… i follow above all steps but it’s work on device,its run on visual studio but not in android device and no error and no exception… why…? please give me some suggestion asa possible. (hi vikas i will use all your source file but can’t work on device and no exception)

hi….
thanks vikas patidar… in my project Url Launching is done.