Search code examples
javajavascriptandroidwebviewandroid-jsinterface

Android JavaScript Bridge - Trigger Events


Probably a simple question, I can call a native function from the JavaScript, for example:

Android.setVolume(0.7)

However, I do not know how to trigger (from Android/Java), or handle events (JS) that are not caused by user interaction, for example, alerting the JS when the user's network state changes, the battery gets below a certain point etc.

Any help greatly appreciated.


Solution

  • However, I do not know how to trigger (from Android/Java), or handle events (JS) that are not caused by user interaction, for example, alerting the JS when the user's network state changes, the battery gets below a certain point etc.

    As with the answer you linked to, use loadUrl() with a javascript: URL, akin to how a bookmarklet works.

    The answer is over a year old and is labelled as a hack, is this still the best way to do it?

    I am not completely clear why the author of that answer described it as a hack. Certainly, it's viable and is used a fair bit.