在javascript web视图中调用Intent
我使用这段代码使用HTML按钮调用Android活动。但这对我不起作用。当我在html页面中单击按钮时,我只想将我的Android活动称为“echos.class” 我的JavaScriptInterface类在javascript web视图中调用Intent,javascript,android,android-intent,Javascript,Android,Android Intent,我使用这段代码使用HTML按钮调用Android活动。但这对我不起作用。当我在html页面中单击按钮时,我只想将我的Android活动称为“echos.class” 我的JavaScriptInterface类 public class JavaScriptInterface { Context mContext; /** Instantiate the interface and set the context */ JavaScriptInterface(Conte
public class JavaScriptInterface {
Context mContext;
/** Instantiate the interface and set the context */
JavaScriptInterface(Context c) {
mContext = c;
}
/** Show a toast from the web page */
public void showToast(String toast) {
Intent mainIntent = new Intent(mContext, echos.class);
JavaScriptInterface.this.startActivity(mainIntent);
}
private void startActivity(Intent mainIntent) {
// TODO Auto-generated method stub
}
}
我的网络视图类
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
WebView myWebView = (WebView) findViewById(R.id.web1);
WebSettings webSettings = myWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
myWebView.addJavascriptInterface(new JavaScriptInterface(this), "Android");
myWebView.loadUrl("file:///android_asset/www/index.html");
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
我的html页面
<html>
<head>
</head>
<body>
<input type="button" value="Say hello" onClick="showAndroidToast('Hello Android!')" />
<script type="text/javascript">
function showAndroidToast(toast) {
Android.showToast(toast);
}
</script>
</body>
</html>
函数showAndroidToast(toast){
showToast(toast);
}
更改
JavaScriptInterface.this.startActivity(mainIntent);
到
由于JavaScriptInterface不是活动您的方法在接口JavaScriptInterface中的startActivity为空。启动活动mContext.startActivity(mainIntent);我希望答案更清楚。@Athul Harikumar,你很棒。
mContext.startActivity(mainIntent);