简体   繁体   中英

How to sendbroadcast in callback function

I create an android project, project is a service, no UI. Java layer(function name is onReceive) will receive a broadcast to startservice. And a native c function(function name is startService) will be called. The C function will create a thread, in this thread, it will call function(function name are SendLaResult(native) and SendLaResult(java)) in Java. And the java function will sendBroadcast to another APP. This is a major flow. The following code is sample.

1. start work

public void onReceive(final Context context, final Intent intent) {
...
          new Thread() {
            public void run() {
                try {
                    String str="test";
                    startService(str);
                    Log.i(TAG, "startService end!!!");
                }
                catch(Exception e) {
                    e.printStackTrace();
                }
            }
        }.start();
...
}

2. native-lib.cpp, call funtion in java.

void SendResult( LaResult         laResult)
{
    bool isAttached = false;
    if(!isAttached) {
        int getEnvStat = g_JavaVM->GetEnv((void **)&g_env, JNI_VERSION_1_6);
        if(getEnvStat < 0) {
            jint status = g_JavaVM->AttachCurrentThread(&g_env, NULL);
            if(status >= 0) {
                isAttached = true;
            }
        }
    }
    if(g_env == NULL) {
        LOGE("g_env is NULL!!!");
    }

    jshort conf = laResult.conf ;
    jshort nls = laResult.nls ;
    jshort egoln = laResult.egoln ;

    g_laresult_constructor = g_env->GetMethodID(g_callback_laresult,"<init>","(SSSI[S)V");
    if(g_laresult_constructor == NULL) {
        LOGE("GetMethodID laresult error!!!");
    }

    jobject jparams = g_env->NewObject(g_callback_laresult, g_laresult_constructor, conf, nls, egoln);
    if(jparams == NULL) {
        char * str=strerror(errno);
        LOGE("NewObject jparams error %s !!!" ,str);
    }

    g_mid_sendlaresult = g_env->GetMethodID(g_callback_laservice, "SendLaResult", "(Lcom/xxx/xxx/xxxx/LaResult;)V");
    if(g_mid_sendlaresult == NULL) {
        LOGE("GetMethodID g_mid_sendlaresult error!!!");
    }

    jobject serviceobject = g_env->AllocObject(g_callback_laservice);
    if(serviceobject == NULL) {
        LOGE("AllocObject serviceobject error!!!");
    }

    g_env->CallVoidMethod(serviceobject, g_mid_sendlaresult, jparams);

    g_env->DeleteLocalRef(jparams);
    if(isAttached ) {
        g_JavaVM->DetachCurrentThread();
    }

    return;
}

3.LaService.java, sendBroadcast to another APK

public void SendLaResult(LaResult laresult) {
    Intent intent = new Intent();
    intent.setAction( "com.xxx.xxx.xxx.LA_RESULT" );
    intent.putExtra( "conf", laresult.conf);
    Log.d( "confidence", Integer.toString(laneresult.confidence) );
    intent.putExtra( "nls", laresult.nls);
    Log.d( "nLanes", Integer.toString(laneresult.nLanes) );
    intent.putExtra( "egoln", laresult.egoln);

    sendBroadcast( intent );
}

4. When the sendBroadcast is called, the following crash occur.

--------- beginning of crash
01-02 18:05:11.505 13501-13537/com.xxx.xxx.xxxxx
E/AndroidRuntime: FATAL EXCEPTION: Thread-9
Process: com.xxx.xxx.xxxx, PID: 13501
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.content.Context.sendBroadcast(android.content.Intent)' on a null object reference
at android.content.ContextWrapper.sendBroadcast(ContextWrapper.java:431)
at com.xxx.xxx.xxxxx.xxxxxx.SendLaResult(LaService.java:94)

I found that the sendBroadcast is asynchronous, it is called by system finally, and return immediately. So i delete the following source code in function SendResult.

g_JavaVM->DetachCurrentThread();

That crash don't occur, however, the following error log occur, please refer to source code in function SendResult.

01-02 18:15:03.696 13707-13743/com.xxx.xxx.xxxx
E/native-lib: NewObject jparams error Success !!!

How to fix the first issue(sendBroadcast crash) Or how to fix the send issue(NewObject return null) Please give some suggestion.

I fix this issue. Do not sendbroadcast in function SendLaResult, store every LaResult to a list, create another thread in Oncreate, in this thread sendboadcast every item in list.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM