import com.codename1.impl.android.AndroidNativeUtil;
in my codename1 lib there is NO com.impl.android
what did i miss? where can i get my Activity?
Activity activity = AndroidNativeUtil.getActivity();
activity.runOnUiThread(new Runnable() {
@Override
public void run() {
// your code here
}
});
package userclasses;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.speech.RecognitionListener;
import android.speech.RecognitionService;
import android.speech.RecognizerIntent;
import android.speech.SpeechRecognizer;
import android.util.Log;
import java.util.ArrayList;
import java.util.Locale;
import com.codename1.impl.android.AndroidNativeUtil;
import com.codename1.ui.Dialog;
import com.codename1.ui.Label;
import com.codename1.ui.geom.Rectangle;
import generated.StateMachineBase;
public class MyNativeStuffImpl {
String speech = "";
private SpeechRecognizer sr;
private static final String TAG = "MyStt3Activity";
class listener implements RecognitionListener {
public void onReadyForSpeech(Bundle params) {
Log.d(TAG, "onReadyForSpeech");
}
public void onBeginningOfSpeech() {
Log.d(TAG, "onBeginningOfSpeech");
}
public void onRmsChanged(float rmsdB) {
Log.d(TAG, "onRmsChanged");
}
public void onBufferReceived(byte[] buffer) {
Log.d(TAG, "onBufferReceived");
}
public void onEndOfSpeech() {
Log.d(TAG, "onEndofSpeech");
new Dialog("endOfSpeech").showPopupDialog(new Rectangle());
}
public void onError(int error) {
Log.d(TAG, "error " + error);
}
public void onResults(Bundle results) {
String str = new String();
Log.d(TAG, "onResults " + results);
ArrayList data = results.getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION);
for (int i = 0; i < data.size(); i++) {
Log.d(TAG, "result " + data.get(i));
str += data.get(i);
}
speech = str;
lbl.setText("i got: "+speech);
}
public void onPartialResults(Bundle partialResults) {
Log.d(TAG, "onPartialResults");
new Dialog("partialResults").showPopupDialog(new Rectangle());
}
public void onEvent(int eventType, Bundle params) {
Log.d(TAG, "onEvent " + eventType);
}
}
public String getTextFromSpeech() {
final Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, Locale.getDefault());
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_PREFERENCE, "en");
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
RecognizerIntent.LANGUAGE_MODEL_WEB_SEARCH);
intent.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS, 3);
Activity activity = AndroidNativeUtil.getActivity();
activity.runOnUiThread(new Runnable() {
@Override
public void run() {
sr = SpeechRecognizer.createSpeechRecognizer(AndroidNativeUtil.getActivity());
sr.setRecognitionListener(new listener());
sr.startListening(intent);
}
});
return "waiting for input\n" + speech;
}
public String getText() {
sr.stopListening();
return "listened: " + speech;
}
public boolean isSupported() {
return true;
}
}
/**
* Your application code goes here
*/
package userclasses;
import android.os.Looper;
import com.codename1.system.NativeLookup;
import com.codename1.ui.*;
import com.codename1.ui.events.*;
import com.codename1.ui.geom.Rectangle;
import com.codename1.ui.util.Resources;
import generated.StateMachineBase;
/**
*
* @author Your name here
*/
public class StateMachine extends StateMachineBase {
private MyNativeStuff s2t;
int first = 0;
public StateMachine(String resFile) {
super(resFile);
// do not modify, write code in initVars and initialize class members there,
// the constructor might be invoked too late due to race conditions that might occur
}
/**
* this method should be used to initialize variables instead of the
* constructor/class scope to avoid race conditions
*
* @param res
*/
@Override
protected void initVars(Resources res) {
}
@Override
protected void onMain_ButtonAction(Component c, ActionEvent event) {
//create Class
s2t = (MyNativeStuff) NativeLookup.create(MyNativeStuff.class);
//2 Functions on one Button
if (first == 1) {
//Show result
findLabel().setText(s2t.getText());
} else if (s2t != null && s2t.isSupported()) {
//invoke Speechrecognition
s2t.getTextFromSpeech();
first = 1;
findLabel().setText("waiting");
}
}
}
package userclasses;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.speech.RecognitionListener;
import android.speech.RecognizerIntent;
import android.speech.SpeechRecognizer;
import android.util.Log;
import java.util.ArrayList;
import java.util.Locale;
import com.codename1.impl.android.AndroidNativeUtil;
public class MyNativeStuffImpl {
//maybe use a getter for message to get the last event
static String message = "";
static String speech = "";
private SpeechRecognizer sr;
class listener implements RecognitionListener {
public void onReadyForSpeech(Bundle params) {
message = "onReadyForSpeech";
}
public void onBeginningOfSpeech() {
message = "onBeginningOfSpeech";
}
public void onRmsChanged(float rmsdB) {
message = "onRmsChanged";
}
public void onBufferReceived(byte[] buffer) {
message = "onBufferReceived";
}
public void onEndOfSpeech() {
message = "onEndofSpeech";
}
public void onError(int error) {
message = "error " + error;
}
public void onResults(Bundle results) {
//here you have what google understand from the speech
//maybe only save the first guess, which would have the highest
//possibility
String str = "";
ArrayList data = results.getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION);
for (int i = 0; i < data.size(); i++) {
str += data.get(i)+";";
}
speech = str;
}
public void onPartialResults(Bundle partialResults) {
message = "onPartialResults";
}
public void onEvent(int eventType, Bundle params) {
message = "onEvent " + eventType;
}
}
public void getTextFromSpeech() {
//initialize the Intent
final Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, Locale.getDefault());
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
RecognizerIntent.LANGUAGE_MODEL_WEB_SEARCH);
//number of guesses
intent.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS, 3);
//Speechrecognition must be run on main Thread
Activity activity = AndroidNativeUtil.getActivity();
activity.runOnUiThread(new Runnable() {
@Override
public void run() {
sr = SpeechRecognizer.createSpeechRecognizer(AndroidNativeUtil.getActivity());
sr.setRecognitionListener(new listener());
sr.startListening(intent);
}
});
}
public String getText() {
return speech;
}
public boolean isSupported() {
return true;
}
}