本文讲述了使用Qt5.3.0开发Android应用的方法,由于官方资料较少,此处记录开发过程遇到的问题及解决方法。具体步骤如下:
1.Android平台的视频播放,只能使用qml的MediaPlayer
2.qml中控件的路径必须加file:// 例如:
- Image{
- source: "file:///mnt/usbhost1/Config/logo.png"
- }
3.C++与qml中js的方法互调
- QQuickView view;
- view.setSource(QUrl(QStringLiteral("qrc:///qml/MainView.qml")));
- QObject *qmlObj =(QObject*) view.rootObject();
- MainWnd *w=new MainWnd(object);
- //暴露C++类给qml供其调用,别名mainWndClass
- view.engine ()->rootContext ()->setContextProperty (QLatin1String("mainWndClass"),w);
- //c++调用qml中的js方法
- //参数必须转换为QVariant
- QMetaObject::invokeMethod (qmlObj,"showRight",Q_ARG(QVariant,1));
- //调用子项的js方法
- qmlPlayer = qmlObj->findChild<QObject*>("playerArea");
- QMetaObject::invokeMethod (qmlPlayer,"setVideoFile",Q_ARG(QVariant,currentVideoFile));
- //MainView.qml
- Rectangle {
- anchors.fill: parent
- property int leftAreaWidth: this.width/5*4
- property int rightAreaWidth: this.width/5
- property int queueFontSize
- function showRight(isShow){
- ....
- }
- Player{
- id:playerArea
- //设置objectName,在c++中才能找到它
- objectName: "playerArea"
- width: parent.width
- height: parent.height
- }
- }
4.c++调用java Android api
在项目目录下建立目录\android\src\org\rophie\ProjectName\JavaClass.java
- org\rophie\ProjectName即为java类的包名package org.rophie.ProjectName;
如我调用Android API调节系统音量
- package org.rophie.ProjectName;
- import org.qtproject.qt5.android.bindings.QtActivity;
- import android.widget.Toast;
- import android.media.AudioManager;
- import android.content.Context;
- public class JavaClass extends QtActivity{
- private static JavaClass m_instance;
- private static AudioManager mAudioManager;
- public JavaClass()
- {
- //构造函数必须
- m_instance = this;
- }
- public static void setVolume(int vol){
- if(mAudioManager==null){
- mAudioManager = (AudioManager)m_instance.getSystemService(Context.AUDIO_SERVICE);
- }
- mAudioManager.setStreamVolume(AudioManager.STREAM_MUSIC, vol, 0);
- }
- }
C++调用:
- QAndroidJniObject::callStaticMethod<void>("org/rophie/ProjectName/JavaClass","setVolume","(I)V",3);
- //具体参照QAndroidJniObject类
5.BroadcastReceiver实现开机自启,和Android一模一样
- public void onReceive(Context context, Intent intent) {
- ......
- //JavaClass为继承QtActivity的java主类
- Intent intent2 = new Intent(context, JavaClass.class);
- ......
- }
6.调用第三方jar包,在src同级目录下新建目录libs,将.jar拷入即可使用
希望本文所述方法对大家的Android开发有所帮助。