Facile.
Provo ad applicarlo al Service che parla...
Qui conviene aspettare che si arrivi a onUtteranceCompletedListener per aprire l'altro e chiudere se stesso, in modo da dare il tempo al Service di parlare.
Ci provo.
Questo service (ne ho creato una copia denominata Voce) non overrida onStartCommand, ma onCreate, che chiama onInit del TextToSpeech, implementata dal service stesso.
Mi conviene piuttosto attendere che abbia finito di parlare per richiamare Alfa, quindi lo start con relativo stop di se stesso lo piazzo in onUtteranceCompleted.
@Override public void onUtteranceCompleted(String utteranceId) { Intent intent=new Intent(this,Alfa.class); startService(intent); stopSelf(); Log.v("SEGNALE","ONUTTERANCE"); }Alfa dovrebbe comportarsi come al solito, richiamando Voce con ritardo secondo AlarmManager.
Proviamo...
Dimenticavo la condizione relativa alla variabile globale:
@Override public void onUtteranceCompleted(String utteranceId) { if(Globals.stop==false){ Intent intent=new Intent(this,Alfa.class); startService(intent); stopSelf(); } Log.v("SEGNALE","ONUTTERANCE"); }e andiamo dopo aver cambiato, in Alfa, Beta con Voce...
Sì, funziona!
Ora devo stabilire i tempi casuali.
L'ho fatto...
Non riesco a cancellare subito l'AlarmManager... Intanto mi annoto il codice:
public class MainActivity extends Activity { Button button; Button bttStart; Intent intent; AlarmManager alarmManager; PendingIntent pendingIntent; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); button=(Button)findViewById(R.id.button1); bttStart=(Button)findViewById(R.id.button2); button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Globals.stop=true; } }); bttStart.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Globals.stop=false; intent=new Intent(getApplicationContext(),Alfa.class); startService(intent); } }); } }
Alfa;
public class Alfa extends Service{ AlarmManager alarmManager; PendingIntent pendingIntent; @Override public IBinder onBind(Intent intent) { // TODO Auto-generated method stub return null; } @Override public int onStartCommand(Intent intent,int flags,int startUI){ Log.v("ALFA","ONSTARTCOMMAND"); if (Globals.stop==false){ Intent i=new Intent(this,Voce.class); pendingIntent=PendingIntent.getService(this,0,i,0); Log.d("MSECS",""+Globals.mSecs); alarmManager=(AlarmManager)this.getSystemService(Context.ALARM_SERVICE); alarmManager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime()+Globals.mSecs, pendingIntent); stopSelf(); } else{ alarmManager.cancel(pendingIntent); stopSelf(); stopService(new Intent(getApplicationContext(),Voce.class)); } return Service.START_NOT_STICKY; } @Override public void onDestroy(){ Log.v("ALFA","ONDESTROY"); super.onDestroy(); } }
Voce:
public class Voce extends Service implements TextToSpeech.OnInitListener,TextToSpeech.OnUtteranceCompletedListener{ TextToSpeech tts; @Override public void onCreate(){ tts=new TextToSpeech(this,this); Log.v("SEGNALE", "ONCREATE"); } @Override public IBinder onBind(Intent intent) { // TODO Auto-generated method stub return null; } @Override public void onInit(int status) { if(status==TextToSpeech.SUCCESS){ int result=tts.setLanguage(Locale.ITALIAN); tts.setOnUtteranceCompletedListener(this); HashMaphashMap=new HashMap (); hashMap.put(TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID, "frase"); tts.setPitch(.5f); int tempo=Globals.mSecs/1000; //tempo in secondi int minuti=tempo/60; int secondi=tempo%60; tts.speak("Sono passati "+minuti+" minuti e"+secondi+"secondi", TextToSpeech.QUEUE_FLUSH, hashMap); } Log.v("SEGNALE","ONINIT"); } @Override public void onUtteranceCompleted(String utteranceId) { Log.v("VARIABILE GLOBALE",""+Globals.mSecs); if(Globals.stop==false){ Globals.mSecs=(int) (Math.random()*3*60*1000); Intent intent=new Intent(this,Alfa.class); startService(intent); stopSelf(); } Log.v("SEGNALE","ONUTTERANCE"); } @Override public void onDestroy(){ tts.stop(); tts.shutdown(); super.onDestroy(); Log.v("SEGNALE","ONDESTROY"); } }
Nessun commento:
Posta un commento