Hybird App
最近遇到一个需求,产品经理要求Android客户端点击“积分商城”调用H5页面的时候,Android和Js可以相互调用方法。这就涉及到HTML和Android之间的交互和通讯。作为Android实习生,虽然是第一次接触,且产品不像以前比赛项目那样,这次是面向消费者人群。于是乎,一顿猛操作…“度娘”[hhhhh~]。大概了解之后,一开始看的资料全是用内嵌的WebView。但是项目中用的是开源框架AgentWeb,且很多地方都涉及到调用相关方法。接着就开始了AgentWeb 的流浪之旅。buBBle~
具体看以下代码:
import android.content.Context;
import android.os.Handler;
import android.os.Looper;
import android.util.Log;
import android.webkit.JavascriptInterface;
import android.widget.Toast;
import com.just.agentweb.AgentWeb;
import org.json.JSONObject;
//定义一个AndroidInterface类 方便调用、管理
public class AndroidInterface {
private AgentWeb agent;
private Context context;
public AndroidInterface(AgentWeb agent, Context context) {
this.agent = agent;
this.context = context;
}
private Handler deliver = new Handler(Looper.getMainLooper());
@JavascriptInterface
public void callAndroid(final String msg) {
deliver.post(new Runnable() {
@Override
public void run() {
Log.i("Info", "main Thread:" + Thread.currentThread());
Toast.makeText(context.getApplicationContext(), "" + msg, Toast.LENGTH_LONG).show();
}
});
Log.i("Info", "Thread:" + Thread.currentThread());
}
//这里是你要传的参数给Js
@JavascriptInterface
public String getJson(){
String result="";
try {
JSONObject mJSONObject=new JSONObject();
mJSONObject.put("phone_number", "12763912321");
mJSONObject.put(

最低0.47元/天 解锁文章
740

被折叠的 条评论
为什么被折叠?



