android2.3 gps上传数据后重启或是 JNIEnv* env = AndroidRuntime::getJNIEnv(); return == NULL

来源:互联网 发布:怎样注册免费域名 编辑:程序博客网 时间:2024/05/16 08:48

 

转载时请注明出处和作者文章出处:http://blog.csdn.net/lbmygf/archive/2011/06/16/6548086.aspx

作者:曼云-->孤峰

 

 

从android2.2 GPS到android2.3 GPS 结构上有些变化,这里主要说如题所讲的问题。(文件都在硬件抽象层自己找,呵呵)

 

对比一下2.2和2.3的回调函数发现2.3多了很多,其中有个很重要,(gps.h)

 

 

/** Callback for creating a thread that can call into the Java framework code.

 *  This must be used to create any threads that report events up to the framework.

 */

typedef pthread_t (* gps_create_thread)(const char* name, void (*start)(void *), void* arg);

 

 

就是这个了,看看他的解释This must be used 必须的用,不用不行,不用你就会出现标题中的问题,我看开始时也没看这些,郁闷了些

 

时间。

 

下面讲讲怎么用她:

 

 

在2.2时 static void  gps_state_init( GpsState*  state )  函数中       (gps_qemu.c)

 

 

有 if ( pthread_create( &state->thread, NULL, gps_state_thread, state ) != 0 ) {

        LOGE("could not create gps thread: %s", strerror(errno));

        goto Fail;

    }

这样一句对波,到2.3的时候 这句话就不行了,要退休了,2.2到2.3系统还是做了很多优化的,这个就是了,2.2的系统会轮询你有没有

 

创建线程,2.3不会了,你不告诉他,他就不知道,JNIEnv* env = AndroidRuntime::getJNIEnv();这句回去获取创建线程,不用回

 

调函数把线程传上去,上层不知道有这个线程,JNIEnv* env == NULL ,所以你把数据传上去时就出问题了,我遇到的现象是系统不

 

断重启,所以你要改。

 

首先:

 

 

static int

qemu_gps_init(GpsCallbacks* callbacks)

{

 

    GpsState*  s = _gps_state;

 

    s->callbacks = *callbacks;

 

 

    if (!s->init)

        gps_state_init(s);

 

    return 0;

}

 

 

把那 s->callbacks = *callbacks;放到gps_state_init(s);前面去

 

为什么呢?  你再 gps_state_init(s);中要用到回调函数上传线程啊,你不先给个接口给他,他怎么传啊。

 

然后:

 

就是把  

 

if ( pthread_create( &state->thread, NULL, gps_state_thread, state ) != 0 ) {

        LOGE("could not create gps thread: %s", strerror(errno));

        goto Fail;

    }

 

 

这句换成

 

state->callbacks.create_thread_cb("qemu_gps",  gps_state_thread, state);

 

第一个参数"qemu_gps"好像没啥特定意义,自己随便,没研究出来。其他的因该都知道哈。

 

恩,问题解决