本文整理汇总了C++中JNIEnv类的典型用法代码示例。如果您正苦于以下问题:C++ JNIEnv类的具体用法?C++ JNIEnv怎么用?C++ JNIEnv使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了JNIEnv类的12个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: _open_uri
static int _open_uri(const String& p_uri) {
JNIEnv *env = ThreadAndroid::get_env();
jstring jStr = env->NewStringUTF(p_uri.utf8().get_data());
return env->CallIntMethod(godot_io,_openURI,jStr) ;
}
示例2: _get_data_dir
static String _get_data_dir() {
JNIEnv *env = ThreadAndroid::get_env();
jstring s =(jstring)env->CallObjectMethod(godot_io,_getDataDir);
return String(env->GetStringUTFChars( s, NULL ));
}
示例3: CreateNativeInterface
//--------------------------------------------------------
/// Create Native Interface
//--------------------------------------------------------
void IJavaInterface::CreateNativeInterface(const std::string& instrInterfaceName)
{
if (mpJavaObject == nullptr)
{
JNIEnv* pEnv = JavaInterfaceManager::GetSingletonPtr()->GetJNIEnvironmentPtr();
//get the class type
jclass jClassType = JavaVirtualMachine::Get()->FindClass(instrInterfaceName);
if (jClassType == nullptr)
{
jthrowable exception = pEnv->ExceptionOccurred();
if (exception != nullptr)
{
pEnv->ExceptionDescribe();
pEnv->ExceptionClear();
}
CS_LOG_FATAL("Cannot find native interface class '" + instrInterfaceName + "'");
}
//get the class instance
jmethodID jConstructor = pEnv->GetMethodID(jClassType, "<init>", "()V");
jobject jClassInstance = pEnv->NewObject(jClassType, jConstructor);
if (jClassInstance == nullptr)
{
jthrowable exception = pEnv->ExceptionOccurred();
if (exception != nullptr)
{
pEnv->ExceptionDescribe();
pEnv->ExceptionClear();
}
CS_LOG_FATAL("Cannot create native interface instance '" + instrInterfaceName + "'");
}
//create a global reference to the instance
mpJavaObject = pEnv->NewGlobalRef(jClassInstance);
//delete all the local references
pEnv->DeleteLocalRef(jClassType);
pEnv->DeleteLocalRef(jClassInstance);
}
else
{
CS_LOG_FATAL("Cannot create native interface " + instrInterfaceName + ", as it has already been created!");
}
}
示例4: call
virtual Variant call(const StringName& p_method,const Variant** p_args,int p_argcount,Variant::CallError &r_error) {
//print_line("attempt to call "+String(p_method));
ERR_FAIL_COND_V(!instance,Variant());
r_error.error=Variant::CallError::CALL_OK;
Map<StringName,MethodData >::Element *E=method_map.find(p_method);
if (!E) {
print_line("no exists");
r_error.error=Variant::CallError::CALL_ERROR_INVALID_METHOD;
return Variant();
}
int ac = E->get().argtypes.size();
if (ac<p_argcount) {
print_line("fewargs");
r_error.error=Variant::CallError::CALL_ERROR_TOO_FEW_ARGUMENTS;
r_error.argument=ac;
return Variant();
}
if (ac>p_argcount) {
print_line("manyargs");
r_error.error=Variant::CallError::CALL_ERROR_TOO_MANY_ARGUMENTS;
r_error.argument=ac;
return Variant();
}
for(int i=0;i<p_argcount;i++) {
if (!Variant::can_convert(p_args[i]->get_type(),E->get().argtypes[i])) {
r_error.error=Variant::CallError::CALL_ERROR_INVALID_ARGUMENT;
r_error.argument=i;
r_error.expected=E->get().argtypes[i];
}
}
jvalue *v=NULL;
if (p_argcount) {
v=(jvalue*)alloca( sizeof(jvalue)*p_argcount );
}
JNIEnv *env = ThreadAndroid::get_env();
//print_line("argcount "+String::num(p_argcount));
for(int i=0;i<p_argcount;i++) {
v[i] = _variant_to_jvalue(env, E->get().argtypes[i], p_args[i]);
}
//print_line("calling method!!");
Variant ret;
switch(E->get().ret_type) {
case Variant::NIL: {
//print_line("call void");
env->CallVoidMethodA(instance,E->get().method,v);
} break;
case Variant::BOOL: {
ret = env->CallBooleanMethodA(instance,E->get().method,v);
//print_line("call bool");
} break;
case Variant::INT: {
ret = env->CallIntMethodA(instance,E->get().method,v);
//print_line("call int");
} break;
case Variant::REAL: {
ret = env->CallFloatMethodA(instance,E->get().method,v);
} break;
case Variant::STRING: {
jobject o = env->CallObjectMethodA(instance,E->get().method,v);
String str = env->GetStringUTFChars((jstring)o, NULL );
ret=str;
} break;
case Variant::STRING_ARRAY: {
jobjectArray arr = (jobjectArray)env->CallObjectMethodA(instance,E->get().method,v);
ret = _jobject_to_variant(env, arr);
} break;
//.........这里部分代码省略.........
示例5: GetJNIEnv
void JniOnObserveListener::onObserveCallback(const HeaderOptions headerOptions,
const OCRepresentation& ocRepresentation, const int& eCode, const int& sequenceNumber)
{
jint envRet;
JNIEnv *env = GetJNIEnv(envRet);
if (NULL == env) return;
jobject jListener = env->NewLocalRef(m_jwListener);
if (!jListener)
{
checkExAndRemoveListener(env);
if (JNI_EDETACHED == envRet) g_jvm->DetachCurrentThread();
return;
}
jclass clsL = env->GetObjectClass(jListener);
if (!clsL)
{
checkExAndRemoveListener(env);
if (JNI_EDETACHED == envRet) g_jvm->DetachCurrentThread();
return;
}
if (OC_STACK_OK != eCode && OC_STACK_RESOURCE_CREATED != eCode && OC_STACK_RESOURCE_DELETED != eCode)
{
jobject ex = GetOcException(eCode, "stack error in onObserveCallback");
if (!ex)
{
checkExAndRemoveListener(env);
if (JNI_EDETACHED == envRet) g_jvm->DetachCurrentThread();
return;
}
jmethodID midL = env->GetMethodID(clsL, "onObserveFailed", "(Ljava/lang/Throwable;)V");
if (!midL)
{
checkExAndRemoveListener(env);
if (JNI_EDETACHED == envRet) g_jvm->DetachCurrentThread();
return;
}
env->CallVoidMethod(jListener, midL, ex);
}
else
{
jobject jHeaderOptionList = JniUtils::convertHeaderOptionsVectorToJavaList(env, headerOptions);
if (!jHeaderOptionList)
{
checkExAndRemoveListener(env);
if (JNI_EDETACHED == envRet) g_jvm->DetachCurrentThread();
return;
}
OCRepresentation * rep = new OCRepresentation(ocRepresentation);
jlong handle = reinterpret_cast<jlong>(rep);
jobject jRepresentation = env->NewObject(g_cls_OcRepresentation,
g_mid_OcRepresentation_N_ctor_bool, handle, true);
if (!jRepresentation)
{
delete rep;
checkExAndRemoveListener(env);
if (JNI_EDETACHED == envRet) g_jvm->DetachCurrentThread();
return;
}
jmethodID midL = env->GetMethodID(clsL, "onObserveCompleted",
"(Ljava/util/List;Lorg/iotivity/base/OcRepresentation;I)V");
if (!midL)
{
checkExAndRemoveListener(env);
if (JNI_EDETACHED == envRet) g_jvm->DetachCurrentThread();
return;
}
env->CallVoidMethod(jListener, midL, jHeaderOptionList, jRepresentation,
static_cast<jint>(sequenceNumber));
if (env->ExceptionCheck())
{
LOGE("Java exception is thrown");
delete rep;
jthrowable ex = env->ExceptionOccurred();
env->ExceptionClear();
m_ownerResource->removeOnObserveListener(env, m_jwListener);
env->Throw((jthrowable)ex);
}
}
if (JNI_EDETACHED == envRet) g_jvm->DetachCurrentThread();
}
示例6: onGetMyProfile
void onGetMyProfile(long requestId, bool success, ProfileItem item, string errnum, string errmsg) {
FileLog("httprequest", "Profile.Native::onGetMyProfile( success : %s )", success?"true":"false");
/* turn object to java object here */
JNIEnv* env;
jint iRet = JNI_ERR;
gJavaVM->GetEnv((void**)&env, JNI_VERSION_1_4);
if( env == NULL ) {
iRet = gJavaVM->AttachCurrentThread((JNIEnv **)&env, NULL);
}
jobject jItem = NULL;
JavaItemMap::iterator itr = gJavaItemMap.find(PROFILE_ITEM_CLASS);
if( itr != gJavaItemMap.end() ) {
jclass cls = env->GetObjectClass(itr->second);
if( cls != NULL) {
jmethodID init = env->GetMethodID(cls, "<init>", "("
"Ljava/lang/String;"
"I"
"Ljava/lang/String;"
"Ljava/lang/String;"
"Ljava/lang/String;"
"Ljava/lang/String;"
"I"
"I"
"I"
"I"
"I"
"I"
"I"
"I"
"I"
"I"
"I"
"I"
"I"
"I"
"Ljava/lang/String;"
"Ljava/lang/String;"
"I"
"Ljava/lang/String;"
"Ljava/lang/String;"
"Ljava/lang/String;"
"Ljava/lang/String;"
"Ljava/lang/String;"
"Ljava/lang/String;"
"Ljava/lang/String;"
"Ljava/lang/String;"
"Ljava/lang/String;"
"I"
"I"
"Ljava/lang/String;"
"I"
"Ljava/lang/String;"
"I"
"I"
"Ljava/lang/String;"
"I"
"Ljava/lang/String;"
"I"
"Ljava/util/ArrayList;"
")V"
);
FileLog("httprequest", "Profile.Native::onGetMyProfile( GetMethodID <init> : %p )", init);
if( init != NULL ) {
jstring manid = env->NewStringUTF(item.manid.c_str());
jstring birthday = env->NewStringUTF(item.birthday.c_str());
jstring firstname = env->NewStringUTF(item.firstname.c_str());
jstring lastname = env->NewStringUTF(item.lastname.c_str());
jstring email = env->NewStringUTF(item.email.c_str());
jstring resume = env->NewStringUTF(item.resume.c_str());
jstring resume_content = env->NewStringUTF(item.resume_content.c_str());
jstring address1 = env->NewStringUTF(item.address1.c_str());
jstring address2 = env->NewStringUTF(item.address2.c_str());
jstring city = env->NewStringUTF(item.city.c_str());
jstring province = env->NewStringUTF(item.province.c_str());
jstring zipcode = env->NewStringUTF(item.zipcode.c_str());
jstring telephone = env->NewStringUTF(item.telephone.c_str());
jstring fax = env->NewStringUTF(item.fax.c_str());
jstring alternate_email = env->NewStringUTF(item.alternate_email.c_str());
jstring money = env->NewStringUTF(item.money.c_str());
jstring photoURL = env->NewStringUTF(item.photoURL.c_str());
jstring mobile = env->NewStringUTF(item.mobile.c_str());
jstring landline = env->NewStringUTF(item.landline.c_str());
jstring landline_ac = env->NewStringUTF(item.landline_ac.c_str());
jclass jArrayList = env->FindClass("java/util/ArrayList");
jmethodID jArrayListInit = env->GetMethodID(jArrayList, "<init>", "()V");
//.........这里部分代码省略.........
示例7: sendData
void ScilabVariables::sendData (JavaVM * jvm_, char const* varName, int const* indexes, int indexesSize, unsigned short type, int handlerId){
JNIEnv * curEnv = NULL;
jvm_->AttachCurrentThread(reinterpret_cast<void **>(&curEnv), NULL);
jclass cls = curEnv->FindClass( className().c_str() );
jmethodID voidsendDatajstringjava_lang_StringjintArray_intintjcharcharjintintID = curEnv->GetStaticMethodID(cls, "sendData", "(Ljava/lang/String;[ICI)V" ) ;
if (voidsendDatajstringjava_lang_StringjintArray_intintjcharcharjintintID == NULL) {
throw GiwsException::JniMethodNotFoundException(curEnv, "sendData");
}
jstring varName_ = curEnv->NewStringUTF( varName );
if (varName != NULL && varName_ == NULL)
{
throw GiwsException::JniBadAllocException(curEnv);
}
jintArray indexes_ = curEnv->NewIntArray( indexesSize ) ;
if (indexes_ == NULL)
{
// check that allocation succeed
throw GiwsException::JniBadAllocException(curEnv);
}
curEnv->SetIntArrayRegion( indexes_, 0, indexesSize, (jint*)(indexes) ) ;
curEnv->CallStaticVoidMethod(cls, voidsendDatajstringjava_lang_StringjintArray_intintjcharcharjintintID ,varName_, indexes_, type, handlerId);
curEnv->DeleteLocalRef(varName_);
curEnv->DeleteLocalRef(indexes_);
curEnv->DeleteLocalRef(cls);
if (curEnv->ExceptionCheck()) {
throw GiwsException::JniCallMethodException(curEnv);
}
}
示例8: detect_language
static const TCHAR *
detect_language()
{
#ifdef ANDROID
JNIEnv *env = Java::GetEnv();
Java::Class cls(env, "java/util/Locale");
// Call static function Locale.getDefault() that
// returns the user's default Locale object
jmethodID cid = env->GetStaticMethodID(cls, "getDefault",
"()Ljava/util/Locale;");
assert(cid != NULL);
jobject _obj = env->CallStaticObjectMethod(cls, cid);
if (_obj == NULL)
return NULL;
Java::LocalObject obj(env, _obj);
// Call function Locale.getLanguage() that
// returns a two-letter language string
cid = env->GetMethodID(cls, "getLanguage", "()Ljava/lang/String;");
assert(cid != NULL);
jstring language = (jstring)env->CallObjectMethod(obj, cid);
if (language == NULL)
return NULL;
// Convert the jstring to a char string
const char *language2 = env->GetStringUTFChars(language, NULL);
if (language2 == NULL) {
env->DeleteLocalRef(language);
return NULL;
}
/* generate the resource name */
const char *language3 = language2;
if (strcmp(language3, "pt") == 0)
/* hack */
language3 = "pt_BR";
// Attach .mo to the language identifier
static char language_buffer[16];
snprintf(language_buffer, sizeof(language_buffer), "%s.mo", language3);
// Clean up the memory
env->ReleaseStringUTFChars(language, language2);
env->DeleteLocalRef(language);
// Return e.g. "de.mo"
return language_buffer;
#else /* !ANDROID */
#if defined(_WIN32_WCE)
/* the GetUserDefaultUILanguage() prototype is missing on
mingw32ce, we have to look it up dynamically */
DynamicLibrary coreloc_dll(_T("coredll"));
if (!coreloc_dll.IsDefined()) {
LogStartUp(_T("Language: coredll.dll not found"));
return NULL;
}
typedef LANGID WINAPI (*GetUserDefaultUILanguage_t)();
GetUserDefaultUILanguage_t GetUserDefaultUILanguage =
(GetUserDefaultUILanguage_t)
coreloc_dll.Lookup(_T("GetUserDefaultUILanguage"));
if (GetUserDefaultUILanguage == NULL) {
LogStartUp(_T("Language: GetUserDefaultUILanguage() not available"));
return NULL;
}
#endif
// Retrieve the default user language identifier from the OS
LANGID lang_id = GetUserDefaultUILanguage();
LogStartUp(_T("Language: GetUserDefaultUILanguage()=0x%x"), (int)lang_id);
if (lang_id == 0)
return NULL;
// Try to convert the primary language part of the language identifier
// to a MO file name in the language table
return find_language(PRIMARYLANGID(lang_id));
#endif /* !ANDROID */
}
示例9: getCurrentEnv
// Constructors
ScilabVariables::ScilabVariables(JavaVM * jvm_) {
jmethodID constructObject = NULL ;
jobject localInstance ;
jclass localClass ;
const std::string construct="<init>";
const std::string param="()V";
jvm=jvm_;
JNIEnv * curEnv = getCurrentEnv();
localClass = curEnv->FindClass( this->className().c_str() ) ;
if (localClass == NULL) {
throw GiwsException::JniClassNotFoundException(curEnv, this->className());
}
this->instanceClass = static_cast<jclass>(curEnv->NewGlobalRef(localClass));
/* localClass is not needed anymore */
curEnv->DeleteLocalRef(localClass);
if (this->instanceClass == NULL) {
throw GiwsException::JniObjectCreationException(curEnv, this->className());
}
constructObject = curEnv->GetMethodID( this->instanceClass, construct.c_str() , param.c_str() ) ;
if(constructObject == NULL){
throw GiwsException::JniObjectCreationException(curEnv, this->className());
}
localInstance = curEnv->NewObject( this->instanceClass, constructObject ) ;
if(localInstance == NULL){
throw GiwsException::JniObjectCreationException(curEnv, this->className());
}
this->instance = curEnv->NewGlobalRef(localInstance) ;
if(this->instance == NULL){
throw GiwsException::JniObjectCreationException(curEnv, this->className());
}
/* localInstance not needed anymore */
curEnv->DeleteLocalRef(localInstance);
/* Methods ID set to NULL */
jobjectArray_getAllListenedVariablesID=NULL;
voidsendDatajstringjava_lang_StringjintArray_intintjobjectArray__doubledoublejbooleanbooleanjintintID=NULL;
voidsendDatajstringjava_lang_StringjintArray_intintjobjectArray__doubledoublejobjectArray__doubledoublejbooleanbooleanjintintID=NULL;
voidsendDatajstringjava_lang_StringjintArray_intintjobjectArray__bytebytejbooleanbooleanjintintID=NULL;
voidsendUnsignedDatajstringjava_lang_StringjintArray_intintjobjectArray__bytebytejbooleanbooleanjintintID=NULL;
voidsendDatajstringjava_lang_StringjintArray_intintjobjectArray__shortshortjbooleanbooleanjintintID=NULL;
voidsendUnsignedDatajstringjava_lang_StringjintArray_intintjobjectArray__shortshortjbooleanbooleanjintintID=NULL;
voidsendDatajstringjava_lang_StringjintArray_intintjobjectArray__intintjbooleanbooleanjintintID=NULL;
voidsendUnsignedDatajstringjava_lang_StringjintArray_intintjobjectArray__intintjbooleanbooleanjintintID=NULL;
voidsendDatajstringjava_lang_StringjintArray_intintjobjectArray__longlongjbooleanbooleanjintintID=NULL;
voidsendDatajstringjava_lang_StringjintArray_intintjobjectArray__booleanbooleanjbooleanbooleanjintintID=NULL;
voidsendDatajstringjava_lang_StringjintArray_intintjobjectArray__java_lang_Stringjava_lang_StringjbooleanbooleanjintintID=NULL;
voidsendDatajstringjava_lang_StringjintArray_intintjcharcharjintintID=NULL;
voidcloseListjintArray_intintjintintID=NULL;
voidsendDatajstringjava_lang_StringjintArray_intintjintintjintintjintintjintArray_intintjintArray_intintjdoubleArray_doubledoublejintintID=NULL;
voidsendDatajstringjava_lang_StringjintArray_intintjintintjintintjintintjintArray_intintjintArray_intintjdoubleArray_doubledoublejdoubleArray_doubledoublejintintID=NULL;
voidsendDatajstringjava_lang_StringjintArray_intintjintintjintintjintintjintArray_intintjintArray_intintjintintID=NULL;
}
示例10: SetCapitalisationMethod
//-------------------------------------------
//-------------------------------------------
void TextEntryJavaInterface::SetCapitalisationMethod(CSInput::TextEntry::Capitalisation ineKeyboardCapitalisation)
{
JNIEnv* pEnv = JavaInterfaceManager::GetSingletonPtr()->GetJNIEnvironmentPtr();
s32 dwKeyboardCapitalisation = KeyboardCapitalisationToInteger(ineKeyboardCapitalisation);
pEnv->CallVoidMethod(GetJavaObject(), GetMethodID("setCapitalisationMethod"), dwKeyboardCapitalisation);
}
示例11: SetKeyboardType
//-------------------------------------------
//-------------------------------------------
void TextEntryJavaInterface::SetKeyboardType(CSInput::TextEntry::Type ineKeyboardType)
{
JNIEnv* pEnv = JavaInterfaceManager::GetSingletonPtr()->GetJNIEnvironmentPtr();
s32 dwKeyboardType = KeyboardTypeToInteger(ineKeyboardType);
pEnv->CallVoidMethod(GetJavaObject(), GetMethodID("setKeyboardType"), dwKeyboardType);
}
示例12: Deactivate
//-----------------------------------------------
//-----------------------------------------------
void TextEntryJavaInterface::Deactivate()
{
JNIEnv* pEnv = JavaInterfaceManager::GetSingletonPtr()->GetJNIEnvironmentPtr();
pEnv->CallVoidMethod(GetJavaObject(), GetMethodID("deactivate"));
}