Android Question Java in Starter Service

scGuy

Member
Licensed User
My application uses OkHttpUtils2 to make requests now and then, when the response comes back I need to compute an SHA1 hash. So, when this occurs from the main Activity, it goes like this...

Dim x as HTTPJob

...create the http request and all goes well, the JobDone event fires (in the Activity), and I use the following to call some inline java to get the SHA1 hash.

B4X:
    Dim jo As JavaObject
    jo.InitializeContext
    sHash = jo.RunMethod("getSHA1",Array(sResponse))

I have no issues with the java, it works fine, I'll include it here anyway:

B4X:
#If Java

import java.security.MessageDigest;
import java.util.Locale;

import android.text.TextUtils;


    public static String bytes2Hex(byte[] bytes) {
        String hs = "";
        String stmp = "";
        for (int n = 0; n < bytes.length; n++) {
            stmp = (Integer.toHexString(bytes[n] & 0XFF));
            if (stmp.length() == 1) {
                hs += "0" + stmp;
            } else {
                hs += stmp;
            }
        }
        return hs.toLowerCase(Locale.ENGLISH);
    }

    public static String getSHA1(String text) {
        String sha1 = null;
        MessageDigest sha1Digest = null;
        try {
            sha1Digest = MessageDigest.getInstance("SHA-1");
        } catch (Exception e) {
            return sha1;
        }
        byte[] textBytes = text.getBytes();
        sha1Digest.update(textBytes, 0, text.length());
        byte[] sha1hash = sha1Digest.digest();
        return bytes2Hex(sha1hash);
    }

#End If

Now, I also have a timer that is sitting in the Starter service module, because even when app is paused, I want to be able to rarely perform the same kind of HTTP request, and of course again create the SHA1 hash on the response. But: When it is the JobDone event in the Starter module that fires when the HTTP job is complete, and in the Starter module I attempt this exact same call:

B4X:
    Dim jo As JavaObject
    jo.InitializeContext
    sHash = jo.RunMethod("getSHA1",Array(sResponse))

… I get this error...

java.lang.RuntimeException: Method: getSHA1 not found in: android.app.Application

...even thought I've tried placing the inline java code shown above in the Starter module as well. It appears that when jo.InitializeContext is called from an Activity module that contains the java routine getSHA1, all is well, but when it is called from Starter module, which also contains inline code for getSHA1, it is not found.

Is it possible to have inline java code in non-activity code? Sorry if I didn't explain this very well...

EDIT: I've created a simple project to demonstrate the problem, without all the http distraction. Run the project, in five seconds a timer from Activity will call hash successfully. Then after another five seconds a timer from Starter will attempt hash and fail.

Thanks!
 

Attachments

  • JavaInStarter.zip
    9 KB · Views: 180
Last edited:

scGuy

Member
Licensed User
Answered my own question a little bit: the JavaObject calls DO work from a standard Service Module, just not the Starter module. Apparently Starter is unique enough that JavaObject.InitializeContext can't get a handle on it.
 
Upvote 0
Top