How can Android code interface with Javascript code in a WebView? - javascript

I know you can take an instance of a webview and call addJavascriptInterface(new WebAppInterface(context), "nameofinterface") to allow javascript to send data to Android Java code. But how do I go the other way? I want to tell javascript to play a video at a specific point in time when I hit the back button on my android device. Any ideas?

webView.loadUrl("javascript:function()");
You can put it here:
#Override
public void onBackPressed() {
// ...
}

Related

Is there a way to get back a result from a PWA I develop to Android using a startActivityForResult from the native app?

I am trying to use a PWA I develop with HTML and JavaScript to process data and send it back to the Android app.
The Android should open the PWA after the user clicks a button for example, I already did this by starting an activity with an intent using the page URL:
Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("https://examplePWA.com/"));
Log.d(TAG, "onClick() called with: view = [" + browserIntent.getDataString() + "]");
startActivity(browserIntent);
What I want now is to get a response intent from the PWA to a startActivityForResult (browserIntent, 123);
sent from Android for example.
How can I do this from the PWA side, is it possible to send an Android Intent from a PWA using JavaScript or any other way?
If I got you right, for the purpose of starting an activity from within the javascript env, you could employ the #JavascriptInterface which will allow you to invoke Java methods inside your javascript code (if you use WebView).
class WebBridge {
#JavascriptInterface
public void openBrowser(String href) {
// .. start your activity here
}
}
// Somewhere where you initialize your WebView
webView.addJavascriptInterface(new WebBridge(), "AndroidWebBridge");
// Inside your javascript app code
window.AndroidWebBridge.openBrowser("https://google.com");
The code below illustrates the overall concept, not a copy-paste solution. Hope that helps your.

how to disable android ImageButton from javascript

i have a hybrid app. I used web-view to make a android hybrid app. I also have a native menubar above the webview. My question is, How can i disable the native menu from my web application? Is it possible? If yes please share your thoughts on this
Check the answer of this question, as they provide snippet code to call Java method from javascript in Android, this will help you if you create a Java method that will disable your ImageButton from a Javascript
Call Java function from JavaScript over Android WebView
You will need to find the user-agent click here to see how to check useragent then u can write a simple javascript function on page load to disable your header
Try to set webviewclient to your webview and override onPageFinished like this. In my project I tried to hide google and facebook buttons on fitbit page.
webView.setWebViewClient(new WebViewClient() {
#Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
if (!TextUtils.isEmpty(url) && url.contains("fitbit")) {
view.loadUrl("javascript: setTimeout(function () { $('.external-choices,.or').hide();} , 1000) ");//JS to hide the fitbit login from G+ and FB
}
}
}

Can javascript in an html running on the Android set the alarm time of Android?

Can javascript in an html running on the Android get access to alarm manager interface ?
Of course yes.
Js and android can invoke each other'methods well.
Yes I think it is possible.
#JavascriptInterface
public boolean invokeAlarmManager() {
//Do whatever you need with AlarmManager
}

IE detects XSS when invoking a method in GWT class using window.opener

I have a GWT application that opens a second browser window. I would like my second window to be able to call a method within the entry point of the first window.
The code sample below works in production (web) mode, but when I try running it in hosted mode, IE detects XSS and overwrites the page with a single "#" to protect against the detected attack. I'm guessing this is because my GWT code server is running on localhost while the application I'm testing is deployed on a virtual machine.
Update: It appears that IE XSS Filtering is sporadic. Sometimes I'm able to get the page to load. But awhile later it starts filtering again.
public class MainWindow implements EntryPoint {
...
#Override
public void onModuleLoad() {
registerJSNIFunctions(this);
}
private native void registerJSNIFunctions(MainWindow mw) /*-{
$wnd.sayHi = function (name) {
mw.#MainWindow::sayHi(Ljava/lang/String;)(name);
}
}-*/;
public void sayHi(String name) {
alert("Hi " + name); // not valid, but you get the point
}
...
}
public class SecondWindow implements EntryPoint {
...
#Override
public void onModuleLoad() {
...
sayHi("kylos");
}
public static native void sayHi(String name) /*-{
$wnd.opener.window.$wnd.sayHi(name);
}-*/;
}
Any ideas on how I could get this to work in hosted mode? Or is there a better way to do cross-window communication with GWT?
Your question is quite interesting, see other´s opinion but I have done something similar using OAuth.
So, if the idea at the end is call from one Window to other some method I´d something like:
....
#Maybe if you use window instead of top works as well
$wnd.opener.top.location.replace(url);
$wnd.close();
....
....
And in the other browser wait for the new request, parse the url, and call "locally" to sayHi. Is this approach valid to you?
If you want further details about the Windows properties you can see W3Schools page
But basically:
$wnd.opener #returns Returns a reference to the window that created the window.
top #returns the topmost browser window
So the issue seems to be sporadic. I'm not sure how exactly the filter gets triggered, but when it does, the rewritten page gets cached by IE so future requests are guaranteed to fail until the browser cache is emptied.
I also found this Microsoft document that describes a custom header, X-XSS-Protection, that can be used to disable the filter. Obviously, this should only be used on a dev system in hosted mode.
To disable the filter, add the following header to your server configuration:
X-XSS-Protection: 0

javascript not working in android

I have a webview in my program.
I loaded a string into this webview that contains javascript function. When i loaded it in the emulator it's not working
i.e if I write a simple alert the webView won't display the alert.
I have enabled the javascript. But then also its not working.
what may be the reason?
Please help
Thankyou
Alerts will not normally work in a WebView. You will need to write the code to do that yourself. You can easily do that by implementing your own version of WebChromeClient. In otherwords:
class ChromeClient extends WebChromeClient {
public boolean onJsAlert(WebView view, String url, String message, JsResult result) {
Toast.makeText(view.getContext(), message, Toast.LENGTH_SHORT).show();
}
}
...
mWebView.setWebChromeClient(new ChromeClient());
Another thing I would recommend is implementing the onConsoleMessage as well. This way you can just use "console.log" in your JavaScript and have it directed to Toast or the Android Log.

Categories