tips: Qt5 call android static function

 1.      ?.pro file add:

QT += qml quick androidextras
ANDROID_PACKAGE_SOURCE_DIR = $$PWD/android-sources

OTHER_FILES += \
    android-sources/AndroidManifest.xml \
    android-sources/src/org/qtproject/qmhp/AndroidCodeMhp.java



2.      gen AndroidManifest.xml file; add premission 

AndroidManifest.xml file in "Xml Source" window , make  a change by hand.

 default activity name is "qt.project.qt5.example.bindings.qtActivity" cause qt don't know your java class name, should be change by hand. 

following is a example:


AndroidManifest.xml:

<?xmlversion="1.0"?>

<manifest package="org.qtproject.qmhp" xmlns:android="http://schemas.android.com/apk/res/android" android:versionName="1.0" android:versionCode="1" android:installLocation="auto">
    <application android:hardwareAccelerated="true" android:name="org.qtproject.qt5.android.bindings.QtApplication" android:label="Qmhp" android:icon="@drawable/icon">
        <activity android:configChanges="orientation|uiMode|screenLayout|screenSize|smallestScreenSize|locale|fontScale|keyboard|keyboardHidden|navigation" android:name="org.qtproject.qmhp.AndroidCodeMhp" android:label="@string/app_name" android:screenOrientation="unspecified" android:launchMode="singleTop">


package:  org.qtproject.qmhp

java File nameAndroidCodeMhp.java

java Class : AndroidCodeMhp


3.      qt5 can only call static java function for now.


example java code(get wifi list):

package org.qtproject.qmhp;

import org.qtproject.qt5.android.bindings.QtActivity;
import org.qtproject.qt5.android.bindings.QtApplication;

import java.lang.String;
import java.util.List;
import android.util.Log;
import android.content.Context;

import android.net.wifi.WifiManager;
import android.net.wifi.WifiInfo;
import android.net.wifi.ScanResult;
public class AndroidCodeMhp extends QtActivity
{
    private static AndroidCodeMhp m_instance;
    private static WifiManager m_wifiManager = null;
    private static WifiInfo m_wifiInfo = null;
    private static List<ScanResult>  m_wifiList;

    	public AndroidCodeMhp()
    {
        m_instance = this;
    }
// WIFI
    public static String getWifi(int nu) {
        if (m_wifiManager == null) {
            m_wifiManager = (WifiManager) m_instance.getSystemService(Context.WIFI_SERVICE);
        }
        m_wifiManager.setWifiEnabled(true);
        m_wifiInfo = m_wifiManager.getConnectionInfo();
        return getWifiInfo();
    }
    public static String getWifiList(int nu){
        if (m_wifiManager == null) {
            m_wifiManager = (WifiManager) m_instance.getSystemService(Context.WIFI_SERVICE);
        }
        m_wifiManager.setWifiEnabled(true);
        m_wifiManager.startScan();
         m_wifiList= m_wifiManager.getScanResults();
        if ( m_wifiList.size() == 0){
                    return "n/a";
        }
        String string = new String();
        for (int i = 0; i <  m_wifiList.size(); i++) {
            string += (( m_wifiList.get(i)).toString());
            string += ";";
        }
        return string;
    }
    public static String getWifiInfo() {
        return (m_wifiInfo == null) ? "NULL" : m_wifiInfo.toString();
    }
    ...
}


4.call from qt like following: 

there is a bug, the function must have parameter, other else noting will return. 

the following parameter "22" is not used, but when I use "(V)Ljava/lang/String;" got nothing;

#include <QtAndroidExtras/QAndroidJniObject>

...
    QAndroidJniObject androidString=
    QAndroidJniObject::callStaticObjectMethod(
                "org/qtproject/qmhp/AndroidCodeMhp",
                                              "getWifiList",
                                              "(I)Ljava/lang/String;",
                                              22);
    m_androidWifiData = androidString.toString();
...


qt call android did work,  but must be careful.



版权声明:本文为u013122984原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。