im building an application that requires the country the user is in. I have no problem getting the country if the user is using a smart phone with
telephonyManager.getNetworkCountryIso()
but how can I achieve this if they are not using a sim, i.e. a nexus 7???
Any help would be greatly appreciated
Thanks
Try this in order to check whether the user is on a tablet or on a phone:
TelephonyManager telephonyManager1 = (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
if(telephonyManager1.getPhoneType() == TelephonyManager.PHONE_TYPE_NONE) {
// TABLET
}
else {
// YOUR PREVIOUS CODE
}
Maybe you can try with the Locale, if the user is in a tablet? I know this doesn't give you the location, but maybe you can try to find out according to the Locale the user has:
String locale = context.getResources().getConfiguration().locale.getCountry();
Hope it helps!
Personally, I would extend the logic to something like this:
Use TelephonyManager
If no modem/SIM is present, use WiFi
If no modem/SIM is present, and WiFi is disabled, use GPS
If no modem/SIM is present and both WiFi and GPS are disabled, use Locale
This way you would cover as much scenarios as possible.
NB Locale will not provide you with your current country, but is dependent on what language is selected in your Android device. I.e., if your phone is in Russian, you will always get Russia.
You could obtain the users location (rough approximation should suffice) and then lookup the approximate address with a Geocoder to determine the country.
In fact, your question is similar to this one.
But in Android, you now get the Geocoder built-in, so no need to specially call a webservice and decode json. See the Location Guide on how to get the users location and the Geocoder API for how to retrieve the location.
Related
I have an app that tries to determine the quality of the mobile network in various locations, and then associate it with which network is in use on the phone.
To get the mobile carrier, I am using
TelephonyManager manager = (TelephonyManager)mContext.getSystemService(
Context.TELEPHONY_SERVICE);
String carrierName = manager.getNetworkOperatorName();
This generally works, but in some cases, I get the result, "Fi Network". Since Google Fi switches between T-Mobile, Sprint and US Celluar in the United States, I'd like to know which of the actual carriers is used, so I can use it to determine the quality of the network.
Is there any API that can tell me which of these carriers the phone is actually using?
TelephonyManager.getSimOperator() will return MCC & MNC codes which should uniquely identify a carrier, you can easily find lists online that maps MCC+MNC to operators names.
I want to make app without use of GPS, and my app is using user's address. so for that i want any idea of getting address without GPS. I want address, not latitude and longitude. Geocoder can be use for getting address but its again using GPS. Please provide solution for my question.
without GPS you can not find address
Get the user to manually enter their address. or bite the bullet and use geo-coding.
EDIT:
you have 2 choices:
1) Use lat/lon and geo - coding to determine the address
or
2) User manually enters either lat/lon or address (which will not go down well)
1) You can get IP address by either $_SERVER['REMOTE_ADDR'] in PHP or using some external JavaScript application, and then pass it to some API like freegeoip and then pass received coordinates to Google. It's quite easy, but it's not reliable solution - it can be very accurate or totally inaccurate.
2) (preferred, but needs permission) You can ask user for with HTML5 Geolocation for his location and it's much more accurate (and then pass it to Google Reverse Geocoding)
I need to determine the country (iso3) the device is in even if the user's device has GPS turned off and does not allow apps to access it's location.
I also need to account for tablets that have no sim card and thus cannot use telephonyManager.
For this reason I don't believe I can use the location manager (also because of these reasons: LocationManager requestLocationUpdates doesn't work)
The approach I am thinking I will need is to make a simple HTTP request to a third party ip location api:
e.g.
http://www.ipinfodb.com/ip_location_api.php
or
https://freegeoip.net
Is this the best way to do it? What is the best open api to use?
Your approach of third party ip location api seems right to currently. May be This would help you
http://ip-api.com/docs/api:json
http://ip-api.com/json
You can use the TelephonyManager
TelephonyManager tm = (TelephonyManager)this.getSystemService(this.TELEPHONY_SERVICE);
String countryCode = tm.getNetworkCountryIso();
Update:
Since you cannot use telephony I would suggest trying this, as it get's the users input from when they setup the device.
String locale = context.getResources().getConfiguration().locale.getCountry();
I want my application to work in specific region e.g US.
We can limit distribution of application from play market but i found there are some hacks to install those apps.
I have to somehow limit the use within application.
For that I can retrieve user's GPS location and use Google's Geocode API for first run. But what if user travels to some other region?
I will have to use Location change listener to cater this scenario, but this will drain battery.
If I go for device's timezone, User can change it.
Is there any other thing i can possibly do to restrict application to specific region?
You can also check for the network the user is registered on with TelephonyManager
You have 2 methods that can be helpful.
GetNetworkCountryIso
http://developer.android.com/reference/android/telephony/TelephonyManager.html#getNetworkCountryIso()
and
GetSimCountryIso
http://developer.android.com/reference/android/telephony/TelephonyManager.html#getSimCountryIso()
Explanation
getNetworkCountryIso() will give you the iso for the country which the user is currenty registered for.
ie: If you're from Albania (al) and went to travel to USA (us) this will return "us"
getSimCountryIso() will give you the iso for the country where the SIM provider's country code.. ie: If you're from Albania (al) and went to travel to USA (us) this will return "al"
UPDATE
You can integrate (if server side available) http://www.whois.net/ip-address-lookup/ to look for the device IP address. You can get the IP like this.
How to get IP address of the device from code?
With a combination of all this functions (Wifi, network provider, IP, GPS, Google Play regions) you can reduce a lot the use limitations of your app. In the other hand if the user it´s advance enough to fake the IP using a proxy, doesn't turn on the Wifi / GPS and doesn't have SIM card, there´s not much more to do.
Hope it helps :)
As a continuation to my comment, you could go by those lines of filtering users by country code.
The problem - only some carriers store the phone number on the actual SIM card. If so, you will be able to obtain it using
TelephonyManager tMgr = (TelephonyManager)mAppContext.getSystemService(Context.TELEPHONY_SERVICE);
String mPhoneNumber = tMgr.getLine1Number();
To avoid the instances of those carriers who don't store the actual number on the SIM - there is no way to retrieve the number seeing it is not stored on the phone.
So, what I would do, is simply on the first run of application, request the user to input his full phone number (including country code) - and store that in SharedPrefs. Then, you will know if to run the app or not.
Good luck, hope this has helped!
The following code does not give me the phone number of the device ,how can i get the phone number
TelephonyManager tm = (TelephonyManager)getSystemService(TELEPHONY_SERVICE);
String number = tm.getLine1Number();
Toast.makeText(getApplicationContext(),"Telephone number: "+ number,
Toast.LENGTH_SHORT).show();
ALso i have tried the following..but the number doest show up
Programmatically obtain the phone number of the Android phone
How to get the mobile number of current sim card in real device?
The only method available from Android API as all the comments suggest is getLine1Number(). However I have never been able to obtain it since your operator must provide you with a SIM that supports letting phone read internal phone number. That seems not very common, so I am afraid that you could be left without means of knowing it.
That's why some programs that use your phone number to identify you (i.e. Whatsapp) do ask user about his/her phone number, because there is no sure way to getting it programatically.
the code provided:
TelephonyManager tMgr =(TelephonyManager)mAppContext.getSystemService(Context.TELEPHONY_SERVICE);
mPhoneNumber = tMgr.getLine1Number();
should work fine, however, it will not if you forgot the permission "READ_PHONE_STATE".
Hope this works now :)
Try this:
private String getMyPhoneNumber(){
TelephonyManager mTelephonyMgr;
mTelephonyMgr = (TelephonyManager)
getSystemService(Context.TELEPHONY_SERVICE);
return mTelephonyMgr.getLine1Number();
}
Hope it help you
[In case if anyone still stumbles here looking for this still existing age-old problem]
AFAIK, TelephonyManager.getLine1Number() is not reliable due to various constraints from operators.
There are some java reflection based hacks but varies from device to device thus making those hacks sort of useless [at least in terms of supported models]
But there is a legitimate lawful logic to find the number, if you really need so. Query all the SMS by sms provider and get the "To" number.
Extra benefits of this trick: 1. you can get all the line numbers if there is multi sim in the device.
Cons: 1. you will need SMS_READ permission [sorry for that]
2. You will get all the sim numbers ever used in the device. this problem can be minimised with some constraint logic e.g. time frame (sms received or sent only today) etc. It would be interesting to hear from others about how to improve this case.