From 9723489cccabb84c53b2d9823e9b56c3ae6cd282 Mon Sep 17 00:00:00 2001 From: Rodrigo Escar <2019446+futureapricot@users.noreply.github.com> Date: Fri, 18 Mar 2022 07:41:41 -0300 Subject: [PATCH] Implements OpenURL() for Android Platform (#2396) --- src/rcore.c | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/src/rcore.c b/src/rcore.c index 127c8a1eb..7614d3f91 100644 --- a/src/rcore.c +++ b/src/rcore.c @@ -242,6 +242,7 @@ //#include // Required for: Android sensors functions (accelerometer, gyroscope, light...) #include // Required for: AWINDOW_FLAG_FULLSCREEN definition and others #include // Required for: android_app struct and activity management + #include // Required for: JNIEnv and JavaVM [Used in OpenURL()] #include // Native platform windowing system interface //#include // OpenGL ES 2.0 library (not required in this module, only in rlgl) @@ -3457,6 +3458,29 @@ void OpenURL(const char *url) #endif #if defined(PLATFORM_WEB) emscripten_run_script(TextFormat("window.open('%s', '_blank')", url)); +#endif +#if defined(PLATFORM_ANDROID) + JNIEnv *env = NULL; + JavaVM *vm = CORE.Android.app->activity->vm; + (*vm)->AttachCurrentThread(vm, &env, NULL); + + jstring urlString = (*env)->NewStringUTF(env, url); + jclass uriClass = (*env)->FindClass(env, "android/net/Uri"); + jmethodID uriParse = (*env)->GetStaticMethodID(env, uriClass, "parse", "(Ljava/lang/String;)Landroid/net/Uri;"); + jobject uri = (*env)->CallStaticObjectMethod(env, uriClass, uriParse, urlString); + + jclass intentClass = (*env)->FindClass(env, "android/content/Intent"); + jfieldID actionViewId = (*env)->GetStaticFieldID(env, intentClass, "ACTION_VIEW", "Ljava/lang/String;"); + jobject actionView = (*env)->GetStaticObjectField(env, intentClass, actionViewId); + jmethodID newIntent = (*env)->GetMethodID(env, intentClass, "", "(Ljava/lang/String;Landroid/net/Uri;)V"); + jobject intent = (*env)->AllocObject(env, intentClass); + + (*env)->CallVoidMethod(env, intent, newIntent, actionView, uri); + jclass activityClass = (*env)->FindClass(env, "android/app/Activity"); + jmethodID startActivity = (*env)->GetMethodID(env, activityClass, "startActivity", "(Landroid/content/Intent;)V"); + (*env)->CallVoidMethod(env, CORE.Android.app->activity->clazz, startActivity, intent); + + (*vm)->DetachCurrentThread(vm); #endif } }