我想了解如何改善我目前的设置。应用程序从FCM消息读取开始,并提示用户说出语音命令。
我没有收到任何错误,有时会有效,但不是一贯的。换句话说,有时程序会说话,有时却不会。你有什么建议我做的?
只需拥有以下内容:
public class MyFirebaseMessagingService extends com.google.firebase.messaging.FirebaseMessagingService {
private static final String TAG = "FirebaseMsgService";
TextToSpeech instanceTTS =null;
Context context;
String moveactivity;
Intent i;
SpeechRecognizer mRecognizer;
Intent Activity_intent;
RecognitionListener listener;
// [START receive_message]
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
// new OperationTTS().execute(remoteMessage.getData().get("TTS"));
instanceTTS =new TextToSpeech(getApplicationContext(), new SdnaOnInitListener(remoteMessage.getData().get("TTS")));
instanceTTS.setOnUtteranceProgressListener(new UtteranceProgressListener() {
@Override
public void onStart(String s) {
}
@Override
public void onDone(String s) {
get_voice();
instanceTTS.shutdown();
instanceTTS = null;
}
@Override
public void onError(String s) {
}
});
//추가한것
sendNotification(remoteMessage);
}
private void sendNotification(RemoteMessage remoteMessage) {
String message = remoteMessage.getData().get("message");
String title = remoteMessage.getData().get("title");
moveactivity = remoteMessage.getData().get("activity");
Activity_intent = SetActivity(moveactivity);//new Intent(this, MenuActivity.class);
Activity_intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, Activity_intent,
PendingIntent.FLAG_ONE_SHOT);
Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle(title)
.setContentText(message)
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setContentIntent(pendingIntent);
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
PowerManager pm = (PowerManager) this.getSystemService(Context.POWER_SERVICE);
PowerManager.WakeLock wakelock = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP, "TAG"); //화면 깨움
wakelock.acquire(5000);
notificationManager.notify(1231231 /* ID of notification */, notificationBuilder.build());
}
private void TextTTS(String message){
String text2 = message;
Log.d("Sdnalog", "TTS 말하는중?"+instanceTTS.isSpeaking());
//http://stackoverflow.com/a/29777304
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
Log.d("Sdnalog", "TTS진입2");
ttsGreater21(text2);
} else {
Log.d("Sdnalog", "TTS진입3");
ttsUnder20(text2);
}
instanceTTS.shutdown();
}
private void ttsUnder20(String text) {
HashMap<String, String> map = new HashMap<>();
map.put(TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID, "MessageId");
Log.d("Sdnalog", "TTS speak");
instanceTTS.speak(text, TextToSpeech.QUEUE_FLUSH, map);
}
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
private void ttsGreater21(String text) {
String utteranceId=this.hashCode() + "";
Log.d("Sdnalog", "TTS speak");
instanceTTS.speak(text, TextToSpeech.QUEUE_FLUSH, null, utteranceId);
}
public class SdnaOnInitListener implements TextToSpeech.OnInitListener
{
String msg;
public SdnaOnInitListener(String s) {
msg = s;
Log.d("Sdnalog", "SdnaOnInitListener 생성자");
}
@Override
public void onInit ( int status){
if (status == TextToSpeech.SUCCESS)
{
instanceTTS.setLanguage(Locale.KOREAN);
Log.d("Sdnalog", "초기화 완료");
TextTTS(msg);
}
}
}
@Override
public void onDestroy() {
Log.d("slog", "onDestroy()");
super.onDestroy();
//Toast toast = Toast.makeText(context, "서비스 onDestroy", Toast.LENGTH_SHORT);
//toast.show();
}
@Override
public void onCreate() {
super.onCreate();
Log.d("slog", "onCreate()");
context = getApplicationContext();
//Toast toast = Toast.makeText(context, "서비스 onCreate", Toast.LENGTH_SHORT);
// toast.show();
}
@Override
public void onStart(Intent intent, int startId) {
Log.d("slog", "onStart()");
super.onStart(intent, startId);
//Toast toast = Toast.makeText(context, "서비스 onStart", Toast.LENGTH_SHORT);
//toast.show();
}
public Intent SetActivity(String a){
Intent intent;
String text;
if(a==null){text="menu";}
else{text=a;}
switch (text){
case "menu":
intent = new Intent(this, MenuActivity.class);
break;
case "air":
intent = new Intent(this, AirSensorList.class);break;
case "body":
intent = new Intent(this, BodySensorList.class);break;
case "commu":
intent = new Intent(this, CommuSensorList.class);break;
case "engine":
intent = new Intent(this, EngineSensorList.class);break;
case "gas":
intent = new Intent(this, GasSensorList.class);break;
case "lower":
intent = new Intent(this, LowerSensorList.class);break;
case "steering":
intent = new Intent(this, SteeringSensorList.class);break;
case "trans":
intent = new Intent(this, TransmissionSensorList.class);break;
case "map":
intent = new Intent(this, MapActivity.class);break;
case "notilog":
intent = new Intent(this, NotiLogList.class);break;
default:
intent = new Intent(this,MenuActivity.class);break;
}
return intent;
}
public void get_voice() {
Thread thread = new Thread(new Runnable() {
public void run() {
handler.sendEmptyMessage(0);
}
});
thread.start();
}
private Handler handler = new Handler() {
public void handleMessage(Message msg) {
listener = new RecognitionListener() {
@Override
public void onReadyForSpeech(Bundle bundle) {
}
@Override
public void onBeginningOfSpeech() {
}
@Override
public void onRmsChanged(float v) {
}
@Override
public void onBufferReceived(byte[] bytes) {
}
@Override
public void onEndOfSpeech() {
}
@Override
public void onError(int i) {
}
@Override
public void onResults(Bundle bundle) {
String key = "";
key = SpeechRecognizer.RESULTS_RECOGNITION;
ArrayList<String> mResult = bundle.getStringArrayList(key);
String[] rs = new String[mResult.size()];
mResult.toArray(rs);
//tv.setText(""+rs[0]);
if(rs[0].contains("예")||rs[0].contains("응")||rs[0].contains("그래")){
Context context = getApplicationContext();
Intent intent = SetActivity(moveactivity);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent);
}
}
@Override
public void onPartialResults(Bundle bundle) {
}
@Override
public void onEvent(int i, Bundle bundle) {
}
};
i = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
i.putExtra(RecognizerIntent.EXTRA_CALLING_PACKAGE, getPackageName());
i.putExtra(RecognizerIntent.EXTRA_LANGUAGE, "ko-KR");
mRecognizer = SpeechRecognizer.createSpeechRecognizer(context);
mRecognizer.setRecognitionListener(listener);
mRecognizer.startListening(i);
// sendNotification(NotiApplyItem item) << 메소드 호출
msg = null;
}
};
}