我想在我的应用程序中实现拨号器功能.实际上,它完成了,但它的工作方式我不想这样做.按下按钮时,将打开本机拨号程序并等待按下按钮.是否可以直接拨打而无需双击?这是我的代码:
Button {
id: callButton
anchors.centerIn: parent
text: 'Make a call'
onClicked: Qt.openUrlExternally('tel:+77051085322')
}
Run Code Online (Sandbox Code Playgroud)
而在iOS上的呼叫可以发出直接,同样不适用于Android系统.要解决此问题,您可以定义一个C++类Wrapper
来处理调用,具体取决于当前的操作系统.此类的实例注册为上下文属性,并直接在QML中使用.
在课程中,您可以利用Android本机API,通过Intent
操作提供自动拨号功能ACTION_CALL
(但请记住,使用它时有一些限制).通常在Android中你写:
Intent callIntent = new callIntent(Intent.ACTION_CALL);
callIntent.setPackage("com.android.phone"); // force native dialer (Android < 5)
callIntent.setPackage("com.android.server.telecom"); // force native dialer (Android >= 5)
callIntent.setData(Uri.parse("tel:" + number));
callIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(callIntent);
Run Code Online (Sandbox Code Playgroud)
通过设置包,我们可以强制使用本机拨号程序.如果没有它,如果在该设备上安装了其他拨号器,用户将会立即选择可用的拨号器(即Skype,Viber等).系统拨号程序包在Lollipop和以前的版本之间进行了更改,因此有必要在运行时检查SDK以设置正确的SDK.
要在C++中调用这些API,您需要Qt Android Extras,特别是QAndroidJniObject
自定义Android清单中的相关权限.只需添加到您的.pro
文件:
android: QT += androidextras #included only in Android builds
Run Code Online (Sandbox Code Playgroud)
以及您的清单中的以下行:
<uses-permission android:name="android.permission.CALL_PHONE"/>
Run Code Online (Sandbox Code Playgroud)
如果您没有定义自定义清单,只需添加一个.从Qt Creator 3.3开始,只需转到Projects > Build > Build Android APK > Create Templates
生成自定义清单.
我们类的标题如下所示 - 缺少构造函数/解构函数:
#ifndef WRAPPER_H
#define WRAPPER_H
#include <QObject>
#include <QString>
#include <QDebug>
#if defined(Q_OS_IOS)
#include <QUrl>
#include <QDesktopServices>
#elif defined(Q_OS_ANDROID)
#include <QtAndroid>
#include <QAndroidJniObject>
#endif
#include <QDesktopServices>
#include <QUrl>
class Wrapper: public QObject
{
Q_OBJECT
public:
Q_INVOKABLE void directCall(QString number);
};
#endif // WRAPPER_H
Run Code Online (Sandbox Code Playgroud)
相应的源文件如下所示 - 缺少构造函数/解构函数:
#include "wrapper.h"
void Wrapper::directCall(QString number)
{
#if defined(Q_OS_IOS)
QDesktopServices::openUrl(QUrl(QString("tel://%1").arg(number)));
#elif defined(Q_OS_ANDROID)
// get the Qt android activity
QAndroidJniObject activity = QAndroidJniObject::callStaticObjectMethod("org/qtproject/qt5/android/QtNative", "activity", "()Landroid/app/Activity;");
//
if (activity.isValid()){
// real Java code to C++ code
// Intent callIntent = new callIntent(Intent.ACTION_CALL);
QAndroidJniObject callConstant = QAndroidJniObject::getStaticObjectField<jstring>("android/content/Intent", "ACTION_CALL");
QAndroidJniObject callIntent("android/content/Intent", "(Ljava/lang/String;)V", callConstant.object());
// callIntent.setPackage("com.android.phone"); (<= 4.4w) intent.setPackage("com.android.server.telecom"); (>= 5)
QAndroidJniObject package;
if(QtAndroid::androidSdkVersion() >= 21)
package = QAndroidJniObject::fromString("com.android.server.telecom");
else
package = QAndroidJniObject::fromString("com.android.phone");
callIntent.callObjectMethod("setPackage", "(Ljava/lang/String;)Landroid/content/Intent;", package.object<jstring>());
// callIntent.setData(Uri.parse("tel:" + number));
QAndroidJniObject jNumber = QAndroidJniObject::fromString(QString("tel:%1").arg(number));
QAndroidJniObject uri = QAndroidJniObject::callStaticObjectMethod("android/net/Uri","parse","(Ljava/lang/String;)Landroid/net/Uri;", jNumber.object());
callIntent.callObjectMethod("setData", "(Landroid/net/Uri;)Landroid/content/Intent;", uri.object<jobject>());
// callIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
jint flag = QAndroidJniObject::getStaticField<jint>("android/content/Intent", "FLAG_ACTIVITY_NEW_TASK");
callIntent.callObjectMethod("setFlags", "(I)Landroid/content/Intent;", flag);
//startActivity(callIntent);
activity.callMethod<void>("startActivity","(Landroid/content/Intent;)V", callIntent.object<jobject>());
}
else
qDebug() << "Something wrong with Qt activity...";
#else
qDebug() << "Does nothing here...";
#endif
}
Run Code Online (Sandbox Code Playgroud)
如开头所述,您可以将此类的实例包含为上下文属性.将main
用于此目的如下所示:
#include <QApplication>
#include <QQmlContext>
#include <QQmlApplicationEngine>
#include "wrapper.h"
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
QQmlApplicationEngine engine;
Wrapper jw;
engine.rootContext()->setContextProperty("caller", &jw);
engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
return app.exec();
}
Run Code Online (Sandbox Code Playgroud)
最后在QML中你可以简单地写:
Button {
id: callButton
anchors.centerIn: parent
text: 'Make a call'
onClicked: caller.directCall("+0123456789")
}
Run Code Online (Sandbox Code Playgroud)
代码可以很容易地扩展到支持WinPhone,同时保持相同的QML接口(可能通过包含专用的头/源对).最后,条件包含的使用保证了代码正确编译,即使使用的工具包在运行中更改.
最后,我要补充一点,Google Play政策并不像Apple App Store政策那么严格.因此,ACTION_CALL
不太可能发生因使用而导致的拒绝应用.
归档时间: |
|
查看次数: |
6538 次 |
最近记录: |