写了个语音识别的程序,我想让我说“开灯”的时候发送一条命令,“关灯”的时候放送一条命令,程序如下package com.example.soundtest;import java.util.ArrayList;import android.R.string;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.speech.RecognizerIntent;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;public class MainActivity extends Activity {
/** Called when the activity is first created. */
private static final int RECOGNIZER_EXAMPLE=101;
private TextView result;
private TextView text;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
result=(TextView)findViewById(R.id.text_result);
text=(TextView)findViewById(R.id.text);
Button start=(Button)findViewById(R.id.start_button);
start.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent intent=new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
intent.putExtra(RecognizerIntent.EXTRA_PROMPT, "Say a word or phrase\nand it will show as text");
startActivityForResult(intent,RECOGNIZER_EXAMPLE);
}
});
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
if(requestCode==RECOGNIZER_EXAMPLE&&resultCode==RESULT_OK){
ArrayList<String> resultList=data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
StringBuffer resultListString=new StringBuffer();;
for(String s:resultList){
resultListString.append(s+",");
}
String str=resultList.get(0);
// if (str=="关灯"){
// Toast.makeText(this, "关灯", Toast.LENGTH_LONG).show();
// }
if (str=="开灯"){
Toast.makeText(this, "开灯", Toast.LENGTH_LONG).show();
} else {
Toast.makeText(this, "你妹", Toast.LENGTH_LONG).show();
}
result.setText(resultListString.toString());
text.setText(str);
}
super.onActivityResult(requestCode, resultCode, data);
}
}
现在问题就是我说开灯的时候,我用testview显示str确实是开灯,但toast一直显示的是“你妹”,不知道问题出在哪了。。
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.speech.RecognizerIntent;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;public class MainActivity extends Activity {
/** Called when the activity is first created. */
private static final int RECOGNIZER_EXAMPLE=101;
private TextView result;
private TextView text;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
result=(TextView)findViewById(R.id.text_result);
text=(TextView)findViewById(R.id.text);
Button start=(Button)findViewById(R.id.start_button);
start.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent intent=new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
intent.putExtra(RecognizerIntent.EXTRA_PROMPT, "Say a word or phrase\nand it will show as text");
startActivityForResult(intent,RECOGNIZER_EXAMPLE);
}
});
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
if(requestCode==RECOGNIZER_EXAMPLE&&resultCode==RESULT_OK){
ArrayList<String> resultList=data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
StringBuffer resultListString=new StringBuffer();;
for(String s:resultList){
resultListString.append(s+",");
}
String str=resultList.get(0);
// if (str=="关灯"){
// Toast.makeText(this, "关灯", Toast.LENGTH_LONG).show();
// }
if (str=="开灯"){
Toast.makeText(this, "开灯", Toast.LENGTH_LONG).show();
} else {
Toast.makeText(this, "你妹", Toast.LENGTH_LONG).show();
}
result.setText(resultListString.toString());
text.setText(str);
}
super.onActivityResult(requestCode, resultCode, data);
}
}
现在问题就是我说开灯的时候,我用testview显示str确实是开灯,但toast一直显示的是“你妹”,不知道问题出在哪了。。
Toast.makeText(this, "开灯", Toast.LENGTH_LONG).show();
} else {
Toast.makeText(this, "你妹", Toast.LENGTH_LONG).show();