赞
踩
一个人的成功是岁月的积累和众人乐善好施的结果,我们尊敬那些无私奉献的人,更尊重那些持之以恒的大无畏开拓者.老子狗屁不是,但是为了更多人的成功,无论如何要把成果分享出来,感谢糊涂李哥以及那些帮助我的人
Qt开发android主要是借用JNI技术,让我们能够调用android(java代码)的一些方法,从而实现对多平台的兼容,下面我们以具体的实例来一步步学习如何用qt开发android应用(完整代码在下方链接).需要注意的是,qt的线程和android(调用的java代码)不在同一个线程,是异步处理的,所以直接从java中获取返回值需要注意异步调用这个特性
震动效果主要调用了android的vibrate服务,对应android.os.Vibrator类。切记添加android.permission.VIBRATE权限
- void Widget::onVibrate()
- {
- #ifdef Q_OS_ANDROID
- QAndroidJniObject activity = androidActivity();
- QAndroidJniObject name = QAndroidJniObject::getStaticObjectField(
- "android/content/Context",
- "VIBRATOR_SERVICE",
- "Ljava/lang/String;"
- );
-
- QAndroidJniObject vibrateService = activity.callObjectMethod(
- "getSystemService",
- "(Ljava/lang/String;)Ljava/lang/Object;",
- name.object<jstring>());
-
- jlong duration = 200;
- vibrateService.callMethod<void>("vibrate", "(J)V", duration);
- #endif
- }
屏幕旋转无需添加任何权限,下面展示的是手动控制自旋,对应android.app.Activity 类,设备感应旋转的后续添加
- void Widget::onScreenOrientation()
- {
- #ifdef Q_OS_ANDROID
- QAndroidJniObject activity = androidActivity();
- jint orient = activity.callMethod<jint>("getRequestedOrientation");
-
- orient = (1 == orient)? 0:1;
- activity.callMethod<void>( "setRequestedOrientation", "(I)V", orient);
- #endif
- }
来电铃声有三种基本模式,分别是响铃、震动和静音,对应着android.media.AudioManager 类
- void Widget::onSetRingMode(int iMode)
- {
- #ifdef Q_OS_ANDROID
- QAndroidJniObject activity = androidActivity();
- QAndroidJniObject name = QAndroidJniObject::getStaticObjectField(
- "android/content/Context",
- "AUDIO_SERVICE", "Ljava/lang/String;");
-
- QAndroidJniObject audioService = activity.callObjectMethod(
- "getSystemService",
- "(Ljava/lang/String;)Ljava/lang/Object;",
- name.object<jstring>());
-
- audioService.callMethod<void>( "setRingerMode", "(I)V", iMode);
-
- //获取当前铃声模式
- //iMode = audioService.callMethod<jint>("getRingerMode", "()I");
- #endif
这个功能主要用于一些类似播放视频不息屏或者音乐播放歌词等场景,主要是用android的电源管理类ndroid.os.PowerManager,这个需要添加权限WAKE_LOCK和DEVICE_POWER
- void Widget::onKeepScreenLight(int iStatus)
- {
- #ifdef Q_OS_ANDROID
- if(Qt::Unchecked == iStatus)
- {
- if(m_wakeLock.isValid())
- {
- m_wakeLock.callMethod<void>("release");
- CHECK_EXCEPTION();
- }
- return;
- }
-
- QAndroidJniObject name = QAndroidJniObject::getStaticObjectField(
- "android/content/Context",
- "POWER_SERVICE",
- "Ljava/lang/String;"
- );
-
- QAndroidJniObject activity = androidActivity();
- QAndroidJniObject powerService = activity.callObjectMethod(
- "getSystemService",
- "(Ljava/lang/String;)Ljava/lang/Object;",
- name.object<jstring>());
-
- QAndroidJniObject tag = QAndroidJniObject::fromString("QtJniWakeLock");
- m_wakeLock = powerService.callObjectMethod(
- "newWakeLock",
- "(ILjava/lang/String;)Landroid/os/PowerManager$WakeLock;",
- 10,
- tag.object<jstring>()
- );
-
-
- if(m_wakeLock.isValid())
- {
- m_wakeLock.callMethod<void>("acquire");
- }
- #endif
- }
源代码链接,链接实效了,资源官网给我下架了,你们创建一个qml应用就行,然后把demo代码拷贝进取
赞
踩
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。