open android settings from QT app(com.android.settings)
Asked Answered
G

4

6

I have an android application in QT. I would like to call android settings from a button.

I used this code in Java :

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

Is there a way to call android settings using QT C++?

Gangrel answered 29/7, 2014 at 20:42 Comment(0)
O
19

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! ;-)

Orebro answered 29/7, 2014 at 20:54 Comment(2)
Really helped...Thanks ! Would give a hundred upvotes if i can :)Cairngorm
Just create a hundred accounts ;-)Orebro
S
2

The accepted answer does not work with custom Android Settings Applications, and also failed on my new Android One phone, besides that, it opens on the Developer page.

Below is a working code (Qt 5.12) that opens the default settings appplication on first page and can easily be changed to open on other pages:

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);
}
Sift answered 19/11, 2019 at 13:11 Comment(0)
S
0

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>());
}
Sikora answered 12/11, 2015 at 11:43 Comment(0)
C
0

Combined both of your solutions and rewritten to QT6:

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

                QJniObject 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;", QJniObject::fromString("android.intent.category.DEFAULT").object<jstring>());
                intent.callObjectMethod("setData", "(Landroid/net/Uri;)Landroid/content/Intent;", uri.object<jobject>());

                QtAndroidPrivate::startActivity(intent.object<jobject>(), 10101);
            }

tho I wander if there is an possibility to access directly permission page of specific permission, ie: i want to get into app permissions/Camera permission

Candelabra answered 14/10 at 8:9 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.