webview java调用js_android WebView java与js相互调用

android webView js 使用

1、js调用java

1、1 js代码

function call(){

window.androidInterface.call(‘02585818031‘);

}

1.2、java代码

package com.example.webview;

import android.annotation.SuppressLint;

import android.content.Intent;

import android.net.Uri;

import android.os.Bundle;

import android.support.v7.app.ActionBarActivity;

import android.view.KeyEvent;

import android.view.Menu;

import android.view.MenuItem;

import android.view.Window;

import android.webkit.JavascriptInterface;

import android.webkit.WebSettings;

import android.webkit.WebView;

import android.webkit.WebViewClient;

@SuppressLint("JavascriptInterface")

public class MainActivity extends ActionBarActivity {

private WebView webView;

private int screenHeight;

private int screenWidth;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

requestWindowFeature(Window.FEATURE_NO_TITLE); //设置无标题

setContentView(R.layout.activity_main);

webView = (WebView) findViewById(R.id.web_view);

WebSettings webSettings = webView.getSettings();

webSettings.setBuiltInZoomControls(true);

webSettings.setLoadWithOverviewMode(true);

webSettings.setJavaScriptEnabled(true);

webView.setWebViewClient(new MyWebViewClient());

webView.addJavascriptInterface(new MyAndroidInterface(), "androidInterface");

String url ="file:///android_asset/tangbangjidian/fuwu.html";

//String url ="file:///android_asset/baitian/index.html";

webView.loadUrl(url);

}

class MyAndroidInterface{

public MyAndroidInterface(){};

@JavascriptInterface //注意:加上这行注解

public void call(String number){

//用intent启动拨打电话

Intent intent = new Intent(Intent.ACTION_CALL,Uri.parse("tel:"+number));

startActivity(intent);

}

}

@Override

public boolean onKeyDown(int keyCode, KeyEvent event) {

if ((keyCode == KeyEvent.KEYCODE_BACK) && webView.canGoBack()) {

webView.goBack(); //goBack()表示返回WebView的上一页面

return false;

}

return true;

}

private class MyWebViewClient extends WebViewClient{

@Override

public boolean shouldOverrideUrlLoading(WebView view, String url) {

view.loadUrl(url);// 当打开新链接时,使用当前的 WebView,不会使用系统其他浏览器

return true;

}

}

@Override

public boolean onCreateOptionsMenu(Menu menu) {

getMenuInflater().inflate(R.menu.main, menu);

return true;

}

@Override

public boolean onOptionsItemSelected(MenuItem item) {

int id = item.getItemId();

if (id == R.id.action_settings) {

return true;

}

return super.onOptionsItemSelected(item);

}

}

2、java 调用js

2.1、java代码,webView为WebView对象

// 无参数调用

webView.loadUrl("javascript:javacalljs()");

// 传递参数调用

webView.loadUrl("javascript:javacalljswithargs(" + "‘hello world‘" + ")");

2.2、js代码

function javacalljs(){

document.getElementById("content").innerHTML +=

"
java调用了js函数";

}

function javacalljswithargs(arg){

document.getElementById("content").innerHTML +=

("
"+arg);

}

原文:http://cbg23.blog.51cto.com/7201812/1701539

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值