Android和Javascript的交互運作
Android和Javascript的交互運作
由於覺得該技術很有趣,所以爬了一下文章,並自己撰寫一次,趕緊備份下來,歡迎有興趣的同好藝起來C/P一下。
資料來源:http://www.ways2u.com/?post=201
android.html
////////////
<html>
<head>
<metahttp-equiv="Content-Type"content="text/html; charset=utf-8"/>
<script language="javascript" type="text/javascript">
function get4Android(str)
{
document.getElementById("show").innerHTML="This is a message from android:"+str;
}
function send2Android()
{
var str = document.getElementById("mess").value;
window.myjs.runOnAndroidJavaScript(str);
}
</script>
</head>
<body>
<inputtype="text"id="mess"/>
<inputtype="button"value="Send To Android"onclick="send2Android()"/>
<divid="show"></div>
</body>
</html>
activity_main.xml
///////////////////////////////
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <EditText android:id="@+id/txt" android:layout_width="fill_parent" android:layout_height="wrap_content" /> <Button android:id="@+id/btn" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="Send To JavaScript" /> <TextView android:id="@+id/show" android:layout_width="fill_parent" android:layout_height="wrap_content" /> <WebView android:id="@+id/wv" android:layout_width="fill_parent" android:layout_height="fill_parent" /> </LinearLayout>
MainActivity.java
/////////////////////////////////
package com.example.android_java_javascript;
import com.example.android_java_javascript.MainActivity;
import android.os.Bundle;
import android.os.Handler;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
@SuppressLint("JavascriptInterface")
public class MainActivity extends Activity {
private EditText txt;
private WebView wv;
private Button btn;
private Handler h = new Handler();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
txt = (EditText) findViewById(R.id.txt);
wv = (WebView) findViewById(R.id.wv);
btn = (Button) findViewById(R.id.btn);
WebSettings webSettings = wv.getSettings();
webSettings.setJavaScriptEnabled(true);
webSettings.setSaveFormData(false);
webSettings.setSavePassword(false);
webSettings.setSupportZoom(false);
wv.addJavascriptInterface(new runJavaScript(), "myjs");
//myjs是自己定義的,供javascript訪問的介面
String url = "file:///android_asset/html/android.html";
wv.loadUrl(url);
btn.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v)
{
//調用javascript的函數get4Android(str)
Toast.makeText(MainActivity.this, "APP click... ", Toast.LENGTH_SHORT).show();
wv.loadUrl("javascript:get4Android('"+ txt.getText().toString() + "')");
}
});
}
//The Java object that is bound runs in another thread and not in the thread that it was constructed in.文檔的一句話!
final class runJavaScript
{
//這個Java 物件是綁定在另一個線程裏的,
public void runOnAndroidJavaScript(final String str)
{
h.post(new Runnable()
{
@Override
public void run()
{
//這裏應該特別注意的
TextView show = (TextView) findViewById(R.id.show);
show.setText("This is a message from javascript:"+str);
}
});
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}