I'm little bit lost, I hope somebody can give me some advice.
I have an application where the main activity starts a service.
The service basically contains a timer in order to execute constantly some
native functions.
Everything is working great as expected, the native library communicates
with the activity and everybody is happy.
Also when closing the main activity, the service is working as expected ..
until I try to wake up the main activity a front of a specific event.
The event is detected inside the native library (C code) and I built a
mechanism in order to bring it out to the Java part.
I created a dummy activity that basically contains a function called from
the native library.
In this function I send out an Intent to wake up the main activity. But I
always end with some exception (usually the null exception).
Here the Java class called from the native library :
public boolean wakeMainActivity()
{
boolean status = MainActivity.internalStatus;
if(MainActivity.internalStatus == true)
Log.d("Wake up activity", "Main activity in focus");
else
{
Log.d("Wake up activity", "Main activity NOT in focus");
/*
* Prepare and send intent to wake up this activity
*/
try
{
Intent intent = new Intent(FakeActivity.this,
MainActivity.class);
startActivity(intent);
} catch ( Exception e) {
e.printStackTrace();
}
}
return status;
}
The flag MainActivity.internalStatus is set in the main activity. onCreate
and OnResume set the flag to true, onPause and onDestroy to false and is
working.
In order to call this function, when the native library detect the event to
wake up the activity, I call this function :
(C code in the JNI area)
/*
* Call JAVA API "wakeMainActivity" in this function
* This function return a status :
* - 0 MainActivty not in focus
* - 1 MainActivity in focus
* - -1 function not found !
*/
int call_wakeMainActivity()
{
int status;
JNIEnv *env = NULL;
jclass cls;
jmethodID mid;
jobject obj;
if (g_JavaVM == NULL)
return -1;
status = (*g_JavaVM)->GetEnv(g_JavaVM, (void**)&env, JNI_VERSION_1_6);
if (env == NULL)
return -1;
cls = (*env)->FindClass(env, "my/application/Fake_activity");
obj = getInstance(env, cls);
mid = (*env)->GetMethodID(env, cls, "wakeMainActivity", "()Z");
if (mid != 0)
{
if((*env)->CallBooleanMethod(env, obj, mid) == 1)
{
/* Called the Java function - return successful - main
activity not in focus */
return 1;
}
else
{
/* Called the Java function - return successful - main
activity in focus */
return 0;
}
}
return -1;
}
g_JavaVM is set on the onCreate and is static.
I'm always able to reach the Java function, but the Intent sent end always
up in a null exception.
I tried many different ways to set up the Intent but I still end up with an
exception.
Any idea ? What I'm doing wrong ?
Thanks
STeve
--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en