我有一项服务,我试图从其中启动语音到文本引擎,但它不工作.. 是否可以从服务启动语音到文本?
MyService.java 类:
public class MyService extends Service{
OnActRes r;
EditText edittext;
//private Runnable m;
@Override
public IBinder onBind(Intent arg0) {
// TODO Auto-generated method stub
return null;
}
@Override
public void onCreate() {
// TODO Auto-generated method stubry
try
{
Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
Toast.makeText(MyService.this,"STEP 1 !", Toast.LENGTH_LONG).show();
intent.putExtra(RecognizerIntent.EXTRA_CALLING_PACKAGE, getClass().getPackage().getName());
Toast.makeText(MyService.this,"STEP 2 !", Toast.LENGTH_LONG).show();
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
Toast.makeText(MyService.this,"STEP 3 !", Toast.LENGTH_LONG).show();
//since we only want one, request 1
intent.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS, 1);
Toast.makeText(MyService.this,"STEP 4 !", Toast.LENGTH_LONG).show();
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, "en-US");
Toast.makeText(MyService.this,"STEP 5 !", Toast.LENGTH_LONG).show();
r.startActivityForResult(intent, 1234);
}
catch(Exception e)
{
Toast.makeText(MyService.this,"ERROR here !", Toast.LENGTH_LONG).show();
}
}
}
结果.java
public class OnActRes extends Activity {
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode != Activity.RESULT_OK){
{
Toast.makeText(OnActRes.this,"AR", Toast.LENGTH_LONG).show();
ArrayList<String> matches = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
String topResult = matches.get(0);
Toast.makeText(OnActRes.this,topResult, Toast.LENGTH_LONG).show();
}
}
}
}