Reputation: 554
So I have a web application that I am loading via WebView inside my android app. How can I call a function inside my android app when a user has clicked a button on the external website? Besides running that function I also want to be able to pass variables to it. Maybe link like myapp://activity/?d=variables%20to%20pass ? How should I implement that both on the website and the android app?
Upvotes: 2
Views: 1355
Reputation: 197
well, not sure if your page can contain "normal" web-links as well... (I'm assuming it can in the example below).
first of all, the page has to tell the app that it's a link the app has to handle
using your link:
"myapp://activity/?d=variables"
in the webview-activity, you'll then have to override the Webviewclient, intercept the url the link would be calling, check if that link is for your app, and eventually execute the required actions, parse parameters etc.:
_webView.setWebViewClient(new WebViewClient(){
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
//find out if the link we're looking for is really for this app
if (url.contains("myapp://activity")){
String action= = url.substring("myapp://activity/?".length());
//parse the action from the link...
//do something with this action..., parse additional params etc.
return true;
}
//handle the "normal" links...
return super.shouldOverrideUrlLoading(view, url);
}
});
hope this helps
Upvotes: 4