I am working on android application where I want to detect the country code from my phone book where by default country code is not added. For example:
+971529090909,
00971529730000,
0529090909
In "+" and "00" state, I have country codes, but in case of "0" my country code is not available. I want to detect the country code of all contacts even if someone didn't put the country code.
How to detect country code/ Area code with any library or with any code snippet.
You can parse any number with any format using the libphonenumber library (maintained by Google), just specify the default Locale while parsing so it'll know what country code to default into.
You can play with an online version of it here.
TelephonyManager telephony = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
String simCountryIso = telephony.getSimCountryIso();
if (simCountryIso != null)
return simCountryIso.toUpperCase(loc);
} else {
Locale loc = Locale.getDefault();
return telephony.getNetworkCountryIso().toUpperCase(loc);
}
Related
im new to android app development and currently trying to get the eNB in Android Studio with Kotlin. Im aware of this solution: How to get eNB id of LTE on Android Studio (TelephonyManager)
However, if I try to implement this, my app just crashes and I get no feedback. Is there anything special you need to keep in mind when setting up your app to get mobile cell data?
Thank you!
Just initialize a TelephonyManager from the Telephony_Service and by filtering for the different mobile net standards like UMTS (3G), LTE (4G) or NR (5G) and using the "getallCellInfo Method, you can get all relevant information from the current cell your phone is connected with. Here is the code:
val tm = getSystemService(TELEPHONY_SERVICE) as TelephonyManager
val cellInfo = tm.allCellInfo
if(cellInfo != null){
for(info in cellInfo){
if(info is CellInfoLte){
val identityLte = info.cellIdentity
val operator = identityLte.operatorAlphaLong.toString()
// get more Information like mcc, mnc, pci, tac, ... //
}
}
}
I am using Android Speech to text for speech recognition.
final Intent sttIntent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
sttIntent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
final String language = config.getLanguage().replace('-', '_');
sttIntent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, language);
sttIntent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_PREFERENCE, language);
sttIntent.putExtra(RecognizerIntent.EXTRA_PARTIAL_RESULTS, true);
sttIntent.putExtra(RecognizerIntent.EXTRA_CALLING_PACKAGE, context.getPackageName());
// WORKAROUND for https://code.google.com/p/android/issues/detail?id=75347
sttIntent.putExtra("android.speech.extra.EXTRA_ADDITIONAL_LANGUAGES", new String[]{language});
return sttIntent;
I am having problem while getting phone number and email id.
It is not giving proper results.
Suppose if user say "triple two triple three five hundred one"
In this case STT should give me 222333501, but it is giving some number and string as result.
Suppose if user say "abc_cde#gmail.com", in this case also it is giving at the rate of and underscore as strings.
Is there any native way to solve this problem.
I have referred a java utility Convert words to number to convert string to number if valid but i am not sure that would work in all cases.
Any help is appreciated.
In my application I'm using SpeechRecognizer, in order to detect what the user said.
My device's language is set to English and it works perfect when I say something in English, but when I say something in other languages, for example - Hebrew, it doesn't work all the time as it works for English, until I set the device's language to Hebrew and then it works OK.
I'm trying to avoid from setting the device's language and want it to automatically detect the user language.
I've noticed that "OK Google" works and detect the correct words in Hebrew even when the device's language is set to English.
For the meantime, what I tried to do is when the user enter for the first time
to my application, I'm asking him to enter his country.
Then when I have his country -> I'm getting the country code and then I create a Locale using the country code.
This locale is then sent as the language to the speech recognizer.
But it didn't help..
// example of how to get the locale using the country code
Locale myLocale = null;
String toSearch = "IL";
toSearch = toSearch.trim().toLowerCase();
for (Locale locale : Locale.getAvailableLocales())
{
if(locale.getCountry().trim().toLowerCase().contains(toSearch))
{
myLocale = locale;
break;
}
}
// example of how I'm sending the locale
Intent recIntent= new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
recIntent.putExtra(RecognizerIntent.ExtraLanguage,myLocale);
I have used this line to check the locale.
String local = context.getResources().getConfiguration().locale.getDisplayCountry();
Toast.MakeText(this,local,Toast.LENGTH_SHORT).show();
It shows Australia (this is where I am).
But I need to display different pages based on locale. so what I have done is
if (local=="Australia")
runpage1();
else
runpage2();
But, it doesn't check the local as "Australia", so straight goes to the else line. I tried like
String x;
if (local=="Australia")
x = "1";
else
x="2";
Toast.MakeText(this,x,Toast.LENGTH_SHORT).show();
It shows 2. ( I am trying to write to make it as simple as possible, so nobody gets confused with previously asked questions)
Can anybody please suggest me why I am being unable to treat locale as String value ?
I have tried using this too.
TelephonyManager teleMgr = (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
String myCode = teleMgr.getSimCountryIso();
But, same thing, myCode is also cannot be checked as String.
Any suggestions will be great.
== for test only reference..equals() for check String values. So you have to compare like this :
String x;
if (local.trim().equals("Australia"))
x = "1";
else
x="2";
Toast.MakeText(this,x,Toast.LENGTH_SHORT).show();
original question
I have a standard texttospeech, android.speech.tts.TextToSpeech
I initialize it and set a language by using tts.setLanguage(Locale.getDefault())
That default Locale is de_DE (for germany, correct).
Right after setting it, i ask the tts to give me its language tts.getLanguage()
now it tells me that its set to "deu_DEU"
There is no Locale with that setting. So i cant even check if its set to the right language because i cant find the Locale object that has the matching values.
Issue might be related to Android 4.3, but i didnt find any info.
Background is, that i need to show values with the same decimal symbol, but tts needs the correct symbol or it says "dot" in german which makes NO sense at all.
Conclusion:
A Locale is a container that contains a string that is composed of a language, a country and an optional string. Every text-to-speech engine can return a custom Locale like "eng_USA_texas".
Furthermore the Locale that is returned by the tts engine can only be a "close match" to the wanted Locale. So "en_US" instead of "en_UK".
However, Locale has a method called getLanguage() and it returns the first part of above mentioned string. "en" or "eng". Those Language codes are regulated by ISO and one can hope that everyone sticks to it. (see link in the accepted answer)
So checking for tts.getLanguage().getLanguage().startsWith("en") should always be true if its some form of english language setting and the ISO standards are fulfilled.
It is important to mention that Locales should not be compared by locale_a == locale_b as both can be different objects yet have the same content, they are containers of sort.
Always compare with locale_a.equals(locale_b)
I hope this helps people sort out some problems with tts and language
You're right, it's frustrating how the locale codes the TTS object uses are different to those of the device locale. I don't understand why this decision was made.
To add further complication, the TTS Engine can supply all kinds of different locales, such as eng_US_sarah or en-US-female etc. It's down to the TTS Engine how these are stored and displayed.
I've had to write additional code to iterate through the returned locales and attempt to match them to the locale the system can use, or vica-versa.
To start with, take a look at how the engines you have installed are returning their locale information. You can then start to collate in your code a list to associate 'deu_DEU' to 'de_De'.
This is often simplistic by using split("_") & startsWith(String), but unfortunately not for all locales.
Here's some base code I've used to analyse the installed TTS Engines' locale structure.
private void getEngines() {
final Intent ttsIntent = new Intent();
ttsIntent.setAction(TextToSpeech.Engine.ACTION_CHECK_TTS_DATA);
final PackageManager pm = getActivity().getPackageManager();
final List<ResolveInfo> list = pm.queryIntentActivities(ttsIntent, PackageManager.GET_META_DATA);
final ArrayList<Intent> intentArray = new ArrayList<Intent>(list.size());
for (int i = 0; i < list.size(); i++) {
final Intent getIntent = new Intent();
getIntent.setAction(TextToSpeech.Engine.ACTION_CHECK_TTS_DATA);
getIntent.setPackage(list.get(i).activityInfo.applicationInfo.packageName);
getIntent.getStringArrayListExtra(TextToSpeech.Engine.EXTRA_AVAILABLE_VOICES);
intentArray.add(getIntent);
}
for (int i = 0; i < intentArray.size(); i++) {
startActivityForResult(intentArray.get(i), i);
}
}
#Override
public void onActivityResult(final int requestCode, final int resultCode, final Intent data) {
try {
if (data != null) {
System.out.print(data.getStringArrayListExtra("availableVoices").toString());
}
} catch (NullPointerException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
From the above ISO-3 codes and the device locale format, you should be able to come up with something for the locales you are concerned with.
I've been intending to submit an enhancement request to AOSP for a while, as all TTS Engines need to use constant values and extras such as gender etc need to be added to use the TTS Engines to their full capabilities.
EDIT: Further to your edit, note the wording regarding setLanguage(). The individual TTS Engine will try and match as close as possible to the requested locale, but that applied locale may be completely wrong, depending on how lenient the Engine provider is in their code and their response.
After creating an object of TextToSpeech class, you should configure it (or check it's available state/values) into TextToSpeech.OnInitListener's onInit() callback. You will get reliable information there about your TextToSpeech object.
Check my answer here:
https://stackoverflow.com/a/65620221/7835969