Search code examples
androidc++qtqandroidjniobject

Opening the Android settings through QAndroidJniObject


I'm developing a Qt Application, and relying on the QAndroidJniObject class to interact with the underlying java layer. I want a button in my app to open the android settings manager. My tentative solution is creating an intent, setting its action and starting an activity with that intent:

const QAndroidJniObject action = QAndroidJniObject::fromString("android.provider.Settings.ACTION_SETTINGS");
QAndroidJniObject intent = QAndroidJniObject("android.content.Intent");
intent.callObjectMethod("setAction", "(Ljava/lang/String;)Landroid/content/Intent;", action.object<jstring>());
QtAndroid::startActivity(intent.object<jobject>(), requestType, this);

However, when I call that function from a qml button, the app crashes with

JNI DETECTED ERROR IN APPLICATION: JNI NewString called with pending exception android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.provider.Settings.ACTION_SETTINGS }

Furthermore, for other activities such as opening a document, done by substituting line 1 in the previous code with this:

const QAndroidJniObject action = QAndroidJniObject::fromString("android.content.Intent.ACTION_OPEN_DOCUMENT");

it works.

My understanding is that the operating system is looking for an activity that is able to perform the specified action, and that both actions could be performed automatically by the basic activities of the OS itself, but this seems not true. What am I missing?


Solution

  • I found the error, I was not creating the action correctly. Because I am using QAndroidJniObject::fromString, I should have passed as a string the action value, not the action name. So, according to the documentation, the correct argument is "android.settings.SETTINGS" (i.e. the value of the variable), instead of its name, "android.provider.Settings.ACTION_SETTINGS".