Android平台,怎么调用javascript操作网页和js调用系统功能 如何在android平台上使用js直接调用Java方法
\u5982\u4f55\u5728Android\u5e73\u53f0\u4e0a\u4f7f\u7528JS\u76f4\u63a5\u8c03\u7528Java\u65b9\u6cd5\u597d\u4e86\u4e0a\u4ee3\u7801
/*JsInterface.java*/
/*\u8be5\u7c7b\u662fJS\u8c03\u7528JAVA\u7aef\u7684,JAVA\u7aef\u8c03\u7528webview\u4e2d\u7684JS\u5f88\u7b80\u5355\uff0c\u540e\u9762\u4ee3\u7801\u5c06\u7ed9\u51fa*/
[java] view plaincopy
public class JsInterface {
/*interface for javascript to invokes*/
public interface wvClientClickListener {
public void wvHasClickEnvent();
}
private wvClientClickListener wvEnventPro = null;
public void setWvClientClickListener(wvClientClickListener listener) {
wvEnventPro = listener;
}
public void javaFunction() {
if(wvEnventPro != null)
wvEnventPro.wvHasClickEnvent();
}
}
/*Js2JavaActivity.JAVA*/
/*\u7a0b\u5e8f\u5165\u53e3*/
[java] view plaincopy
public class Js2JavaActivity extends Activity{
private Button btn_show,btn_hide;
private WebView wv;
private JsInterface JSInterface2 = new JsInterface();
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.js2java);
btn_show = (Button)findViewById(R.id.btn_java2js_show);
btn_hide = (Button)findViewById(R.id.btn_java2js_hide);
wv = (WebView)findViewById(R.id.wv_js2java);
wv.getSettings().setJavaScriptEnabled(true);
wv.addJavascriptInterface(JSInterface2,"JSInterface2");
wv.setWebViewClient(new webviewClient());
wv.loadUrl("file:///android_asset/index.html");
}
class webviewClient extends WebViewClient {
@Override
public void onPageFinished(WebView view, String url) {
// TODO Auto-generated method stub
super.onPageFinished(view, url);
btn_show.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Toast.makeText(getBaseContext(), "You click show button", 1000).show();
wv.loadUrl(String.format("javascript:java2js(0)"));//\u8fd9\u91cc\u662fjava\u7aef\u8c03\u7528webview\u7684JS
}
});
btn_hide.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Toast.makeText(getBaseContext(), "You click hide button", 1000).show();
wv.loadUrl(String.format("javascript:java2js(1)"));//\u8fd9\u91cc\u662fjava\u7aef\u8c03\u7528webview\u7684JS
}
});
JSInterface2.setWvClientClickListener(new webviewClick());//\u8fd9\u91cc\u5c31\u662fjs\u8c03\u7528java\u7aef\u7684\u5177\u4f53\u5b9e\u73b0
}
}
class webviewClick implements wvClientClickListener {
@Override
public void wvHasClickEnvent() {
// TODO Auto-generated method stub
Toast.makeText(getBaseContext(), "link be on click", 1000).show();
}
}
}
/*\u597d\u5427\uff0c\u7167\u987e\u7801\u519c\u9700\u8981\uff0c\u5c06\u5e03\u5c40\u7684\u4e5f\u8d34\u4e0a*/
[html] view plaincopy
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_weight="1">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/btn_java2js_show"
android:text="click_show"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/btn_java2js_hide"
android:text="click_hide"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_weight="1">
<WebView
android:id="@+id/wv_js2java"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
/*\u6700\u540e\u662fHTML\u6587\u4ef6*/
[html] view plaincopy
function java2js(isHide) {
var x = document.getElementById('div');
if(isHide==0)
x.className = "";
else
x.className = "hide";
}
function js2java() {
JSInterface2.javaFunction();
}
.hide {
display:none;
}
js2java
this is a hide div~~~~
\u4e4b\u524d\u9879\u76ee\u4e5f\u6709\u505a\u8fc7\uff0c\u4eca\u65e5\u6709\u7a7a\u5c31\u590d\u4e60\u4e00\u4e0b....\u4f46\u72af\u4e86\u4e00\u4e2a\u5f88\u4f4e\u7ea7\u9519\u8bef\uff0c\u7adf\u7136\u628aJSInterface\u7c7b\u7684\u6210\u5458\u7ed9static\u4e86\uff0c\u540e\u9762\u8fd8wv.addJavascriptInterface(new JSInterface(),"JSInterface2"); \u6240\u4ee5\u540e\u9762javascript\u8c03\u7528java\u5c31\u51fa\u73b0\u629b\u51faCloneNotSupportedException------\u201c"Class doesn't implement Cloneable"\u201d\u3002\u3002\u597d\u4e86\u4e0a\u4ee3\u7801
/*JsInterface.java*/
/*\u8be5\u7c7b\u662fJS\u8c03\u7528JAVA\u7aef\u7684,JAVA\u7aef\u8c03\u7528webview\u4e2d\u7684JS\u5f88\u7b80\u5355\uff0c\u540e\u9762\u4ee3\u7801\u5c06\u7ed9\u51fa*/
[java] view plaincopy
public class JsInterface {
/*interface for javascript to invokes*/
public interface wvClientClickListener {
public void wvHasClickEnvent();
}
private wvClientClickListener wvEnventPro = null;
public void setWvClientClickListener(wvClientClickListener listener) {
wvEnventPro = listener;
}
public void javaFunction() {
if(wvEnventPro != null)
wvEnventPro.wvHasClickEnvent();
}
}
/*Js2JavaActivity.JAVA*/
/*\u7a0b\u5e8f\u5165\u53e3*/
[java] view plaincopy
public class Js2JavaActivity extends Activity{
private Button btn_show,btn_hide;
private WebView wv;
private JsInterface JSInterface2 = new JsInterface();
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.js2java);
btn_show = (Button)findViewById(R.id.btn_java2js_show);
btn_hide = (Button)findViewById(R.id.btn_java2js_hide);
wv = (WebView)findViewById(R.id.wv_js2java);
wv.getSettings().setJavaScriptEnabled(true);
wv.addJavascriptInterface(JSInterface2,"JSInterface2");
wv.setWebViewClient(new webviewClient());
wv.loadUrl("file:///android_asset/index.html");
}
class webviewClient extends WebViewClient {
@Override
public void onPageFinished(WebView view, String url) {
// TODO Auto-generated method stub
super.onPageFinished(view, url);
btn_show.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Toast.makeText(getBaseContext(), "You click show button", 1000).show();
wv.loadUrl(String.format("javascript:java2js(0)"));//\u8fd9\u91cc\u662fjava\u7aef\u8c03\u7528webview\u7684JS
}
});
btn_hide.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Toast.makeText(getBaseContext(), "You click hide button", 1000).show();
wv.loadUrl(String.format("javascript:java2js(1)"));//\u8fd9\u91cc\u662fjava\u7aef\u8c03\u7528webview\u7684JS
}
});
JSInterface2.setWvClientClickListener(new webviewClick());//\u8fd9\u91cc\u5c31\u662fjs\u8c03\u7528java\u7aef\u7684\u5177\u4f53\u5b9e\u73b0
}
}
class webviewClick implements wvClientClickListener {
@Override
public void wvHasClickEnvent() {
// TODO Auto-generated method stub
Toast.makeText(getBaseContext(), "link be on click", 1000).show();
}
}
}
/*\u597d\u5427\uff0c\u7167\u987e\u7801\u519c\u9700\u8981\uff0c\u5c06\u5e03\u5c40\u7684\u4e5f\u8d34\u4e0a*/
[html] view plaincopy
<LinearLayout xmlns:android="ht tp:/ /schemas.androi d.c om/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_weight="1">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/btn_java2js_show"
android:text="click_show"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/btn_java2js_hide"
android:text="click_hide"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_weight="1">
<WebView
android:id="@+id/wv_js2java"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
/*\u6700\u540e\u662fHTML\u6587\u4ef6*/
[html] view plaincopy
function java2js(isHide) {
var x = document.getElementById('div');
if(isHide==0)
x.className = "";
else
x.className = "hide";
}
function js2java() {
JSInterface2.javaFunction();
}
.hide {
display:none;
}
js2java
this is a hide div~~~~
预期效果:1、java编程实现显示一个网页显示(list,list中有电话号码),网页中的数据内容由程序传过去。
2、点击网页中的电话号码部分,调用手机的打电话界面。
具体实现:
1、在assets中定义index.html文件,这个文件中table中的数据由javascript生成
ps:
(1)contactlist(jsons)将由java程序调用
(2)onload="javascript:myjavascript.show():javascript调用java程序,详情见下文
2、下面是java程序代码,首先是布局文件,很简单,就是一个webview
4、MyJavaScript负责提供数据并显示html
至此,java程序与javascript之间的双向调用已经完成了。至于我们预期目标的第2项,只需要:
(1)、在MyJavaScript类中添加方法
Java代码
/*
* 拨打电话方法
*/
ublic void call(final String phone){
Intent intent = new Intent(Intent.ACTION_CALL,Uri.parse("tel:" + phone));
text.startActivity(intent);
}
/*
* 拨打电话方法
*/
public void call(final String phone){
Intent intent = new Intent(Intent.ACTION_CALL,Uri.parse("tel:" + phone));
context.startActivity(intent);
}
(2)、在index.html中将
Java代码
td3.innerHTML = jsonObj.phone;
td3.innerHTML = jsonObj.phone;
修改为:
Java代码
td3.innerHTML = "<a href=\"javascript:myjavascript.call('"+jsonObj.phone + "')\">" + jsonObj.phone + "</a>";
td3.innerHTML = "<a href=\"javascript:myjavascript.call('"+jsonObj.phone + "')\">" + jsonObj.phone + "</a>";
最后记得加上打电话的权限:
Java代码
<uses-permission android:name="android.permission.CALL_PHONE"></uses-permission>
绛旓細绗竴姝ワ細浣跨敤Java缂栧啓HelloWorld 鐨Android搴旂敤绋嬪簭锛歱ackage com.lucyfyr;import android.app.Activity;import android.os.Bundle;import android.util.Log;public class HelloWorld extends Activity {/** Called when the activity is first created. */@Overridepublic void onCreate(Bundle savedInstance...
绛旓細杩欐槸鍥犱负浣犵殑studio璁剧疆浜嗙渷鐢垫ā寮忥紝浣犲彲浠ラ氳繃 File>Power Save Mode鍙栨秷鎺
绛旓細JPushInterface.setAliasAndTags(context,鍞竴鏍囩ず, 鍒嗙被鏍囩ず, new TagAliasCallback() { Override public void gotResult(int arg0, String arg1,Set<String> arg2) { Log.i("JPush", "Jpush status: " + arg0);//鐘舵 } });杩欎釜鏂规硶鍙互鍦ㄤ富绾跨▼锛屽瓙绾跨▼娣诲姞锛屽綋鐘舵佷负0鏃讹紝鍗崇粦瀹氭垚鍔燂紝...
绛旓細jobject this,jint x,jint y ){ return first(x, y);} 绗簩姝ユ槸閲嶇偣锛屾垜浠潵鍒嗘瀽涓媘k鏂囦欢锛岀湅缂栬瘧鏂囦欢鏄鎬庢牱鐢熸垚first.c瀵逛簬鐨勯潤鎬佹枃浠讹紝骞跺湪缂栬瘧second.c鐨勬椂鍊欏姞杞介潤鎬佹枃浠 1: LOCAL_PATH:= $(call my-dir)2:3: # first lib, which will be built statically 4:5: includ...
绛旓細濡備綍灏咼2ME涓婇潰鐨勬父鎴忔敼鍒癆ndroid涓婇潰 鎴戞兂灏咼2me涓婇潰鐨勬父鎴忕Щ妞嶅埌Android骞冲彴涓婇潰杩愯,涓嶇煡閬撻渶瑕佹敼鍔ㄥ摢浜涗笢瑗,杩樿澶у鎸囨暀銆... 鎴戞兂灏咼2me涓婇潰鐨勬父鎴忕Щ妞嶅埌Android骞冲彴涓婇潰杩愯,涓嶇煡閬撻渶瑕佹敼鍔ㄥ摢浜涗笢瑗,杩樿澶у鎸囨暀銆 灞曞紑 鎴戞潵绛 2涓洖绛 #鐑# 鐢熸椿涓湁鍝簺鎴愮樉椋熺墿? rflame 2010-10-24 鐭ラ亾...
绛旓細涔熷氨鏄垜浠湪涓婇潰鍒涘缓鐨勪富UnityTestActivity.JAVA銆傛嬁鍒板畠鐨勫璞″悗璋冪敤jo.Call锛堬級鍙傛暟1琛ㄧず璋冪敤UnityTestActivity.JAVA绫讳腑鐨勬柟娉曞悕绉帮紝鍙傛暟2琛ㄧず璇ユ柟娉曚紶閫掕繃鍘荤殑鍙傛暟銆傚涓嬪浘鎵绀猴細鈥滅涓涓狝ctivity鈥濅笌鈥滅浜屼釜Activit鈥濆氨鏄垜鍦–#涓紶閫掕繃鍘荤殑瀛楃涓层7 鍙戝竷鎴android骞冲彴apk ...
绛旓細JPush鏋佸厜鎺ㄩ佸彲浠ュ彧鎺ㄩ佺粰鎸囧畾鐨勯偅閮ㄥ垎android鐢ㄦ埛锛岄氳繃浣跨敤鏍囩锛屽埆鍚嶏紝Registration ID 鍜岀敤鎴峰垎缇わ紝寮鍙戣呭彲浠ュ悜鐗瑰畾鐨勪竴涓垨澶氫釜鐢ㄦ埛鎺ㄩ佹秷鎭侸Push 鏄粡杩囪冮獙鐨勫ぇ瑙勬ā App 鎺ㄩ骞冲彴锛姣忓ぉ鎺ㄩ佹秷鎭噺绾т负鏁扮櫨浜挎潯銆 寮鍙戣呴泦鎴 SDK 鍚庯紝鍙互閫氳繃璋冪敤 API 鎺ㄩ佹秷鎭傚悓鏃讹紝JPush 鎻愪緵鍙鍖栫殑 web...
绛旓細Android studio涓粯璁や娇鐢ㄧ殑鏄痝radle缂栬瘧鏂瑰紡锛屼笌ADT缂栬緫鏂瑰紡涓嶄竴鏍凤紝閭d箞so鏂囦欢搴斿綋濡備綍寮曞叆鍛紵鍏跺疄寰堢畝鍗曘傝繖閲屼互闆嗘垚JPUSH涓轰緥锛岀湅涓涓媠o鏂囦欢濡備綍寮曞叆鍒扮紪璇戠幆澧冿紝鏈缁堝埌JNI鐩存帴鍙互璋冪敤璇o鏂囦欢銆傞鍏堬紝鍦ㄦ垜浠殑Module鐨勬牴鐩綍涓缓绔媗ibs鐩綍锛屽皢jpush闆嗘垚SDK涓殑so鏂囦欢鍒嗗埆鎷峰叆锛屾埅鍥惧涓嬶細鐒跺悗灏辨槸...
绛旓細浣犻兘宸茬粡鍐欐柟娉曠殑杩斿洖鍊间簡锛屽氨鐩存帴return 杩欎釜jbyteArray銆俲ava绔氨鑳芥帴鍙楀埌
绛旓細娉ㄥ唽搴旂敤 1 杩涘叆瀹樼綉 棣栧厛杩涘叆瀹樼綉棣栭〉娉ㄥ唽璐﹀彿 2 娉ㄥ唽搴旂敤 鐧诲綍鍒扮敤鎴骞冲彴锛鐐瑰嚮鍒涘缓搴旂敤濡傚浘鎵绀:3 鍖呭悕锛氬垱寤哄簲鐢ㄩ」鐩殑鍩虹鍖咃紝鍗曞嚮鍒涘缓锛屼骇鐢熷簲鐢ㄤ俊鎭鍥炬墍绀猴細鐜鎼缓 1 SDK涓嬭浇 涓嬭浇 JPUSH Android 鈥 SDK銆2 瀵煎叆SDK寮鍙戝寘 澶嶅埗 libs/jpush-sdk-release1.x.y.jar 鍒板伐绋 libs/ 鐩綍涓 澶...