Ratwanska Ratwanska - 5 months ago 38
Android Question

how to wait for return value from javascript in android and proceed

I am just a learner of Android since a week before, It might be a trivial for someone but this looks big for me as I am new to this. I searched a lot and couldn't found a straight forward question or solution. So, I dont want to spend much time in confusing and post my question here.

I am having a webview in my app, which loads a webpage. I placed a button "Generate" below webview and calling an JS function "saveData()" in that web webpage in that button's onclick. This function needs few seconds to execute and return its response based on the user's network connection speed. Then, I am showing "copy" button to copy the return value.

generate.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
generate.setVisibility(View.GONE);
copy.setVisibility(View.VISIBLE);
webView.loadUrl("javascript:alert(saveData)"); // this function needs few seconds.
}
});


I am catching the return value of the JS function as follows, and setting the value of string "JsVal"

String JsVal;
public String getJsVal() {
return jsVal;
}

public void setJsVal(String jsVal) {
this.jsVal = jsVal;
}

webView.setWebChromeClient(new WebChromeClient() {
@Override
public boolean onJsAlert(WebView view, String url, String message,JsResult result) {
Log.d("LogTag from js call method", message);
setJsVal(message);
result.confirm();
return true;
}
});


Here, copy button code is making text copied for paste to somewhere,

copy.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(getApplicationContext(), " Copied", Toast.LENGTH_SHORT).show();
pb.setVisibility(View.GONE);
ClipboardManager myClipboard;
myClipboard = (ClipboardManager)getSystemService(CLIPBOARD_SERVICE);
ClipData myClip;
myClip = ClipData.newPlainText("text", tVal);
myClipboard.setPrimaryClip(myClip);
}
});


Problem is, if I click the generate button, immediatly copy button is available. If I click on the copy button immediatly and paste in notes app, it shows null. If I copy after 2-3 secondes its not null.

I want to show copy button after the JS return value assigned to String variable "JsVal", untill I need to show nothing.

How to do this,

Answer

You should do it using a AsyncTask in button's onClick.

You can take a look at this documentation, https://developer.android.com/reference/android/os/AsyncTask.html

change your code as follows,

generate.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {               
        generate.setVisibility(View.GONE);  
        //copy.setVisibility(View.VISIBLE);   
        webView.loadUrl("javascript:alert(saveData)"); // this function needs few seconds.

        AsyncTaskRunner runner = new AsyncTaskRunner();
        runner.execute();

        }
});     

Also you need to add the following code to get the AsyncTask works.

private class AsyncTaskRunner extends AsyncTask<String, String, String> {
    @Override
    protected String doInBackground(String... params) {
        try {
            if (getVal() == null) {
                doInBackground(params);
            }           
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "success";
    }

    @Override
    protected void onPostExecute(String result) {
        copy.setVisibility(View.VISIBLE);
    }

    @Override
    protected void onPreExecute() {
    }

    @Override
    protected void onProgressUpdate(String... text) {
    }
}

This is the solution you need to implement and it should work as you expected.

Comments