您的位置:首页 > 移动开发 > Android开发

Android java 与 javascript互访(相互调用)的方法例子

2011-06-03 10:16 751 查看
转载请注明原贴地址:http://blog.csdn.net/feifei454498130/archive/2011/06/03/6524183.aspx





有时用java去控制javascript,可以控制整个WebView,或者是flash播放器。而用javascript访问java则可以起一个回调的作用或者其它。

以下是一个简单的例子,大家看一下就明白了。

新建一个index.html文件保存到assets/sample文件夹下



<html>
<head>
<meta http-equiv="content-type" content="text/html;charset=UTF-8">
<title> this is a test...</title>
</head>
<body>
<script type="text/javascript">
function javaWork(str) {
document.getElementById("output").innerHTML = str;
}
</script>
<a href="javascript:myfunction.show('这是来自javascript的参数')">Click here~</a><br>
<a href="javascript:myfunction.DumpActivity('com.android.settings', 'com.android.settings.Settings')">跳到setting的Activity~</a><br>
<div id="output"></div>
</body>



res/layout/main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<WebView
	android:id="@+id/web_01"
	android:layout_width="fill_parent"
	android:layout_height="fill_parent"
	/>
</LinearLayout>






以下是java代码



import android.app.Activity;
import android.content.ComponentName;
import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnKeyListener;
import android.view.Window;
import android.view.WindowManager;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebSettings.PluginState;
import android.widget.Toast;
public class FlashActivity extends Activity {
    private WebView mWebView;
    private String url = "";
	
	/** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        setContentView(R.layout.main);
        initWebView ();
    }
    
    private void initWebView () {
        url = "file:///android_asset/sample/index.html";
        mWebView = (WebView)findViewById(R.id.web_01);
        mWebView.clearCache(true);
        mWebView.setOnKeyListener(new keyListener());
        WebSettings wSetting = mWebView.getSettings();
        // support for javascript
        wSetting.setJavaScriptEnabled(true);
        wSetting.setPluginState(PluginState.ON);
        
        // these will access by javascript
        mWebView.addJavascriptInterface(new Object() {
        	public void show(String str) {
        		showToast(str);
        		
        	}
        	public void DumpActivity(String packet, String activityName) {
        		Intent intent = new Intent(Intent.ACTION_MAIN,null);
        		intent.addCategory(Intent.CATEGORY_LAUNCHER);
        		ComponentName cn = new ComponentName(packet, activityName);
        		intent.setComponent(cn);
        		intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
        		startActivity(intent);
        	}
        }, "myfunction");
        mWebView.loadUrl(url);
    }
    
	/** show toast, just for test**/
	private void showToast(String str){
		Toast.makeText(this, str, Toast.LENGTH_SHORT).show();
	}
	class keyListener implements OnKeyListener {
		public boolean onKey(View v, int keyCode, KeyEvent event) {
			return onKeyDown(keyCode, event);
		}
		
	}
	// key listener
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		switch(keyCode){
		case KeyEvent.KEYCODE_MENU:
			 mWebView.loadUrl("javascript:javaWork('参数是来自java的~')");
			break;
		case KeyEvent.KEYCODE_BACK:
			onDestroy();
			break;
		}
		return super.onKeyDown(keyCode, event);
	}
	protected void onResume() {
		super.onResume();
	}
	protected void onPause() {
		super.onPause();
	}
	protected void onDestroy() {
		super.onDestroy();
		// kill the process
		android.os.Process.killProcess(android.os.Process.myPid());
	}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: