javascriptinterface in activity itself

This commit is contained in:
Alexander Mahr 2024-10-09 16:23:50 +02:00
parent 51734be9f8
commit f1dcecf971
2 changed files with 11 additions and 15 deletions

View file

@ -31,14 +31,15 @@ build : ./bin/example.app.apk
#./bin/classes.dex : ./obj/app/example/ExampleApp.class ./obj/app/example/ExampleApp$$1.class ./obj/app/example/MyJavascriptInterface.class
#./bin/classes.dex : ./obj/app/example/ExampleApp.class ./obj/app/example/MyJavascriptInterface.class ./obj/app/example/MyRunnable.class
./bin/classes.dex : ./obj/app/example/ExampleApp.class ./obj/app/example/MyJavascriptInterface.class ./obj/app/example/MyRunnable.class
#./bin/classes.dex : ./obj/app/example/ExampleApp.class ./obj/app/example/MyJavascriptInterface.class ./obj/app/example/MyRunnable.class
./bin/classes.dex : ./obj/app/example/ExampleApp.class
$(ANDROID_HOME)/build-tools/$(ANDROID_VERSION)/d8 $^ --lib $(ANDROID_HOME)/platforms/$(PLATFORM)/android.jar --output bin
./src/app/example/R.java : $(shell find ./res -type f)
$(ANDROID_HOME)/build-tools/$(ANDROID_VERSION)/aapt package -v -f -m -S ./res -J ./src -M ./AndroidManifest.xml \
-I $(ANDROID_HOME)/platforms/$(PLATFORM)/android.jar
./obj/app/example/ExampleApp.class : ./src/app/example/ExampleApp.java ./src/app/example/R.java ./src/app/example/MyJavascriptInterface.java
./obj/app/example/ExampleApp.class : ./src/app/example/ExampleApp.java ./src/app/example/R.java
javac -d ./obj -classpath $(ANDROID_HOME)/platforms/$(PLATFORM)/android.jar -sourcepath ./src $<
./ToyKey.keystore :

View file

@ -23,7 +23,7 @@ public class ExampleApp extends Activity {
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
// we create the webview (at least on the android 14 that is a webview that features avif + websockets etc....)
WebView myWebView = new WebView(this);//activityContext);
MyJavascriptInterface myJavaScriptInterface = new MyJavascriptInterface(this,myWebView);
// MyJavascriptInterface myJavaScriptInterface = new MyJavascriptInterface(this,myWebView);
// we create a webview (there is also setwebviewclient-vs-setwebchromeclient
WebViewClient myWebViewClient= new WebViewClient();
myWebView.setWebViewClient(myWebViewClient);
@ -32,7 +32,7 @@ public class ExampleApp extends Activity {
myWebSettings.setBuiltInZoomControls(true);
myWebSettings.setDisplayZoomControls(false);
myWebSettings.setJavaScriptEnabled(true);
myWebView.addJavascriptInterface(myJavaScriptInterface, "myJavaScriptInterface");
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 encodedHtml = Base64.encodeToString(unencodedHtml.getBytes(),Base64.NO_PADDING);
@ -40,17 +40,12 @@ public class ExampleApp extends Activity {
// alternatively this could be to load a website
//myWebView.loadUrl("https://alexmahr.de/ru");
setContentView(myWebView);
// new java.util.Timer().schedule( new MyRunnable(myWebView),2000);
// java.util.TimerTask() {
// @Override
// public void run() {
// this actually blocks all of the webview
try {
Thread.sleep(3000);
} catch (Exception e) { }
myWebView.evaluateJavascript("document.body.innerHTML='all is lost';",null);
// // this code will be executed after 2 seconds
// }
// , 2000);
}
@JavascriptInterface
public String toString() {
// this.webview.evaluateJavascript("(setTimeout(()=>{document.body.innerHTML='all gone';},2000)()",null);
return "this is good";
}
}