6

我在 QT 中有一个 android 应用程序。我想从一个按钮调用android设置。

我在 Java 中使用了这段代码:

public void usb(View v){
    Intent intent = new Intent();  
    intent.setClassName("com.android.settings", "com.android.settings.DevelopmentSettings");  
    startActivity(intent);  
} 

有没有办法使用 QT C++ 调用 android 设置?

4

3 回答 3

18

QAndroidJniObject 使得从 Qt C++ 代码创建 JNI 对象成为可能。

例如:获取活动:

QAndroidJniObject activity =  QAndroidJniObject::callStaticObjectMethod("org/qtproject/qt5/android/QtNative", "activity", "()Landroid/app/Activity;");

例如:创建一个新的 Intent:

QAndroidJniObject intent("android/content/Intent","()V");

然后,您可以逐步翻译从 C++ 执行您的 java 代码......

要回答您的具体问题,只需复制/粘贴此代码:

QAndroidJniObject activity = QAndroidJniObject::callStaticObjectMethod("org/qtproject/qt5/android/QtNative", "activity", "()Landroid/app/Activity;");   //activity is valid
if ( activity.isValid() )
{
    // Equivalent to Jave code: 'Intent intent = new Intent();'
    QAndroidJniObject intent("android/content/Intent","()V");
    if ( intent.isValid() )
    {
        QAndroidJniObject param1 = QAndroidJniObject::fromString("com.android.settings");
        QAndroidJniObject param2 = QAndroidJniObject::fromString("com.android.settings.DevelopmentSettings");

        if ( param1.isValid() && param2.isValid() )
        {
            // Equivalent to Jave code: 'intent.setClassName("com.android.settings", "com.android.settings.DevelopmentSettings");'
            intent.callObjectMethod("setClassName","(Ljava/lang/String;Ljava/lang/String;)Landroid/content/Intent;",param1.object<jobject>(),param2.object<jobject>());

            // Equivalent to Jave code: 'startActivity(intent);'
            activity.callObjectMethod("startActivity","(Landroid/content/Intent;)V",intent.object<jobject>());
        }
    }
}

...然后投票!;-)

于 2014-07-29T20:54:45.997 回答
1

接受的答案不适用于自定义 Android 设置应用程序,并且在我的新 Android One 手机上也失败了,除此之外,它在开发人员页面上打开。

下面是一个工作代码(Qt 5.12),它在第一页打开默认设置应用程序,并且可以很容易地更改为在其他页面上打开:

const QAndroidJniObject ACTION_SETTINGS = QAndroidJniObject::getStaticObjectField("android/provider/Settings",
                                                                                  "ACTION_SETTINGS",
                                                                                  "Ljava/lang/String;");
if (ACTION_SETTINGS.isValid()) {
    const QAndroidIntent intent(ACTION_SETTINGS.toString());
    QtAndroid::startActivity(intent.handle(), 10101);
}
于 2019-11-19T13:11:21.787 回答
0

这是您如何使用 Qt 在应用程序管理器中打开特定应用程序的详细信息(按包 ID):

QAndroidJniObject activity = QAndroidJniObject::callStaticObjectMethod("org/qtproject/qt5/android/QtNative", "activity", "()Landroid/app/Activity;");   //activity is valid
if (activity.isValid())
{
    QAndroidJniObject param = QAndroidJniObject::fromString("package:com.example.mycoolapp");
    // Equivalent to Jave code: 'Uri uri = Uri::parse("...");'
    QAndroidJniObject uri = QAndroidJniObject::callStaticObjectMethod("android/net/Uri", "parse", "(Ljava/lang/String;)Landroid/net/Uri;", param.object<jstring>());
    if (!uri.isValid()) {
        qWarning("Unable to create Uri object");
        return;
    }
    QAndroidJniObject packageName = QAndroidJniObject::fromString("android.settings.APPLICATION_DETAILS_SETTINGS");

    QAndroidJniObject intent("android/content/Intent","(Ljava/lang/String;)V", packageName.object<jstring>());
    if (!intent.isValid()) {
        qWarning("Unable to create Intent object");
        return;
    }
    intent.callObjectMethod("addCategory", "(Ljava/lang/String;)Landroid/content/Intent;", QAndroidJniObject::fromString("android.intent.category.DEFAULT").object<jstring>());
    intent.callObjectMethod("setData", "(Landroid/net/Uri;)Landroid/content/Intent;", uri.object<jobject>());

    activity.callObjectMethod("startActivity","(Landroid/content/Intent;)V",intent.object<jobject>());
}
于 2015-11-12T11:43:27.660 回答