Background:

Sometimes we can’t use QT to meet our needs, such as obtaining the sn number of the device. QT can’t do this function, so we have to use Android native functions to do this function. This article is only “(get Android device SN number)” as an example, other methods are similar, learn to learn by analogy, draw inferential.

Now that you have the foundation of the first article, the rest of your work is easy.

How to call Android methods in Qt

Step 1 — Add android code

Based on your Qmake project, select Create Templates in your Build. This will Create an Android folder in your Pro folder, which is the default QtCreator infrastructure for your Android project, like this:There is no SRC folder in this directory, where the Java code is stored, nor libs folder, where the JAR packages you might depend on are stored, so we need to create both manually.

Because we are going to use Java’s native functions for this article, we need to create the SRC directory and the Java classes we are going to use. Like this:Notice where the red line is, this is the package name for our Java code, which we need to create ourselves, and then create the Java class we want to use in the test folder at the bottom level. As follows:

package com.qht.test;

import android.util.Log;
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;

public class Test  {
  
    public String getSN(a) {
        return execCmd("getprop ro.boot.serialno");
    }

    private String execCmd(String cmd){
        String result="";
        Process process = null;
        DataOutputStream dos = null;
        DataInputStream dis = null;
        try {
            process = Runtime.getRuntime().exec(cmd);
            BufferedReader reader = new BufferedReader(
                    new InputStreamReader(process.getInputStream()));
            int read;
            char[] buffer = new char[4096];
            StringBuffer output = new StringBuffer();
            while ((read = reader.read(buffer)) > 0) {
                output.append(buffer, 0, read);
            }
            reader.close();
            process.waitFor();

            result = output.toString();
            return result;
        } catch (IOException e) {
            throw new RuntimeException(e);
        } catch (InterruptedException e) {
            throw newRuntimeException(e); }}}Copy the code

After the Java code is written, open the androidmanifest.xml file in the Android directory and modify the package name. The customized package name is com.qht.test. Our Android part is now complete.

Step 2 – Add QT code

After we have written the Android part of the code above, we need to call the getSN method we have written in QT:

#include <QtAndroid>
#include <QAndroidJniEnvironment>
#include <QAndroidJniObject>
using namespace  QtAndroid;

 QAndroidJniObject class("com/qht/test/Test");
    QAndroidJniObject obj = class.callObjectMethod("getSN"."()Ljava/lang/String;");
    qDebug() < <"HardWare::getSystemDiskSN:" << obj.toString().simplified(a);Copy the code

Since our getSN function returns a String with no arguments, we should have no problem with the above method.