diff --git a/apk/src/app/example/ExampleApp.java b/apk/src/app/example/ExampleApp.java index 073d095..7e41bbe 100644 --- a/apk/src/app/example/ExampleApp.java +++ b/apk/src/app/example/ExampleApp.java @@ -13,14 +13,41 @@ import android.widget.*; //import android.widget.TextView; import android.webkit.*; import android.net.Uri; +import org.json.JSONObject; //import android.webkit.WebView; //import android.webkit.WebMessage; //import android.webkit.WebMessagePort; - +import java.io.InputStream; public class ExampleApp extends Activity { private static final String BASE_URI = "https://alexmahr.de"; + private WebMessagePort port; + private void initPort(WebView myWebView) { + final WebMessagePort[] channel=myWebView.createWebMessageChannel(); + port=channel[0]; + port.setWebMessageCallback(new WebMessagePort.WebMessageCallback() { + @Override + public void onMessage(WebMessagePort port, WebMessage message) { + } + }); + myWebView.postWebMessage(new WebMessage("", new WebMessagePort[]{channel[1]}),Uri.parse(BASE_URI)); + } + + public String readFileFromAssets(String filename) { + String filecontents = ""; + try { + InputStream stream = getAssets().open(filename); + int filesize = stream.available(); + byte[] filebuffer = new byte[filesize]; + stream.read(filebuffer); + stream.close(); + filecontents = new String(filebuffer); + } catch (Exception e) { + // I <3 java exceptions + } + return filecontents; + } @Override public void onCreate(Bundle savedInstanceState) { @@ -39,17 +66,19 @@ public class ExampleApp extends Activity { myWebSettings.setDisplayZoomControls(false); myWebSettings.setJavaScriptEnabled(true); myWebView.addJavascriptInterface(this, "myJavaScriptInterface"); - // providing a webpage inline - String unencodedHtml = "https://html5test.co/

this is html

this is a h2

"; - String encodedHtml = Base64.encodeToString(unencodedHtml.getBytes(),Base64.NO_PADDING); - myWebView.loadDataWithBaseURL(BASE_URI,unencodedHtml, "text/html", "UTF-8",null); + // load the html from assets file + String html = readFileFromAssets("index.html"); + myWebView.loadDataWithBaseURL(BASE_URI,html, "text/html", "UTF-8",null); //myWebView.loadData(encodedHtml, "text/html", "base64"); // alternatively this could be to load a website //myWebView.loadUrl("https://alexmahr.de/ru"); setContentView(myWebView); - new CountDownTimer(30000, 1000) { + new CountDownTimer(5000, 1000) { public void onTick(long millisUntilFinished) { - myWebView.postWebMessage(new WebMessage("this is the message"+millisUntilFinished),Uri.parse(BASE_URI)); + try{ + JSONObject MyJSONObject = new JSONObject("{\"json\":[1,2,3],\"something\":\"test\"}"); + myWebView.postWebMessage(new WebMessage("this is the message"+millisUntilFinished+ " " + MyJSONObject.get("something")),Uri.parse(BASE_URI)); + } catch( Exception e) { } // mTextField.setText("seconds remaining: " + millisUntilFinished / 1000); // myWebView.evaluateJavascript("document.body.innerHTML='SUP "+millisUntilFinished+" all is lost';",null); }