-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLocationUtils.java
More file actions
29 lines (25 loc) · 1.08 KB
/
LocationUtils.java
File metadata and controls
29 lines (25 loc) · 1.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
package common.android.fiot.androidcommon;
import android.content.Context;
import android.telephony.TelephonyManager;
import java.util.Locale;
/**
* Created by caoxuanphong on 7/28/16.
*/
public class LocationUtils {
public static String getUserCountry(Context context) {
try {
final TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
final String simCountry = tm.getSimCountryIso();
if (simCountry != null && simCountry.length() == 2) { // SIM country code is available
return simCountry.toLowerCase(Locale.US);
} else if (tm.getPhoneType() != TelephonyManager.PHONE_TYPE_CDMA) { // device is not 3G (would be unreliable)
String networkCountry = tm.getNetworkCountryIso();
if (networkCountry != null && networkCountry.length() == 2) { // network country code is available
return networkCountry.toLowerCase(Locale.US);
}
}
}
catch (Exception e) { }
return null;
}
}