C++ 调用 java jni.h 的使用

时间:2023-12-22 09:32:32

----------------------------------------------c++---------------------------------------

#include "JniHelper.h"

JniMethodInfo t;

if (JniHelper::getStaticMethodInfo(t,"org/cocos2dx/lib/Cocos2dxActivity", "openUrl", "(Ljava/lang/String;)V")) {
jstring stringArg = t.env->NewStringUTF(_url.c_str());
t.env->CallStaticVoidMethod(t.classID, t.methodID,stringArg);
t.env->DeleteLocalRef(t.classID);
}

调用openUrl 参数为_url

++++++++++++++++++++++++++java+++++++++++++++++++++++++++++

public static void openUrl(String url) {
Uri uri = Uri.parse(url);
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
m_instance.startActivity(intent);
}

++++++++++++++++++++end+++++++++++++++++++++++++++++++++++++++

=================c++=================================

const char* text="";

const char* picture="";

const char* title="";

JniMethodInfo t;

if (JniHelper::getStaticMethodInfo(t,"org/cocos2dx/lib/ShareDialog", "shareText", "(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;)V")) {
jstring stringText = t.env->NewStringUTF(text);
jstring stringPicture = t.env->NewStringUTF(filepath.c_str());
jstring stringTitle = t.env->NewStringUTF(title);
t.env->CallStaticVoidMethod(t.classID, t.methodID,stringText,stringPicture,stringTitle);
t.env->DeleteLocalRef(t.classID);
}

===============java=======================================

public static void shareText(String text,String picture,String title) {
File f = new File(picture);
Uri u = Uri.fromFile(f);

String subject = "share";
int shareTitleId = m_instance.getResources().getIdentifier("share_title","string",m_instance.getPackageName());
if(shareTitleId!=0){
subject = m_instance.getString(shareTitleId);
}

Intent intent=new Intent(Intent.ACTION_SEND);

intent.putExtra(Intent.EXTRA_SUBJECT,title);
intent.putExtra(Intent.EXTRA_STREAM, u);
intent.putExtra(Intent.EXTRA_TEXT,text);
intent.setType("image/*");
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
m_instance.startActivity(Intent.createChooser(intent,subject));
}

======================end=======================