Reputation: 7891
I'm developing an Android application using Qt. Now I'm going to let users share an image from my app. I tried to create new Intent:
package ir.qtips;
import android.content.Intent;
import android.net.Uri;
public class ShareActivity extends org.qtproject.qt5.android.bindings.QtActivity{
private static ShareActivity instance;
ShareActivity() {
instance = this;
}
public void createInstagramIntent(){
String type = "image/*";
String captionText = "<< media caption >>";
// Create the new Intent using the 'Send' action.
Intent share = new Intent(Intent.ACTION_SEND);
// Set the MIME type
share.setType(type);
// Add the URI and the caption to the Intent.
share.putExtra(Intent.EXTRA_STREAM, Uri.parse("file:///sdcard/a.jpg"));
share.putExtra(Intent.EXTRA_TEXT, caption);
// Broadcast the Intent.
startActivity(Intent.createChooser(share, "Share to"));
}
}
And then I tried to call createInstagramIntent
from C++ :
#ifdef Q_OS_ANDROID
QAndroidJniObject jni("ir/qtips/ShareActivity");
jni.callMethod<void>("createInstagramIntent");
#endif
But It doesn't work. In logs from app one line is more important than others I think:
java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
Upvotes: 3
Views: 410
Reputation: 21899
You cannot create a Handler Thread from a thread that's hasn't called Looper.prepare( )
. What you can do is you can create a Handler
Thread like following using Looper
Static Class
Handler handler = new Handler(Looper.getMainLooper());
Here is an example project that has called Android Text to Speech Service. You can follow this code to solve your problem because Text To Speech should also be called the same way as Share Intent
Show/Hide keyboard example, It also needs to be called from the UI Thread. It may help you out.
{
public void setSuspendSleep() {
this.runOnUiThread( new Runnable() {
public void run()
{
getWindow().addFlags( WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON );
}
} );
}
public void setAllowSleep() {
this.runOnUiThread( new Runnable() {
public void run() {
getWindow().clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
}
} );
}
}
------- and then in C++ ------
// getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
// getWindow().clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
extern "C" void SuspendSleep( int bStopSleep )
{
static int init;
static jobject WindowManager_LayoutParams_FLAG_KEEP_SCREEN_ON;
// Attaches the current thread to the JVM.
jint lResult;
jint lFlags = 0;
JavaVM* lJavaVM = engine.app->activity->vm;
JNIEnv* lJNIEnv = engine.app->activity->env;
JavaVMAttachArgs lJavaVMAttachArgs;
lJavaVMAttachArgs.version = JNI_VERSION_1_6;
lJavaVMAttachArgs.name = "NativeThread";
lJavaVMAttachArgs.group = NULL;
lResult=lJavaVM->AttachCurrentThread(&lJNIEnv, &lJavaVMAttachArgs);
if (lResult == JNI_ERR) {
return;
}
// Retrieves NativeActivity.
jobject lNativeActivity = engine.app->activity->clazz;
jclass ClassNativeActivity = lJNIEnv->GetObjectClass(lNativeActivity);
if( bStopSleep )
{
jmethodID MethodSetFlags = lJNIEnv->GetMethodID( ClassNativeActivity, "setSuspendSleep", "()V");
if( MethodSetFlags )
lJNIEnv->CallVoidMethod( lNativeActivity, MethodSetFlags );
}
else
{
jmethodID MethodSetFlags = lJNIEnv->GetMethodID( ClassNativeActivity, "setAllowSleep", "()V");
if( MethodSetFlags )
lJNIEnv->CallVoidMethod( lNativeActivity, MethodSetFlags );
}
// Finished with the JVM.
lJavaVM->DetachCurrentThread();
}
Upvotes: 3