Store information into Preference
package app.test;
import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.text.TextUtils;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Toast;
public class Test extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
WebView webview = new WebView(this);
webview.getSettings().setJavaScriptEnabled(true);
webview.setWebViewClient(mClient);
webview.addJavascriptInterface(new MyJavaScriptInterface(), "BRIDGE");
setContentView(webview);
webview.loadUrl("file:///android_asset/form.html");
}
private static final String JS_SETELEMENT = "javascript:document.getElementById('%s').value='%s'";
private static final String JS_GETELEMENT =
"javascript:window.BRIDGE.storeElement('%s',document.getElementById('%s').value)";
private static final String ELEMENTID = "emailAddress";
private WebViewClient mClient = new WebViewClient() {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(String.format(JS_GETELEMENT, ELEMENTID, ELEMENTID));
return false;
}
@Override
public void onPageFinished(WebView view, String url) {
SharedPreferences prefs = getPreferences(Activity.MODE_PRIVATE);
view.loadUrl(String.format(JS_SETELEMENT, ELEMENTID, prefs.getString(ELEMENTID, "")));
}
};
private class MyJavaScriptInterface {
public void storeElement(String id, String element) {
SharedPreferences.Editor edit = getPreferences(Activity.MODE_PRIVATE).edit();
edit.putString(id, element);
edit.commit();
if(!TextUtils.isEmpty(element)) {
Toast.makeText(Test.this, element, Toast.LENGTH_SHORT).show();
}
}
}
}
/*<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html>
<form name="input" action="form.html" method="get">
Enter Email: <input type="text" id="emailAddress" />
<input type="submit" value="Submit" />
</form>
</html>*/
Related examples in the same category