Reputation: 201
I've been working on an web wrapper app. I use webview to load some webpage, which needs user log-in. In order to control log-in process to get automatic log-in, I created a log-in page locally and log-in process works fine. The log-in page will store user log-in info locally so that next time when user opens the app, log-in page will be skipped and logged in webpage will be displayed.
However, I've met problems for log-out. How could user return to the local log-in page? What I'm thinking is to create a log-out bar above webview, with a log-out button there.Are there any better ways?
What's even worse, there is also a log-out button on the webpage and that button will lead user to the online log-in page, which is not able to store log-in info locally since it seems there is no way to retrieve HTML text field info on webview and pass the info to Java (Or I'll have no need to have local log-in page any more). Is there a way to avoid this?
Thanks!
Upvotes: 0
Views: 1798
Reputation: 2921
Check out how Apache Cordova handles communication between Java and JavaScript.
To execute JavaScript from Java, you load a javascript URL on your webview:
mWebView.loadURL("javascript:codeToExec();");
You can also invoke native (Java) code from JS in a WebView:
The first method is to use the @JavascriptInterface
annotation, along with the WebView
's addJavascriptInterface
method. However, this is not safe on devices running on older API levels.
The second method to invoke native code is to make calls to a fake URL, and handle the URL in the WebViewClients
's shouldOverrideUrlLoading
method:
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (url.startsWith("http://execute_code/")) {
// execute native code based on action in url
}
..
}
Since it is more efficient to make calls using addJavascriptInterface
, Cordova enables and uses this interface if the running API level is >= JELLY_BEAN_MR1.
Upvotes: 2