Basically the JavascriptInterface
receive a Click event from WebView
, then I need to change an HTML element multiple times, the problem is the WebView show only the last change, that's mean the rendering is not immediate.
Question: How to make webview.loadUrl("javascript:updateProgress(20);");
take effect and change WebView content immediately?
- - - - - - - - - - - - - - - - -
Old question:
I have a HTML progress-bar in my WebView, I can simply update the progress-bar value by running webview.loadUrl("javascript:updateProgress(20);");
this work fine from onCreate()
.
// JavaScript in WebView
function updateProgress(percentage){
document.getElementById('progressBar').style.width = percentage + '%';
}
Now, I have a class that send binary data to an connected BLE device, I toke the example from Google BluetoothLeGatt, and I added a method to write to an characteristic (send data) in BluetoothLeService.java.
public void WriteCharacteristic(BluetoothGattCharacteristic characteristic, byte[] data, MainActivity mainactivity){
byte[] data_twenty_byte = new byte [20];
int progress_count = 0;
while(get_next_twenty_byte(data, data_twenty_byte)){
characteristic.setValue(data_twenty_byte);
mBluetoothGatt.writeCharacteristic(characteristic);
progress_count++;
mainactivity.webview.loadUrl("javascript:updateProgress(" + progress_count + ");");
}
}
The problem is the WebView won't be updated (Redraw/Re-Render) while WriteCharacteristic()
is running, the WebView Redraw/Re-Render only after WriteCharacteristic()
finish, mean at progress-bar 100%.
Note: I already tried runOnUiThread(new Runnable() {});
My Question is, How to force mainactivity.webview
to Redraw/Re-Render immediately ?
Thank you,