我编码如下。
它似乎显示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>());
}
}
答案 0 :(得分:1)
startActivity
是Activity类的方法,而不是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>());
}
}
}