最近工作太忙了,根本没时间写博客,今天在工作上遇到了一个问题,找到了临时解决的办法,这边分享出来,也给自己留个纪念。
android与js交互越来越时尚了,但是我们通常会用webview与其互通。有些时候,项目中根本不需要用到webview,那怎么办呢?今天我就是为了这个问题,去思考,去找答案。
自己摸索了一下午,终于有了答案,以下我会把代码贴出来,与大家分享:
xml文件:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity">
<TextView
android:id="@android:id/text1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />
<TextView
android:id="@android:id/text2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dip"
android:text="@string/hello_world" />
</LinearLayout>
没什么东西,两个TextView文本,这两个东西,我是用来显示与js交互后获取的数据,先准备放在这边。
主要关键代码:
package com.nmbb.example.sampleexecutejs;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
import org.mozilla.javascript.Context;
import org.mozilla.javascript.Function;
import org.mozilla.javascript.NativeJavaObject;
import org.mozilla.javascript.NativeObject;
import org.mozilla.javascript.Scriptable;
import org.mozilla.javascript.ScriptableObject;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView text1 = (TextView) findViewById(android.R.id.text1);
TextView text2 = (TextView) findViewById(android.R.id.text2);
String i = "哈哈,你真吊!!";
text1.setText(runScript(JAVA_CALL_JS_FUNCTION, "b", new Object[]{i}));
text2.setText(runScript(JS_CALL_JAVA_FUNCTION, "Test", new String[]{}));
}
/**
* Java执行js的方法
*/
private static final String JAVA_CALL_JS_FUNCTION = "var b=function(aa){ return 'allen:'+aa; }";
/**
* js调用Java中的方法
*/
private static final String JS_CALL_JAVA_FUNCTION = //
"var ScriptAPI = java.lang.Class.forName(\"" + MainActivity.class.getName() + "\", true, javaLoader);" + //
"var methodRead = ScriptAPI.getMethod(\"jsCallJava\", [java.lang.String]);" + //
// "var url=\"android\""+
"function jsCallJava(url) {return methodRead.invoke(null, url);}" + //
"function Test(){ return jsCallJava(); }";
/**
* 执行JS
*
* @param js js代码
* @param functionName js方法名称
* @param functionParams js方法参数
* @return
*/
public String runScript(String js, String functionName, Object[] functionParams) {
Context rhino = Context.enter();
rhino.setOptimizationLevel(-1);
Object result = null;
try {
Scriptable scope = rhino.initStandardObjects();
ScriptableObject.putProperty(scope, "javaContext", Context.javaToJS(MainActivity.this, scope));
ScriptableObject.putProperty(scope, "javaLoader", Context.javaToJS(MainActivity.class.getClassLoader(), scope));
rhino.evaluateString(scope, js, "MainActivity", 1, null);
Function function = (Function) scope.get(functionName, scope);
result = function.call(rhino, scope, scope, functionParams);
if (result instanceof String) {
return (String) result;
} else if (result instanceof NativeJavaObject) {
return (String) ((NativeJavaObject) result).getDefaultValue(String.class);
} else if (result instanceof NativeObject) {
return (String) ((NativeObject) result).getDefaultValue(String.class);
}
Context.exit();
} catch (Exception e) {
}
return result.toString();//(String) function.call(rhino, scope, scope, functionParams);
}
public static String jsCallJava(String url) {
return "allen 帅呆了!!";
}
}
runScript()是最重要的方法,里面承载了java call js或js call java方法的程序,但是在此之前需要引入一个jar:
compile 'rhino:js:1.7R2'
后来上网搜了一下这个人rhino,没什么话可说的,牛逼人物,下方是这个大牛的github地址:
rhino的仓库地址
运行结果:
请注意下这两句:
text1.setText(runScript(JAVA_CALL_JS_FUNCTION, "b", new Object[]{i}));
text2.setText(runScript(JS_CALL_JAVA_FUNCTION, "Test", new String[]{}));
传递的js代码和执行的js的方法名称,多式多样的,大家可以去尝试,这边给大家提个醒。
以下附上demo的下载地址: