Home > Mobile >  Android method returns a value, need to wait for a result to return after processing is complete, no
Android method returns a value, need to wait for a result to return after processing is complete, no

Time:11-20

In MainAcitivity:

I'm in a webview tuned up a js interface
() in this interface is above the androidJsUtils

The interface for the mainactivity to tone up or gallery, after the operation I inside the onActivityResult method to get the two pictures, the following

I am unable to deal with is how to deal with the interface to return the onActivityResult good pictures, if like the above write returns from the start, my first idea was to have the thread block waiting for my pictures onActivityResult full after processing, and then return the bitmap, the problem is the interface is in the main thread, if blocked even can't get into the dialog, not blocked again back to the first time to return to the empty bitmap,

CodePudding user response:

Hs invokes the Java method is not in the main thread, so you can add a lock wait before the return should be feasible,


Generally don't do it, you can add a callback in interface, when to get the images by the callback correction result

CodePudding user response:

How do you write this callback brother can help give a simple example, although know is a callback, but can't completely write

CodePudding user response:

@ JavascriptInterface
Public void addPhoto (String STR) {//"{" callBack" : "callBack"} "
Try {
If (TextUtils. IsEmpty (STR)) return;
JSONObject object=new JSONObject (STR);
The callBack=object. Get string (" callBack ");

} the catch (JSONException e) {
e.printStackTrace();
}
}

After taking pictures:
Handler. Post (new Runnable () {
@ Override
Public void the run () {
The String url="javascript:" + callBack + "(" + callbackStr +") ";
Logger. D (TAG, "load js url=" + url);
WebView. LoadUrl (url);
}
});

The callback is a js method name, need web side to you

CodePudding user response:

CallbackStr is your good image information data with web side agreed

CodePudding user response:

Yes thank you very much I figure that out

CodePudding user response:

A polling child thread
  • Related