모바일 웹을 제작 하고 있습니다.
WebView를 사용해서 index.html을 불러온 후 speech버튼을 누르면 안드로이드 음성인식 api를 실행하려 합니다.
근데 버튼을 누르면 바로 다운됩니다. 혹시 아시는 분은 답변 좀 부탁드립니다.
------------------------------------------------------------------------------
public class WebViewTestActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
WebView WebView = (WebView)this.findViewById(R.id.webview);
WebSettings webSettings = WebView.getSettings();
webSettings.setJavaScriptEnabled(true);
WebView.addJavascriptInterface(new MyJavaScript(this), "MyJavaScript");
WebView.setWebViewClient(new MyWebViewClient());
WebView.loadUrl("file:///android_asset/index.html);
}
}
------------------------------------------------------------------------------
public class MyWebViewClient extends WebViewClient {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (Uri.parse(url).getHost().equals("127.0.0.1")) {
return false;
}
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
view.getContext().startActivity(intent);
return true;
}
}
------------------------------------------------------------------------------
public class MyJavaScript extends Activity {
Context context;
private static final int REQUEST_CODE = 0;
public MyJavaScript() {
// TODO Auto-generated constructor stub
}
public MyJavaScript(Context context) {
this.context = context;
}
public void showToast(String message) {
Toast.makeText(context, message, Toast.LENGTH_LONG).show();
}
public void onCon(){
AlertDialog alert = new AlertDialog.Builder( this )
.setIcon( R.drawable.ic_launcher )
.setTitle( "AlertTitle" )
.setMessage( "AlertMessage" )
.setPositiveButton( "OK", new DialogInterface.OnClickListener(){
public void onClick(DialogInterface dialog, int which){
dialog.dismiss();
}})
.show();
}
public void onClick() { //이 method를 호출하고 싶은데 다운됩니다.
try {
Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
intent.putExtra(RecognizerIntent.EXTRA_PROMPT, "Recognize");
startActivityForResult(intent, REQUEST_CODE);
} catch (ActivityNotFoundException e) {
}
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == REQUEST_CODE && resultCode == RESULT_OK) {
String str = "";
ArrayList<String> results = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
str += results.get(0);
}
}
}
------------------------------------------------------------------------------
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>index</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<link rel="stylesheet" href="http://code.jquery.com/mobile/1.0.1/jquery.mobile-1.0.1.css" />
<script src="http://code.jquery.com/jquery-1.7.1.js"></script>
<script src="http://code.jquery.com/mobile/1.0.1/jquery.mobile-1.0.1.js"></script>
<script src="./index.js" type="text/javascript" charset="utf-8"></script>
</head>
<body>
<div data-role="page">
<div data-role="content">
<div data-role="controlgroup">
<a href="write.html" data-role="button"><h1>Write</h1></a> <a
href="index.html" data-role="button"><h1>Read</h1></a>
</div>
<a id="speechBtn" href="#" data-role="button" data-theme="b"><h1>Speech!</h1></a>
</div>
</div>
</body>
</html>
------------------------------------------------------------------------------
$(document).ready(function() {
$(document).on('click', '#speechBtn' , function(event){
MyJavaScript.onClick();
event.preventDefault();
});
});




MyJavaScript 에서 extends Activity 이걸 지워주세요
그리고 거기에 맞게 코드를 수정해주시면 됩니다.