当我点击 Google地图上的标记时,我希望它输入另一个活动,但是当我从 javascript 获得结果时,它不能做意图
WebSettings webSetting = webView.getSettings();
webSetting.setJavaScriptEnabled(true);
webSetting.setDisplayZoomControls(true);
webSetting.setSupportZoom(true);
webSetting.setBuiltInZoomControls(true);
webView.addJavascriptInterface(new JavaScriptInterfaceTest(this), "Android");
和我的JavaScriptInterfaceTest()
:
public class JavaScriptInterfaceTest {
Context mContext;
/** Instantiate the interface and set the context */
JavaScriptInterfaceTest(Context c) {
mContext = c;
}
/** Show a toast from the web page */
@JavascriptInterface
public void responseResult(String result){
Log.e("JsCallBack","DRINK");
Intent mainIntent = new Intent(MainActivity.this, Test.class);
startActivity(mainIntent);
}
@JavascriptInterface
private void startActivity(Intent mainIntent) {
// TODO Auto-generated method stub
}
}
HTML:
drinkMarker = new google.maps.Marker({
position: {lat: 25.079734,lng: 121.569519},
map: map,
});
drinkMarker.addListener('click',function(){
Android.responseResult("drink");
});
答案 0 :(得分:4)
传递当前上下文而不是MainActivity
public class JavaScriptInterfaceTest {
Context mContext;
/** Instantiate the interface and set the context */
JavaScriptInterfaceTest(Context c) {
mContext = c;
}
/** Show a toast from the web page */
@JavascriptInterface
public void responseResult(String result){
Log.e("JsCallBack","DRINK");
Intent mainIntent = new Intent(mContext, Test.class);
mContext.startActivity(mainIntent);
}
@JavascriptInterface
private void startActivity(Intent mainIntent) {
// TODO Auto-generated method stub
}
}