open android settings from QT app(com.android.settings)

痞子三分冷 提交于 2019-12-04 07:37:53

QAndroidJniObject makes it possible to create JNI objects from Qt C++ code.

For example: to get the activity:

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

For example: to create a new Intent:

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

You can then step by step translate execute your java code from C++....

To answer your specific question, just copy/paste this code:

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

...and then vote up! ;-)

This is how you can open details of specific application (by package id) in Application Manager using Qt:

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>());
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!