How have JS context in native module with Broadcast receiver ?

You must Login before you can answer or comment on any questions.

Hi,

I try to create an Android module using Broadcast receiver to detect network change.

My broadcast receiver works and I can call a callBack method, but when my application is closed, I have an error about V8 runtime disposed.

I try to create to use the same mechanism lie service using an url property to give js file to my module. After that I use KrollRuntime.runModule to execute this code. Is works if my application run but if it was closed runModule wait the next starting.

Is there an other way to have a JS context in my module (something around KrollProxy) an evaluate my js code ?

Thanks,

1 Answer

I managed to create a native intentService that executes javascript code. Here the code it works (I use activity count to have V8 runtime even if the application is minimized). I saw in there is a last fix to add service count. I could use it when this version will be released.

...
protected void onHandleIntent(Intent intent) {
    KrollRuntime.incrementActivityRefCount();
    try {
        KrollRuntime.getInstance().evalString(
                        KrollAssetHelper.readAsset(getPath("background.js")), 
                        getPath("background.js"));              
    }
    finally {
        KrollRuntime.decrementActivityRefCount();
    }
}
 
/**
 * To have absolue path
 * @param url
 * @return
 */
private String getPath(String url) {
    String fullUrl = url;
    if (!fullUrl.contains("://") && !fullUrl.startsWith("/")) {
        fullUrl = new TiUrl(url).baseUrl + fullUrl;
    }
 
    if (fullUrl.startsWith(TiC.URL_APP_PREFIX)) {
        fullUrl = fullUrl.replaceAll("app:/", "Resources");
 
    } else if (fullUrl.startsWith(TiC.URL_ANDROID_ASSET_RESOURCES)) {
        fullUrl = fullUrl.replaceAll("file:///android_asset/", "");
    }
 
    return fullUrl;
    }
...
But I still have one last issue, when the process is closed. The broadcast receiver triggers process starting, but in this case the Runtime doesn't seem complete and KrollRuntime.incrementActivityRefCount hang. I think it's the CountDownLatch which wait but I don't know how have good runtime in this case.

Someone can I help me please ? Thanks

Your Answer

Think you can help? Login to answer this question!