Hi Felgo Team,
I’m trying to add Account Picker from Google Play services with custom java, but Im getting this error.
The goal is to get the list of all accounts on the Android system. Any hints or alternative way how to fix/do this?
error:
F zygote64: java_vm_ext.cc:534] JNI DETECTED ERROR IN APPLICATION: JNI NewString called with pending exception java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
Java:
also tried extending the class to VPlayActivity but registerForActivityResult
can only be called inside Activity
package org.qtproject.example.jnimessenger;
import android.util.Log;
import android.app.Activity;
import android.accounts.AccountManager;
import android.content.Context;
import android.content.Intent;
//Android X
import androidx.appcompat.app.AppCompatActivity;
import androidx.activity.result.ActivityResult;
import androidx.activity.result.ActivityResultCallback;
import androidx.activity.result.ActivityResultLauncher;
import androidx.activity.result.contract.ActivityResultContracts;
//Google Play Services
import com.google.android.gms.common.AccountPicker;
import com.google.android.gms.common.AccountPicker.AccountChooserOptions;
//Felgo
//import net.vplay.helper.VPlayActivity;
public class JniMessenger extends AppCompatActivity{
private static native void callFromJava(String message);
public JniMessenger() {}
protected void newChooseAccountIntent(Context context) {
Intent intent = new Intent(AccountPicker.newChooseAccountIntent(null, null, new String[]{"com.google"},
false, null, null, null, null));
System.err.println("Java call success!");
launchSomeActivity.launch(intent);
}
ActivityResultLauncher<Intent> launchSomeActivity = registerForActivityResult(
new ActivityResultContracts.StartActivityForResult(),
new ActivityResultCallback<ActivityResult>() {
@Override
public void onActivityResult(ActivityResult result) {
if (result.getResultCode() == Activity.RESULT_OK) {
Intent data = result.getData();
String accountName = data.getStringExtra(AccountManager.KEY_ACCOUNT_NAME);
System.err.println("Account Name=" + accountName);
}
}
});
}
cpp:
#ifdef Q_OS_ANDROID
#include <QtAndroid>
#include <QtAndroidExtras/QAndroidJniObject>
#include <QtAndroidExtras/QAndroidJniEnvironment>
JNIEXPORT jint JNI_OnLoad(JavaVM* vm, void* /*reserved*/)
{
JNIEnv* env;
if (vm->GetEnv(reinterpret_cast<void**>(&env), JNI_VERSION_1_6) != JNI_OK) {
qDebug() << "ERROR: JNI version";
return JNI_ERR;
}
jclass javaClass = env->FindClass("org/qtproject/example/jnimessenger/JniMessenger");
if (!javaClass) {
qDebug() << "class not found";
return JNI_ERR;
} else {
qDebug() << "class found";
QAndroidJniObject classObject(javaClass);
classObject.callMethod<void>("newChooseAccountIntent");
}
return JNI_VERSION_1_6;
}
#endif
Thank you,