本文内容参考自:https://blog.csdn.net/jun4331247/article/details/80739662
1.新建JFullScreen.java,添加以下代码,放置在QT安装目录\android_armv7\src\android\java\src\org\qtproject\qt5\android\bindings
package org.qtproject.junj; import android.app.Activity; import android.content.Context; import android.util.Log; import android.view.View; public class JFullScreen { static final private String TAG = "JFullScreen"; static private JFullScreen mInstance = new JFullScreen(); private Context mContext = null; private JFullScreen() { } static public JFullScreen instance() { return mInstance; } public void setContext(Context context) { Log.d(TAG, "context: " + context.toString()); mContext = context; } public void fullScreenLeanBack() { Log.d(TAG, "fullScreenLeanBack"); Activity activity = (Activity) mContext; View decorView = activity.getWindow().getDecorView(); decorView.setSystemUiVisibility( View.SYSTEM_UI_FLAG_LAYOUT_STABLE | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION | View.SYSTEM_UI_FLAG_FULLSCREEN); } public void fullScreenImmersive() { Log.d(TAG, "fullScreenImmersive"); Activity activity = (Activity) mContext; View decorView = activity.getWindow().getDecorView(); decorView.setSystemUiVisibility( View.SYSTEM_UI_FLAG_IMMERSIVE | View.SYSTEM_UI_FLAG_LAYOUT_STABLE | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION | View.SYSTEM_UI_FLAG_FULLSCREEN); } public void fullScreenStickyImmersive() { Log.d(TAG, "fullScreenStickyImmersive"); Activity activity = (Activity) mContext; View decorView = activity.getWindow().getDecorView(); decorView.setSystemUiVisibility( View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY | View.SYSTEM_UI_FLAG_LAYOUT_STABLE | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION | View.SYSTEM_UI_FLAG_FULLSCREEN); } public int addTest(int value0, int value1) { Log.d(TAG, "I'm a test."); return value0 + value1; } }
2. .pro文件新增:+= androidextras
3.使用JNI写调用Java代码的C++类
这里主要使用QAndroidJniObject类来实现调用Java代码,并使用QtAndroid::runOnAndroidThread让代码执行在UI线程。
jfullscreen.h
#ifndef JFULLSCREEN_H #define JFULLSCREEN_H #include <QAndroidJniObject> class JFullScreen { public: JFullScreen(); void fullScreenLeanBack(); void fullScreenImmersive(); void fullScreenStickyImmersive(); int addTest(int value0, int value1); private: QAndroidJniObject _javaClass; }; #endif // JFULLSCREEN_H
jfullscreen.cpp
#include "jfullscreen.h" #include <functional> #include <QtAndroid> // JFS: JFullScreen #define JFS_CLASSNAME "org/qtproject/junj/JFullScreen" #define JFS_SIGNTURE_INSTANCE "()Lorg/qtproject/junj/JFullScreen;" void realFullScreenLeanBackMethod() { QAndroidJniObject javaCustomFunction = QAndroidJniObject::callStaticObjectMethod(JFS_CLASSNAME, "instance", JFS_SIGNTURE_INSTANCE); javaCustomFunction.callMethod<void>("fullScreenLeanBack", "()V"); } void realFullScreenImmersiveMethod() { QAndroidJniObject javaCustomFunction = QAndroidJniObject::callStaticObjectMethod(JFS_CLASSNAME, "instance", JFS_SIGNTURE_INSTANCE); javaCustomFunction.callMethod<void>("fullScreenImmersive", "()V"); } void realFullScreenStickyImmersiveMethod() { QAndroidJniObject javaCustomFunction = QAndroidJniObject::callStaticObjectMethod(JFS_CLASSNAME, "instance", JFS_SIGNTURE_INSTANCE); javaCustomFunction.callMethod<void>("fullScreenStickyImmersive", "()V"); } JFullScreen::JFullScreen() { if(!QAndroidJniObject::isClassAvailable(JFS_CLASSNAME)) { qDebug("JFullScreen is unavailable."); return; } _javaClass = QAndroidJniObject::callStaticObjectMethod(JFS_CLASSNAME, "instance", JFS_SIGNTURE_INSTANCE); if(!_javaClass.isValid()) { qDebug(" JFullScreen instance is invalid."); } _javaClass.callMethod<void>("setContext", "(Landroid/content/Context;)V", QtAndroid::androidActivity().object<jobject>()); } //use runOnAndroidThread() in order to run on android ui thread void JFullScreen::fullScreenLeanBack() { QtAndroid::Runnable runnable = realFullScreenLeanBackMethod; QtAndroid::runOnAndroidThread(runnable); } void JFullScreen::fullScreenImmersive() { QtAndroid::Runnable runnable = realFullScreenImmersiveMethod; QtAndroid::runOnAndroidThread(runnable); } void JFullScreen::fullScreenStickyImmersive() { QtAndroid::Runnable runnable = realFullScreenStickyImmersiveMethod; QtAndroid::runOnAndroidThread(runnable); } int JFullScreen::addTest(int value0, int value1) { jint sum = _javaClass.callMethod<jint>("addTest", "(II)I", value0, value1); return sum; }
4.添加jfullscreen.cpp、jfullscreen.h到工程
5.测试
创建一个JFullScreen进行测试。
JFullScreen *pManager = new JFullScreen; int sum = pManager->addTest(1, 2); Q_ASSERT(sum == 3); // pManager->fullScreenLeanBack(); // pManager->fullScreenImmersive(); pManager->fullScreenStickyImmersive();
后记,三种全屏模式
1. Lean Back
Lean Back模式应用在不需要用户频繁的点击屏幕的情形下,比如视频播放APP。该模式下,用户只需要单击屏幕,System bar将会自动显示处理,无需做更多的操作。
2.Immersive
Immersive模式应用在用户需要频繁与屏幕交互的情形下,比如游戏,图片浏览,阅读等等。该模式下,用户需要在屏幕边缘向内滑动才能呼出system bar。
3. Sticky immersive
Sticky immersive模式与Immersive模式的区别是:System bar 呼出时,System bar是半透明显示的, 同时System bar会自动隐藏。这种模式的好处是如果用户误操作导致system bar显示出来后,无需做什么其它操作,system bar会自动隐藏,在有些引用场景下,这是一个比较好的用户体验。