package cn.itcast.web;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.webkit.WebSettings;
import android.webkit.WebView;
public class DemoActivity extends Activity {
private WebView webView;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
webView = (WebView) this.findViewById(R.id.webview);
//相当于创建了一个浏览器
WebSettings settings = webView.getSettings(); // 得到浏览器的设置
settings.setJavaScriptEnabled(true);
webView.addJavascriptInterface(new Object(){
public void callPhone(){
System.out.println("callphone");
Intent intent = new Intent();
intent.setAction(Intent.ACTION_CALL);
intent.setData(Uri.parse("tel:1351234567"));
startActivity(intent);
}
}, "demo");
String url = getResources().getString(R.string.serverurl);
webView.loadUrl(url);
}
public void calljavascript(View view){
webView.loadUrl("javascript:fillContent()");
}
}
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="调用javascript"
android:onClick="calljavascript"
>
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/webview"
/>
服务器端代码:
function fillContent(){
document.getElementById("content").innerHTML =
"java调用javascript哈哈,这些话事javascript搞出来的";
}
java和javascript相互调用