onCreate(), , TTS:
if ((isPackageInstalled(getPackageManager(), SPECIFIC_TTS_PACKAGE_NAME))) {
Log.e(TTS_TAG, "Intended TTS engine installed");
mTTS = new TextToSpeech(this, new TextToSpeech.OnInitListener() {
@Override
public void onInit(int status) {
if (status == TextToSpeech.ERROR) {
Log.e(TTS_TAG, "TTS initialize failed");
} else {
int result = mTTS.setLanguage(Locale.US);
if (result == TextToSpeech.LANG_NOT_SUPPORTED
|| result == TextToSpeech.LANG_MISSING_DATA) {
Log.e(TTS_TAG, "Language not supported");
} else {
mButtonSpeak.setEnabled(true);
}
}
}
}, SPECIFIC_TTS_PACKAGE_NAME);
} else {
Log.e(TTS_TAG, "Intended TTS engine is not installed");
installSpecificTTSEngine();
}
TTS:
private void installSpecificTTSEngine() {
if (internetIsConnected()) {
Intent installIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("market://details?id="+SPECIFIC_TTS_PACKAGE_NAME));
installIntent.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY | Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET|Intent.FLAG_ACTIVITY_MULTIPLE_TASK|Intent.FLAG_ACTIVITY_NEW_TASK);
try {
Log.e(TTS_TAG, "Installing TTS engine: " + installIntent.toUri(0));
startActivity(installIntent);
} catch (ActivityNotFoundException ex) {
Log.e(TTS_TAG, "Failed to install TTS engine, no activity found for " + installIntent + ")");
}
} else {
Log.e(TTS_TAG, "Internet is not connected for download tts engine");
}
}
:
public static boolean isPackageInstalled(PackageManager pm, String packageName) {
try {
pm.getPackageInfo(packageName, 0);
} catch (PackageManager.NameNotFoundException e) {
return false;
}
return true;
}
protected boolean internetIsConnected() {
ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo netInfo = cm != null ? cm.getActiveNetworkInfo() : null;
if (netInfo != null && netInfo.isConnected()) {
return true;
} else {
return false;
}
}