我无法在 Activity 使用 Intent
I can't use Intent at Activity
当我点击 Google 地图 上的标记时,我希望它进入另一个 Activity,但是当我从 javascript 得到结果时,它不能执行 Intent
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");
});
传递当前上下文而不是 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
}
}
当我点击 Google 地图 上的标记时,我希望它进入另一个 Activity,但是当我从 javascript 得到结果时,它不能执行 Intent
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");
});
传递当前上下文而不是 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
}
}