Home  >  Article  >  Database  >  cocos2dx代码调用本地浏览器步骤详解

cocos2dx代码调用本地浏览器步骤详解

WBOY
WBOYOriginal
2016-06-07 15:24:311395browse

1.找到ndk路径/sources/cocos2dx/platform/android/CCApplication.h添加方法 void openURL(const char* pszUrl); 2找到ndk路径/sources/cocos2dx/platform/android/CCApplication.cpp 实现方法 void CCApplication::openURL(const char* pszUrl) { JniMethod

1.找到ndk路径/sources/cocos2dx/platform/android/CCApplication.h添加方法

void openURL(const char* pszUrl);

2找到ndk路径/sources/cocos2dx/platform/android/CCApplication.cpp实现方法

void CCApplication::openURL(const char* pszUrl)
{
JniMethodInfo minfo;
if (JniHelper::getStaticMethodInfo(minfo,
"org/cocos2dx/lib/Cocos2dxActivity",  //src路径下的文件
"openURL", //Cocos2dxActivity文件的方法(下面会添加)
"(Ljava/lang/String;)V"))                      //openURL()方法的参数类型Object,返回值类型void
{
jstring StringArg1 = minfo.env->NewStringUTF(pszUrl);
minfo.env->CallStaticVoidMethod(minfo.classID, minfo.methodID, StringArg1);
minfo.env->DeleteLocalRef(StringArg1);
minfo.env->DeleteLocalRef(minfo.classID);
}
}

3.找到Cocos2dxActivity.java 导入命名空间

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

4.Cocos2dxActivity.java中添加 

private static Activity wu= null;

修改onCreate()为

protected void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
sContext = this;
    this.mHandler = new Cocos2dxHandler(this);
    wu = this;
    this.init();
Cocos2dxHelper.init(this, this);
}

添加方法

  //实现浏览器模块的调用
   public static void openURL(String url) 
     { 
           Intent intent = new Intent(Intent.ACTION_VIEW);  
           intent .setData(Uri.parse(url));
           wu.startActivity(i);
     }

在想调用的地方实现调用

CCApplication::sharedApplication()->openURL("http://www.mcttqp.com/Download/djmxd.apk");

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn