In java File
public class NewActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_new);
String url = "https://www.example.com";
WebView web = (WebView) findViewById(R.id.webView4);
web.loadUrl(url);
final WebView mWebView = (WebView) findViewById(R.id.webView4);
mWebView.getSettings().setJavaScriptEnabled(true);
// Now I create an instance of my JavaScriptInterface class in my OnCreate method.
final JavaScriptInterface myJavaScriptInterface
= new JavaScriptInterface(this);
//After the initialization of the JavaScriptInterface class, I added one more line in my OnCreate method:
mWebView.addJavascriptInterface(myJavaScriptInterface, "AndroidFunction");
mWebView.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);
mWebView.getSettings().setSavePassword(true);
mWebView.getSettings().setSupportZoom(true);
mWebView.getSettings().setSaveFormData(true);
mWebView.getSettings().setSupportZoom(false);
mWebView.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);
mWebView.getSettings().setDomStorageEnabled(true);
mWebView.getSettings().setSupportMultipleWindows(false);
mWebView.getSettings().setLightTouchEnabled(true);
mWebView.getSettings().setBuiltInZoomControls(true);
mWebView.getSettings().setLayoutAlgorithm(WebSettings.LayoutAlgorithm.NORMAL);
mWebView.setWebViewClient(new WebViewClient() {
public boolean shouldOverrideUrlLoading (WebView view, String url){
return true;
}
});
mWebView.loadUrl("https://www.example.com");
}
public class JavaScriptInterface {
Context mContext;
JavaScriptInterface(Context c) {
mContext = c;
}
public void showNotification(String webMessage){
String tittle="Sample Notification";
String subject="Sample Subject";
String body=webMessage;
NotificationManager notif=(NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
Notification notify=new Notification.Builder
(getApplicationContext()).setContentTitle(tittle).setContentText(body).
setContentTitle(subject).setSmallIcon(R.drawable.abc).build();
notify.flags |= Notification.FLAG_AUTO_CANCEL;
notif.notify(0, notify);
}
}
}
And in You webpage, you should write this.
<!DOCTYPE >
<html xmlns="http://www.w3.org/1999/xhtml" debug="true">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="viewport"
content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0">
<meta name="apple-mobile-web-app-capable" content="yes">
<meta name="viewport" content="target-densitydpi=device-dpi" />
<script type="text/javascript">
function init()
{
var testVal = document.getElementById('mytextId').value;
AndroidFunction.showNotification(testVal);
}
</script>
</head>
<body>
<div style="float: left;width: 50%;">
<input type="text" style="width: 180px;"
name="myText" id="mytextId" />
</div>
<div style="clear: both;height: 3px;"> </div>
<div>
<input value="submit" type="button" name="submit"
id="btnSubmit" onclick="javascript:return init();" />
</div>
</body>
</html>
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…