简体   繁体   English

如何将android本机代码与Qt Quick项目集成?

[英]How to integrate android native code with Qt Quick project?

I am trying to get wifi name connected to my mobile using QAndroidJniObject.我正在尝试使用 QAndroidJniObject 将 wifi 名称连接到我的手机。

java file: java文件:

package org.qtproject.example;
import android.net.NetworkInfo.DetailedState;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.app.Activity;
import android.content.Context;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;



public class QtAndroidToastJava extends QtActivity
{



    public static String getWifiName(Context context) {
        WifiManager manager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
        if (manager.isWifiEnabled()) {
           WifiInfo wifiInfo = manager.getConnectionInfo();
           if (wifiInfo != null) {
              DetailedState state = WifiInfo.getDetailedStateOf(wifiInfo.getSupplicantState());
              if (state == DetailedState.CONNECTED || state == DetailedState.OBTAINING_IPADDR) {
                  return wifiInfo.getSSID();
              }
           }
        }
        return null;
    }
}

My cpp code is我的 cpp 代码是

void WIFICLASS::updateAndroidNotification()
{

qDebug()<<"******************************************8";

auto returnString = QAndroidJniObject::callStaticMethod <jstring>("org/qtproject/example/QtAndroidToastJava",
                                             "getWifiName","(V;)Ljava/lang/String");

// //  QString user = juser.toString();
//   qDebug()<<"ANSWER"<<user;

 qDebug()<<returnString;

}

After trying to build this I am getting this errors: 23: error: undefined reference to '_jstring* QAndroidJniObject::callStaticMethod<_jstring*>(char const*, char const*, char const*, ...) '在尝试构建这个之后,我得到了这个错误: 23: error: undefined reference to '_jstring* QAndroidJniObject::callStaticMethod<_jstring*>(char const*, char const*, char const*, ...) '

How can I solve this issue?我该如何解决这个问题?

What is the correct way to do this?这样做的正确方法是什么?

There are two things wrong here:这里有两件事不对:

1.) The message signature you are passing in C++ is wrong. 1.) 您在 C++ 中传递的消息签名是错误的。 It should be:它应该是:

"(Landroid/content/Context;)Ljava/lang/String;"

Mind the ;介意的; at the end of each class name!在每个班级名称的末尾! It is always L<classname>;它总是L<classname>; ! Also, you have to always exactly match the method as declared in java.此外,您必须始终完全匹配 java 中声明的方法。 Multiple parameters do not need to be seperated.多个参数不需要分开。 If you have eg a method void test(int a, int b) , the signature would be (II)V .例如,如果您有一个方法void test(int a, int b) ,则签名将是(II)V

2.) The method you are calling is an object method, which means you must use QAndroidJniObject::callStaticObjectMethod 2.) 你调用的方法是一个对象方法,这意味着你必须使用QAndroidJniObject::callStaticObjectMethod

auto res = QAndroidJniObject::callStaticObjectMethod("org/qtproject/example/QtAndroidToastJava",
                                                     "getWifiName",
                                                     "(Landroid/content/Context;)Ljava/lang/String;",
                                                     QtAndroid::androidContext().object());

That method returns you an QAndroidJniObject and you can call QAndroidJniObject::toString() to convert the result to a string.该方法返回一个QAndroidJniObject ,您可以调用QAndroidJniObject::toString()将结果转换为字符串。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Qt Quick Android:如何集成无障碍服务 - Qt Quick Android : How to integrate an Accessibility Service 如何将Qt库集成到Android本机项目中 - How to integrate Qt library inside Android native project 如何将OpenCV集成到Qt Creator Android项目中 - How to integrate OpenCV into Qt Creator Android project 如何在Android上的本机联系人应用程序中将您的应用程序集成到QUICK CONTACT中? - How to integrate your app in QUICK CONTACT on the native contact app on android? 如何在Android上访问Qt5.2 Quick 2项目的陀螺仪? - How to access gyroscope form Qt5.2 Quick 2 project on Android? 将android项目集成到React Native中 - Integrate android project into React Native 如何在Xamarin Android项目上集成WebRTC Android本机库? - How to integrate webrtc android native library on xamarin android project? 如何将ReactJS代码与新的React Native项目集成在一起? - How to integrate ReactJS code with a new React Native project? 如何在本地android项目中集成libcurl并在Windows上使用ndk进行构建? - How to integrate libcurl in native android project and build using ndk on windows? 如何将 Unity 应用程序集成到原生 android 工作室项目中 - How to integrate Unity app into a native android studio project
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM