本文整理汇总了C++中JavaVM类的典型用法代码示例。如果您正苦于以下问题:C++ JavaVM类的具体用法?C++ JavaVM怎么用?C++ JavaVM使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了JavaVM类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: CCLOG
JNIEnv *Kawaz::OUYA::OuyaController::getJNIEnv() {
JavaVM* jvm = cocos2d::JniHelper::getJavaVM();
if (NULL == jvm) {
CCLOG("Failed to get JNIEnv. JniHelper::getJavaVM() is NULL");
return NULL;
}
JNIEnv *env = NULL;
// get jni environment
jint ret = jvm->GetEnv((void**) &env, JNI_VERSION_1_4);
switch (ret) {
case JNI_OK:
// Success!
return env;
case JNI_EDETACHED:
// Thread not attached
// TODO : If calling AttachCurrentThread() on a native thread
// must call DetachCurrentThread() in future.
// see: http://developer.android.com/guide/practices/design/jni.html
if (jvm->AttachCurrentThread(&env, NULL) < 0) {
CCLOG("Failed to get the environment using AttachCurrentThread()");
return NULL;
} else {
// Success : Attached and obtained JNIEnv!
return env;
}
case JNI_EVERSION:
// Cannot recover from this error
CCLOG("JNI interface version 1.4 not supported");
default:
CCLOG("Failed to get the environment using GetEnv()");
return NULL;
}
}
示例2: createServiceProxy
int createServiceProxy(int id) {
JavaVM *jvm; /* representa uma VM Java */
JNIEnv *env; /* ponteiro para a interface de metodos nativos */
jvm = bftsmartdiversity::jvm;
env = bftsmartdiversity::env;
/* invoke the Main.test method using the JNI */
jclass cls = env->FindClass("bftsmart/diversity/ClientWrapper");
if (cls == NULL) {
std::cout << "ERRO no FindClass diversity" << std::endl;
jvm->DestroyJavaVM();
return 0x8100;
}
jint arg = (jint) id;
jmethodID constru = env->GetMethodID(cls, "<init>", "(I)V");
if (constru == NULL) {
std::cout << "ERRO ao construir wrapper getmid" << std::endl;
jvm->DestroyJavaVM();
return 0x8200;
}
bftsmartdiversity::serviceProxy = env->NewObject(cls, constru, arg);
if (bftsmartdiversity::serviceProxy == NULL) {
std::cout << "ERRO ao construir wrapper int. srvprox" << std::endl;
jvm->DestroyJavaVM();
return 0x8201;
}
bftsmartdiversity::invokeOrderedMethod = env->GetMethodID(cls, "invokeOrdered", "([B)[B");
bftsmartdiversity::invokeUnorderedMethod = env->GetMethodID(cls, "invokeUnordered", "([B)[B");
if (bftsmartdiversity::invokeOrderedMethod == NULL || bftsmartdiversity::invokeUnorderedMethod == NULL) {
std::cout << "ERRO ao obter execute(un) ordered " << std::endl;
jvm->DestroyJavaVM();
return 0x8107;
}
return 0x0;
}
示例3: VMOpenException
JavaVMSingleton::JavaVMSingleton(const char* classPath, const char* javaLibPath)
{
JavaVM *jvms;
jsize nVMs;
if ( JNI_GetCreatedJavaVMs(&jvms, 1, &nVMs) == 0 )
{
if (nVMs == 1)
{
JavaVM *jvm = jvms;
JNIEnv *env;
if( jvm->AttachCurrentThread((void **)&env, NULL) == 0)
{
init(jvm, env);
return;
}
else
throw VMOpenException();
}
}
init(classPath, javaLibPath);
}
示例4: lua_pushnil
jobject LuaJavaBridge::checkVector(lua_State *L)
{
vector<string> strings;
lua_pushnil(L); /* first key */
while (lua_next(L, -2)) {
int index = lua_tonumber(L, -2) - 1;
strings.push_back(checkObj(L));
lua_pop(L, 1);
}
JNIEnv *env = 0;
JavaVM* jvm = cocos2d::JniHelper::getJavaVM();
jint ret = jvm->GetEnv((void**)&env, JNI_VERSION_1_4);
switch (ret) {
case JNI_OK:
break;
case JNI_EDETACHED :
if (jvm->AttachCurrentThread(&env, NULL) < 0)
{
LOGD("Failed to get the environment using AttachCurrentThread()");
return NULL;
}
break;
case JNI_EVERSION :
default :
LOGD("Failed to get the environment using GetEnv()");
return NULL;
}
PSJNIHelper::createVector();
for(vector<string>::iterator it = strings.begin(); it != strings.end(); ++it)
{
PSJNIHelper::pushVectorElement(*it);
}
return PSJNIHelper::getVector();
}
示例5: getScilabJavaVM
void ScilabJavaEnvironmentWrapper::unwraprowstring(int id, const ScilabStringStackAllocator & allocator) const
{
JNIEnv * curEnv = NULL;
JavaVM * vm = getScilabJavaVM();
vm->AttachCurrentThread(reinterpret_cast<void **>(&curEnv), NULL);
jobjectArray res = static_cast<jobjectArray>(curEnv->CallStaticObjectMethod(ScilabJavaObjectClass_, unwrapRowStringID_ , id));
if (curEnv->ExceptionCheck())
{
throw GiwsException::JniCallMethodException(curEnv);
}
jint lenRow = curEnv->GetArrayLength(res);
jboolean isCopy = JNI_FALSE;
char ** addr = new char*[lenRow];
jstring * resString = new jstring[lenRow];
for (jsize i = 0; i < lenRow; i++)
{
resString[i] = reinterpret_cast<jstring>(curEnv->GetObjectArrayElement(res, i));
addr[i] = const_cast<char *>(curEnv->GetStringUTFChars(resString[i], &isCopy));
}
allocator.allocate(1, lenRow, addr);
for (jsize i = 0; i < lenRow; i++)
{
curEnv->ReleaseStringUTFChars(resString[i], addr[i]);
curEnv->DeleteLocalRef(resString[i]);
}
delete[] addr;
delete[] resString;
curEnv->DeleteLocalRef(res);
if (curEnv->ExceptionCheck())
{
throw GiwsException::JniCallMethodException(curEnv);
}
}
示例6: defined
void CLcxSndJni::Destroy()
{
if(0>m_fd)
return;
#if defined(__ANDROID__)
JNIEnv* pEnv = NULL;
JavaVM* pJvm = LCSYS::g_pJavaVM;
jclass clzz = LCSYS::g_JniClzz;
if(NULL == pJvm || 0 > pJvm->AttachCurrentThread(&pEnv, NULL))
{
LOGE("SndJni::Play::Err::AttachCurrentThread::\n");
return;
}
pEnv->CallStaticIntMethod(clzz, Jni_SndDestroy, (jint)m_fd);
#endif
m_fd = -1;
}
示例7: JNI_HELPER_LOGE
JniCallbackHelper::~JniCallbackHelper() {
if (mMethodId != 0) {
if (mEnv->ExceptionCheck()) {
JNI_HELPER_LOGE(
"There was an exception thrown from the Java method!!");
mEnv->ExceptionClear();
}
}
for (std::vector<jobject>::iterator it = mNewJavaObjects.begin();
it != mNewJavaObjects.end(); ++it) {
mEnv->DeleteLocalRef(*it);
}
mEnv->DeleteGlobalRef(mJObject);
if (JNI_EDETACHED == mGetEnvResult) {
JavaVM * vm;
mEnv->GetJavaVM(&vm);
vm->DetachCurrentThread();
}
}
示例8: LOGD
void OpenSLMediaPlayerHQVisualizerJNIBinder::onLeaveInternalPeriodicCaptureThread(
OpenSLMediaPlayerHQVisualizer *visualizer) noexcept
{
LOGD("onLeaveInternalPeriodicCaptureThread()");
JavaVM *vm = jvm_;
if (jvm_attached_) {
// release global references
for (auto &t : jwaveform_data_) {
t.release(env_);
}
for (auto &t : jfft_data_) {
t.release(env_);
}
// detach
(void)vm->DetachCurrentThread();
jvm_attached_ = false;
}
env_ = nullptr;
}
示例9: main
int main(int argc, char** argv) {
JavaVMInitArgs args;
args.version = JNI_VERSION_1_1;
args.nOptions = 1;
args.options = new JavaVMOption[1];
args.options->optionString = (char*)"/home/benland100/Desktop/sjvm/javatest/";
JavaVM *vm;
JNIEnv *env;
JNI_CreateJavaVM(&vm,(void**)&env,(void*)&args);
delete [] args.options;
//jclass system = env->FindClass("java/lang/System");
//jclass printstream = env->FindClass("java/io/PrintStream");
//jfieldID outID = env->GetStaticFieldID(system,"out","Ljava/io/PrintStream;");
//jobject out = env->GetStaticObjectField(system,outID);
//cout << (int)out << '\n';
//jclass mainclass = env->FindClass("smart/Client");
//jmethodID main = env->GetStaticMethodID(mainclass,"main","([Ljava/lang/String;)V");
//env->CallStaticVoidMethod(mainclass,main,env->NewObjectArray(0,env->FindClass("java/lang/String"),0));
//jobject obj = env->AllocObject(exception);//env->NewObject(exception,einit);
//env->CallVoidMethod(obj,einit);
//jclass test = env->FindClass("fields/subclass");
jclass mainclass = env->FindClass("javatest");
jmethodID main = env->GetStaticMethodID(mainclass,"main","([Ljava/lang/String;)V");
try {
env->CallStaticVoidMethod(mainclass,main);
if (env->ExceptionOccurred() == 0) {
cout << "Executing Success\n";
} else {
cout << "Damn, exceptions...\n";
}
} catch ( ... ) {
}
env->DeleteLocalRef(mainclass);
vm->DestroyJavaVM();
return 0;
}
示例10: closeKeyboardIME
int closeKeyboardIME()
{
ANativeActivity *activity = sf::getNativeActivity();
JavaVM* vm = activity->vm;
JNIEnv* env = activity->env;
JavaVMAttachArgs attachargs;
attachargs.version = JNI_VERSION_1_6;
attachargs.name = "NativeThread";
attachargs.group = NULL;
jint res = vm->AttachCurrentThread(&env, &attachargs);
if (res == JNI_ERR)
return EXIT_FAILURE;
jclass natact = env->FindClass("android/app/NativeActivity");
jclass context = env->FindClass("android/content/Context");
jfieldID fid = env->GetStaticFieldID(context, "INPUT_METHOD_SERVICE", "Ljava/lang/String;");
jobject svcstr = env->GetStaticObjectField(context, fid);
jmethodID getss = env->GetMethodID(natact, "getSystemService", "(Ljava/lang/String;)Ljava/lang/Object;");
jobject imm_obj = env->CallObjectMethod(activity->clazz, getss, svcstr);
jclass imm_cls = env->GetObjectClass(imm_obj);
jmethodID toggleSoftInput = env->GetMethodID(imm_cls, "toggleSoftInput", "(II)V");
env->CallVoidMethod(imm_obj, toggleSoftInput, 1, 0);
env->DeleteLocalRef(imm_obj);
env->DeleteLocalRef(imm_cls);
env->DeleteLocalRef(svcstr);
env->DeleteLocalRef(context);
env->DeleteLocalRef(natact);
vm->DetachCurrentThread();
return EXIT_SUCCESS;
}
示例11: show_ads
void show_ads (void)
{
// Get the android application's activity.
ANativeActivity* activity = gEngine.app->activity;
JavaVM* jvm = activity->vm;
JNIEnv* env = NULL;
DTboolean needs_detach = false;
int env_stat = jvm->GetEnv( (void**) &env, JNI_VERSION_1_6);
if (env_stat == JNI_EDETACHED) {
jvm->AttachCurrentThread(&env, 0);
needs_detach = true;
}
ASSERT(env);
jclass c_activity = env->GetObjectClass(activity->clazz);
jmethodID m_show_ad_popup = env->GetMethodID(c_activity, "showAdPopup", "()V");
env->CallVoidMethod(activity->clazz, m_show_ad_popup);
if (needs_detach)
jvm->DetachCurrentThread();
}
示例12: callStringJavaMethod
string callStringJavaMethod(const char* javaMethodName, string param1, string param2, string param3){
ANativeActivity* activity = NULL; JavaVM* jvm = NULL; JNIEnv* env = NULL;
string result;
if(prepareForCallJavaMethod(&activity, &jvm, &env)){
jclass clazz = env->GetObjectClass(activity->clazz);
jmethodID methodID = env->GetMethodID(clazz, javaMethodName, "(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;)Ljava/lang/String;");
jstring jStringParam1 = env->NewStringUTF( param1.c_str() );
jstring jStringParam2 = env->NewStringUTF( param2.c_str() );
jstring jStringParam3 = env->NewStringUTF( param3.c_str() );
if( !jStringParam1 || !jStringParam2 || !jStringParam3 )
{
__android_log_print(ANDROID_LOG_ERROR, "NativeActivityConnector#callStringJavaMethod", "Fallo al pasar string de ndk a java");
return result;
}
jstring jresult = (jstring) env->CallObjectMethod(activity->clazz, methodID, jStringParam1, jStringParam2, jStringParam3);
result = jstringToString(jresult, env);
env->DeleteLocalRef( jStringParam1 );
env->DeleteLocalRef( jStringParam2 );
env->DeleteLocalRef( jStringParam3 );
jvm->DetachCurrentThread();
}
return result;
}
示例13: ofGetJNIEnv
JNIEnv * ofGetJNIEnv() {
JNIEnv *env;
JavaVM * vm = ofGetJavaVMPtr();
if(!vm) {
ofLogError("ofAppAndroidWindow") << "couldn't get java virtual machine";
return NULL;
}
int getEnvStat = vm->GetEnv((void**) &env, JNI_VERSION_1_4);
if (getEnvStat == JNI_EDETACHED) {
if (vm->AttachCurrentThread(&env, NULL) != 0) {
ofLogError("ofAppAndroidWindow") << "couldn't get environment using GetEnv()";
return NULL;
}
} else if (getEnvStat != JNI_OK) {
ofLogError("ofAppAndroidWindow") << "couldn't get environment using GetEnv()";
return NULL;
}
return env;
}
示例14: set_wake_alarm_callout
static bool set_wake_alarm_callout(uint64_t delay_millis, bool should_wake, alarm_cb cb, void *data) {
JNIEnv *env;
JavaVM *vm = AndroidRuntime::getJavaVM();
jint status = vm->GetEnv((void **)&env, JNI_VERSION_1_6);
jboolean ret = JNI_FALSE;
if (status != JNI_OK && status != JNI_EDETACHED) {
ALOGE("%s unable to get environment for JNI call", __func__);
return false;
}
if (status == JNI_EDETACHED && vm->AttachCurrentThread(&env, &sAttachArgs) != 0) {
ALOGE("%s unable to attach thread to VM", __func__);
return false;
}
sAlarmCallback = cb;
sAlarmCallbackData = data;
jboolean jshould_wake = should_wake ? JNI_TRUE : JNI_FALSE;
if (sJniAdapterServiceObj) {
ret = env->CallBooleanMethod(sJniAdapterServiceObj, method_setWakeAlarm, (jlong)delay_millis, jshould_wake);
} else {
ALOGE("JNI ERROR : JNI reference already cleaned : set_wake_alarm_callout", __FUNCTION__);
}
if (!ret) {
sAlarmCallback = NULL;
sAlarmCallbackData = NULL;
}
if (status == JNI_EDETACHED) {
vm->DetachCurrentThread();
}
return !!ret;
}
开发者ID:daddy366,项目名称:packages-Bluetooth,代码行数:37,代码来源:com_android_bluetooth_btservice_AdapterService.cpp
示例15: getPath
std::string Database::getPath(std::string name) {
std::string databasePath;
JNIEnv* env;
JavaVM* vm = engine->app->activity->vm;
vm->AttachCurrentThread(&env, NULL);
jclass clazz = env->GetObjectClass(engine->app->activity->clazz);
jmethodID methodj = env->GetMethodID(clazz, "getDatabasePath", "(Ljava/lang/String;)Ljava/io/File;");
jobject filej = env->CallObjectMethod(engine->app->activity->clazz, methodj, env->NewStringUTF(name.c_str()));
if (filej != NULL) {
clazz = env->GetObjectClass(filej);
methodj = env->GetMethodID(clazz, "getPath", "()Ljava/lang/String;");
jstring jstr = (jstring)env->CallObjectMethod(filej, methodj);
if (jstr != NULL) {
const char* str = env->GetStringUTFChars(jstr, NULL);
databasePath = str;
env->ReleaseStringUTFChars(jstr, str);
}
}
vm->DetachCurrentThread();
return databasePath;
}