I hope that you're enjoying learning Android development. It can be done as follows:
public class SplashActivity extends AppCompatActivity implements TextToSpeech.OnInitListener {
private TextToSpeech mTts;
private static final String TAG = MainActivity.class.getName();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mTts = new TextToSpeech(this, this);
}
@Override
protected void onPause() {
if(mTts != null){
mTts.stop();
mTts.shutdown();
}
super.onPause();
}
@Override
public void onInit(int status) {
if (status == TextToSpeech.SUCCESS) {
mTts.setOnUtteranceProgressListener(new UtteranceProgressListener() {
@Override
public void onDone(String utteranceId) {
Log.d(TAG, "Done: " + utteranceId);
Intent homeIntent = new Intent(CinemaList.this, MovieList.class);
startActivity(homeIntent);
}
@Override
public void onError(String utteranceId) {
Log.e(TAG, "Error: " + utteranceId);
}
@Override
public void onStart(String utteranceId) {
Log.i(TAG, "Started: " + utteranceId);
}
});
mTts.speak("Done by ME!", TextToSpeech.QUEUE_ADD, null);
} else {
Log.e(TAG, "Failed");
}
}
}
I took the liberty of starting the 'Cinema List' activity after the speech completes. However, you can continue to use your Handler if you wish.
Also, rather than calling finish() explicitly, I'd advise adding the noHistory flag to the SplashActivity in the manifest:
<activity
android:name=".SplashActivity"
android:noHistory="true"/>
The noHistory
flag ensures that the Activity
will not be there when back is pressed.
noHistory vs finish() - Which is preferred?