bool JniNative::registerNativeMethods() { JNINativeMethod methods[] { {"notifyMsg", "(III)V", (void*)notifyMsg}, {"setDirectBuffer","(Ljava/lang/Object;I)V",(void*)setDirectBuffer} }; const char *classname = "an/qt/useJar/ExtendsQtNative"; jclass clazz; QAndroidJniEnvironment env; QAndroidJniObject javaClass(classname); clazz = env->GetObjectClass(javaClass.object<jobject>()); // QDBG << "find ExtendsQtNative - " << clazz; bool result = false; if (clazz) { jint ret = env->RegisterNatives(clazz, methods, sizeof(methods) / sizeof(methods[0])); env->DeleteLocalRef(clazz); // QDBG << "RegisterNatives return - " << ret; result = ret >= 0; } if (env->ExceptionCheck()) env->ExceptionClear(); return result; }
static bool registerNativeMethods() { JNINativeMethod methods[] { {"OnImageCaptured", "(ILjava/lang/String;)V", (void*)onImageCaptured} }; const char *classname = "an/qt/imageProcessor/ImageCaptureNative"; jclass clazz; QAndroidJniEnvironment env; QAndroidJniObject javaClass(classname); clazz = env->GetObjectClass(javaClass.object<jobject>()); //clazz = env->FindClass(classname); qDebug() << "find ImageCaptureNative - " << clazz; bool result = false; if(clazz) { jint ret = env->RegisterNatives(clazz, methods, sizeof(methods) / sizeof(methods[0])); env->DeleteLocalRef(clazz); qDebug() << "RegisterNatives return - " << ret; result = ret >= 0; } if(env->ExceptionCheck()) env->ExceptionClear(); return result; }
void QQmlAndroidScrollView::onRegisterNativeMethods(jobject listener) { JNINativeMethod methods[] {{"onScrollChanged", "(JII)V", reinterpret_cast<void *>(onScrollChanged)}}; QAndroidJniEnvironment env; jclass cls = env->GetObjectClass(listener); env->RegisterNatives(cls, methods, sizeof(methods) / sizeof(methods[0])); env->DeleteLocalRef(cls); }
bool aseman_android_loclis_registerNativeMethods() { JNINativeMethod methods[] {{"_locationListened", "(DDDLjava/lang/String;)V", reinterpret_cast<void *>(locationListened)}}; QAndroidJniObject javaClass("land/aseman/android/extra/AsemanLocationListener"); QAndroidJniEnvironment env; jclass objectClass = env->GetObjectClass(javaClass.object<jobject>()); env->RegisterNatives(objectClass, methods, sizeof(methods) / sizeof(methods[0])); env->DeleteLocalRef(objectClass); return true; }
float ScreenValues::retrieveDensity() { #ifdef Q_OS_ANDROID QAndroidJniEnvironment env; env->PushLocalFrame(9); QAndroidJniObject activity = QAndroidJniObject::callStaticObjectMethod("org/qtproject/qt5/android/QtNative", "activity", "()Landroid/app/Activity;"); jclass activityClass = env->GetObjectClass(activity.object<jobject>()); jmethodID mIDGetResources = env->GetMethodID(activityClass, "getResources", "()Landroid/content/res/Resources;"); jobject resources = env->CallObjectMethod(activity.object<jobject>(), mIDGetResources); jclass resourcesClass = env->GetObjectClass(resources); jmethodID mIDGetDisplayMetrics = env->GetMethodID(resourcesClass, "getDisplayMetrics", "()Landroid/util/DisplayMetrics;"); jobject displayMetrics = env->CallObjectMethod(resources, mIDGetDisplayMetrics); jclass displayMetricsClass = env->GetObjectClass(displayMetrics); jfieldID fIDDensityDpi = env->GetFieldID(displayMetricsClass, "density", "F"); jfloat densityDpi = env->GetFloatField(displayMetrics, fIDDensityDpi); float result = (float)densityDpi; env->PopLocalFrame(NULL); return result; #else return QGuiApplication::primaryScreen()->physicalDotsPerInch(); #endif }
bool QBluetoothSocketPrivate::fallBackConnect(QAndroidJniObject uuid, int channel) { qCWarning(QT_BT_ANDROID) << "Falling back to workaround."; QAndroidJniEnvironment env; jclass remoteDeviceClazz = env->GetObjectClass(remoteDevice.object()); jmethodID getClassMethod = env->GetMethodID(remoteDeviceClazz, "getClass", "()Ljava/lang/Class;"); if (!getClassMethod) { qCWarning(QT_BT_ANDROID) << "BluetoothDevice.getClass method could not be found."; return false; } QAndroidJniObject remoteDeviceClass = QAndroidJniObject(env->CallObjectMethod(remoteDevice.object(), getClassMethod)); if (!remoteDeviceClass.isValid()) { qCWarning(QT_BT_ANDROID) << "Could not invoke BluetoothDevice.getClass."; return false; } jclass classClass = env->FindClass("java/lang/Class"); jclass integerClass = env->FindClass("java/lang/Integer"); jfieldID integerType = env->GetStaticFieldID(integerClass, "TYPE", "Ljava/lang/Class;"); jobject integerObject = env->GetStaticObjectField(integerClass, integerType); if (!integerObject) { qCWarning(QT_BT_ANDROID) << "Could not get Integer.TYPE"; return false; } jobjectArray paramTypes = env->NewObjectArray(1, classClass, integerObject); if (!paramTypes) { qCWarning(QT_BT_ANDROID) << "Could not create new Class[]{Integer.TYPE}"; return false; } QAndroidJniObject parcelUuid("android/os/ParcelUuid", "(Ljava/util/UUID;)V", uuid.object()); if (parcelUuid.isValid()) { jint socketChannel = remoteDevice.callMethod<jint>("getServiceChannel", "(Landroid/os/ParcelUuid;)I", parcelUuid.object()); if (env->ExceptionCheck()) { env->ExceptionDescribe(); env->ExceptionClear(); } if (socketChannel == remoteDevice.getStaticField<jint>("android/bluetooth/BluetoothDevice", "ERROR")) { qCWarning(QT_BT_ANDROID) << "Cannot determine RFCOMM service channel."; } else { qCWarning(QT_BT_ANDROID) << "Using found rfcomm channel" << socketChannel; channel = socketChannel; } } QAndroidJniObject method = remoteDeviceClass.callObjectMethod( "getMethod", "(Ljava/lang/String;[Ljava/lang/Class;)Ljava/lang/reflect/Method;", QAndroidJniObject::fromString(QLatin1String("createRfcommSocket")).object<jstring>(), paramTypes); if (!method.isValid() || env->ExceptionCheck()) { qCWarning(QT_BT_ANDROID) << "Could not invoke getMethod"; if (env->ExceptionCheck()) { env->ExceptionDescribe(); env->ExceptionClear(); } return false; } jclass methodClass = env->GetObjectClass(method.object()); jmethodID invokeMethodId = env->GetMethodID( methodClass, "invoke", "(Ljava/lang/Object;[Ljava/lang/Object;)Ljava/lang/Object;"); if (!invokeMethodId) { qCWarning(QT_BT_ANDROID) << "Could not invoke method."; return false; } jmethodID valueOfMethodId = env->GetStaticMethodID(integerClass, "valueOf", "(I)Ljava/lang/Integer;"); jclass objectClass = env->FindClass("java/lang/Object"); jobjectArray invokeParams = env->NewObjectArray(1, objectClass, env->CallStaticObjectMethod(integerClass, valueOfMethodId, channel)); jobject invokeResult = env->CallObjectMethod(method.object(), invokeMethodId, remoteDevice.object(), invokeParams); if (!invokeResult) { qCWarning(QT_BT_ANDROID) << "Invoke Resulted with error."; if (env->ExceptionCheck()) { env->ExceptionDescribe(); env->ExceptionClear(); } return false; } socketObject = QAndroidJniObject(invokeResult); socketObject.callMethod<void>("connect"); if (env->ExceptionCheck()) { env->ExceptionDescribe(); env->ExceptionClear(); qCWarning(QT_BT_ANDROID) << "Socket connect via workaround failed."; return false; } qCWarning(QT_BT_ANDROID) << "Workaround invoked."; return true; }