Qt上的startActivity,没有任何显示

时间:2015-12-18 08:47:35

标签: android qt

我编码如下。 它似乎显示Android设置菜单,但没有任何结果。 我没有看到任何错误或记录它。我不知道在调用startActivity()之后会发生什么。

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

if(activity.isValid()){
    QAndroidJniObject p1 = QAndroidJniObject::fromString("com.android.settings");
    QAndroidJniObject p2 = QAndroidJniObject::fromString("com.android.settings.DevelopmentSettings");

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

    if(p1.isValid() && p2.isValid() && intent.isValid()){

         qDebug() << "intent create OK and startActivity";

         intent.callObjectMethod("setClassName",
                                         "(Ljava/lang/String;Ljava/lang/String;)Landroid/content/Intent;",
         p1.object<jobject>(),
         p2.object<jobject>());
         intent.callObjectMethod("startActivity",
                     "(Landroid/content/Intent;)V",
                     intent.object<jobject>());
    }
}

1 个答案:

答案 0 :(得分:1)

startActivityActivity类的方法,而不是Intent。因此需要在activity对象上调用它,而不是在intent上调用。

intent.callObjectMethod("startActivity", "(Landroid/content/Intent;)V", intent.object<jobject>());

必须替换为:

activity.callObjectMethod("startActivity", "(Landroid/content/Intent;)V", intent.object<jobject>());

以下是打开Android设置菜单的工作代码:

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>());
        }
    }
}