Changing orientation of a particular page in android

会有一股神秘感。 提交于 2019-12-19 10:14:20

问题


I am working on an android app in Qt and c++. My whole application has portrait orientation.But when i play a video i want to change the orientation to landscape, and after video ends it should again change to portrait.

So the question is How is it possible to set the screen to landscape or portrait modes in a Qt/C++ application for Android.


回答1:


Screen orientation on Android can be changed using setRequestedOrientation Java function so you should call a Java function from your app. To run Java code in your Qt Android application you should use the Qt Android Extras module which contains additional functionality for development on Android.

You can use JNI to call a Java function from C/C++ or callback a C/C++ function from Java.

Here you could have it in a static Java method like :

package com.MyApp;

public class OrientationChanger
{
    public static int change(int n)
    {
        switch(n)
        {
               case 0:
                   setRequestedOrientation (ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
                   break;
               case 1:
                   setRequestedOrientation (ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
                   break;                   
           }
    }
}

First you need to add this to your .pro file :

QT += androidextras

And Include the relevant header file :

#include <QAndroidJniObject>

You can then call this static Java function from your C++ code.

To change the orientation to landscape mode :

bool retVal = QAndroidJniObject::callStaticMethod<jint>
                        ("com/MyApp/OrientationChanger" // class name
                        , "change" // method name
                        , "(I)I" // signature
                        , 0);

To change the orientation to portrait mode :

bool retVal = QAndroidJniObject::callStaticMethod<jint>
                        ("com/MyApp/OrientationChanger" // class name
                        , "change" // method name
                        , "(I)I" // signature
                        , 1);



回答2:


You don't need to call it through Java code. You can directly call it from C++ using JNI as follow:

void MyAndroidHelperClass::setScreenOrientation(int orientation)
{
    QAndroidJniObject activity = QAndroidJniObject::callStaticObjectMethod("org/qtproject/qt5/android/QtNative", "activity", "()Landroid/app/Activity;");
    if ( activity.isValid() )
    {
        activity.callMethod<void>
                ("setRequestedOrientation" // method name
                 , "(I)V" // signature
                 , orientation);
    }
}


来源:https://stackoverflow.com/questions/28643633/changing-orientation-of-a-particular-page-in-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!