example webmessage
This commit is contained in:
parent
f6261aebcd
commit
61c53c1fe4
3 changed files with 29 additions and 57 deletions
|
@ -1,5 +1,10 @@
|
|||
# Repo allowing -in a KISS way- to create a containered way to build an Android App APK
|
||||
|
||||
|
||||
## changelog
|
||||
|
||||
* using https://android.googlesource.com/platform/cts/+/android-7.1.1_r13/tests/tests/webkit/src/android/webkit/cts/PostMessageTest.java derived
|
||||
webmessage to send data to Javascript/Webview from Java (using a timer)
|
||||
## brach `no-res.xml`
|
||||
|
||||
this branch features the exmaple.app to not require the compoletely bogus unneeded requirement
|
||||
|
|
|
@ -1,22 +1,27 @@
|
|||
package app.example;
|
||||
|
||||
import android.util.Log;
|
||||
import android.widget.TextView;
|
||||
import android.util.Base64;
|
||||
import android.app.Activity;
|
||||
import android.os.Bundle;
|
||||
import android.text.method.ScrollingMovementMethod;
|
||||
import android.view.MenuItem;
|
||||
import android.view.*;
|
||||
import android.widget.*;
|
||||
import android.webkit.WebView;
|
||||
import android.webkit.*;
|
||||
import android.view.ViewGroup.*;
|
||||
import android.util.Base64;
|
||||
import java.lang.Thread;
|
||||
import android.os.CountDownTimer;
|
||||
import android.text.method.ScrollingMovementMethod;
|
||||
import android.view.*;
|
||||
//import android.view.MenuItem;
|
||||
import android.view.ViewGroup.*;
|
||||
import android.widget.*;
|
||||
//import android.widget.TextView;
|
||||
import android.webkit.*;
|
||||
import android.net.Uri;
|
||||
//import android.webkit.WebView;
|
||||
//import android.webkit.WebMessage;
|
||||
//import android.webkit.WebMessagePort;
|
||||
|
||||
|
||||
public class ExampleApp extends Activity {
|
||||
|
||||
private static final String BASE_URI = "https://alexmahr.de";
|
||||
|
||||
@Override
|
||||
public void onCreate(Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
|
@ -35,23 +40,24 @@ public class ExampleApp extends Activity {
|
|||
myWebSettings.setJavaScriptEnabled(true);
|
||||
myWebView.addJavascriptInterface(this, "myJavaScriptInterface");
|
||||
// providing a webpage inline
|
||||
String unencodedHtml = "<!DOCTYPE html><html><script> window.addEventListener('load',()=>{d=document.createElement('div');d.innerHTML=myJavaScriptInterface.toString()+'JAVASCRIPT works';document.body.appendChild(d);},false);</script><a href='https://html5test.co/'>https://html5test.co/</a><h1> this is html <h1> <h2> this is a h2</h2><img src='https://wald.alexmahr.de/images/rabe.avif'></html>";
|
||||
String unencodedHtml = "<!DOCTYPE html><html><script> window.addEventListener('message',(e)=>{document.body.innerHTML='';u=document.createElement('div');u.innerHTML='message'+e.data;document.body.appendChild(u);},true);window.addEventListener('load',()=>{d=document.createElement('div');d.innerHTML=myJavaScriptInterface.toString()+'JAVASCRIPT works';document.body.appendChild(d);},false);</script><a href='https://html5test.co/'>https://html5test.co/</a><h1> this is html <h1> <h2> this is a h2</h2><img src='https://wald.alexmahr.de/images/rabe.avif'></html>";
|
||||
String encodedHtml = Base64.encodeToString(unencodedHtml.getBytes(),Base64.NO_PADDING);
|
||||
myWebView.loadDataWithBaseURL("https://alexmahr.de/",unencodedHtml, "text/html", "UTF-8",null);
|
||||
myWebView.loadDataWithBaseURL(BASE_URI,unencodedHtml, "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) {
|
||||
public void onTick(long millisUntilFinished) {
|
||||
public void onTick(long millisUntilFinished) {
|
||||
myWebView.postWebMessage(new WebMessage("this is the message"+millisUntilFinished),Uri.parse(BASE_URI));
|
||||
// mTextField.setText("seconds remaining: " + millisUntilFinished / 1000);
|
||||
myWebView.evaluateJavascript("document.body.innerHTML='SUP "+millisUntilFinished+" all is lost';",null);
|
||||
}
|
||||
// myWebView.evaluateJavascript("document.body.innerHTML='SUP "+millisUntilFinished+" all is lost';",null);
|
||||
}
|
||||
|
||||
public void onFinish() {
|
||||
public void onFinish() {
|
||||
myWebView.evaluateJavascript("document.body.innerHTML='all is lost';",null);
|
||||
}
|
||||
}.start();
|
||||
}
|
||||
}.start();
|
||||
}
|
||||
@JavascriptInterface
|
||||
public String toString() {
|
||||
|
|
|
@ -1,39 +0,0 @@
|
|||
package app.example;
|
||||
|
||||
import android.util.Log;
|
||||
import android.widget.TextView;
|
||||
import android.app.Activity;
|
||||
import android.os.Bundle;
|
||||
import android.text.method.ScrollingMovementMethod;
|
||||
import android.view.MenuItem;
|
||||
import android.view.*;
|
||||
import android.widget.*;
|
||||
import android.webkit.WebView;
|
||||
import android.webkit.*;
|
||||
import android.view.ViewGroup.*;
|
||||
import android.util.Base64;
|
||||
import android.os.Environment;
|
||||
import java.io.*;
|
||||
//File all;
|
||||
|
||||
public class MyJavascriptInterface {
|
||||
private Activity activity;
|
||||
private WebView webview;
|
||||
|
||||
public MyJavascriptInterface(Activity activity,WebView webview) {
|
||||
this.activity = activity;
|
||||
this.webview = webview;;
|
||||
}
|
||||
|
||||
// @JavascriptInterface
|
||||
// public void startVideo(String videoAddress){
|
||||
// Intent intent = new Intent(Intent.ACTION_VIEW);
|
||||
// intent.setDataAndType(Uri.parse(videoAddress), "video/3gpp");
|
||||
// activity.startActivity(intent);
|
||||
// }
|
||||
@JavascriptInterface
|
||||
public String toString() {
|
||||
// this.webview.evaluateJavascript("(setTimeout(()=>{document.body.innerHTML='all gone';},2000)()",null);
|
||||
return "injectedobject";
|
||||
}
|
||||
}
|
Loading…
Add table
Reference in a new issue