public static boolean isCN(Context context) {TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);String countryIso = tm.getSimCountryIso();boolean isCN = false;//判断是不是大陆if (!TextUtils.isEmpty(countryIso)) {countryIso = countryIso.toUpperCase(Locale.US);if (countryIso.contains("CN")) {isCN = true;}}return isCN;}
/** 判断是否是国内的 SIM 卡,优先判断注册时的mcc */ public static boolean isChinaSimCard(Context c) {String mcc = getSimOperator(c);if (isOperatorEmpty(mcc)) {return false;} else {return mcc.startsWith("460");} }
Locale locale = Locale.getDefault(); String country = locale.getCountry();
CN 就是中国
1、添加位置权限
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"></uses-permission>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"></uses-permission>
2、activity实现获取经纬度,地理位置代码
public class MainActivity extends AppCompatActivity {private TextView textView;private static final String[] authBaseArr = {//申请类型Manifest.permission.ACCESS_FINE_LOCATION,Manifest.permission.ACCESS_COARSE_LOCATION};private static final int authBaseRequestCode = 1;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);textView = findViewById(R.id.textView);LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);initNavi();//权限检查的代码if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {// TODO: Consider callingreturn;}locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,//指定GPS定位提供者1000,//指定数据更新的间隔时间1,//位置间隔的距离为1mnew LocationListener() {//监听GPS信息是否改变@Overridepublic void onLocationChanged(Location location) {//GPS信息发送改变时回调Log.i("lgq","onLocationChanged===="+location.getProvider());}@Overridepublic void onStatusChanged(String provider, int status, Bundle extras) {//GPS状态发送改变时回调}@Overridepublic void onProviderEnabled(String provider) { //定位提供者启动时回调}@Overridepublic void onProviderDisabled(String provider) { //定位提供者关闭时回调}});Location location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);//获取最新的定位信息locationUpdates(location);}private boolean hasBasePhoneAuth() {PackageManager pm = getPackageManager();for (String auth : authBaseArr) {if (pm.checkPermission(auth, getPackageName()) != PackageManager.PERMISSION_GRANTED) {return false;}}return true;}private void initNavi() {// 申请权限if (android.os.Build.VERSION.SDK_INT >= 23) {if (!hasBasePhoneAuth()) {this.requestPermissions(authBaseArr, authBaseRequestCode);return;}}}public void locationUpdates(Location location){if(location != null){StringBuilder stringBuilder = new StringBuilder(); //构建一个字符串构建器,用于记录定位信息stringBuilder.append("您的位置是:\n");stringBuilder.append("经度:");stringBuilder.append(location.getLongitude());stringBuilder.append("\n纬度:");stringBuilder.append(location.getLatitude());textView.setText(stringBuilder.toString());Log.i("lgq",".....经度==="+location.getLongitude()+"...纬度+====="+location.getLatitude());String ab = getAddress(location.getLatitude(),location.getLongitude());Log.i("lgq","sssssfa===="+ab);}else{textView.setText("GPS失效啦...");}}public String getAddress(double latitude, double longitude) {Geocoder geocoder = new Geocoder(this, Locale.getDefault());try {List<Address> addresses = geocoder.getFromLocation(latitude,longitude, 1);// Address[addressLines=[0:"广东省东莞市健升大厦"],feature=健升大厦,admin=广东省,sub-admin=null,locality=东莞市,thoroughfare=null,postalCode=null,countryCode=CN,countryName=中国,hasLatitude=true,
// latitude=23.025354,hasLongitude=true,longitude=113.748738,phone=null,url=null,extras=Bundle[mParcelledData.dataSize=92]]if (addresses.size() > 0) {Address address = addresses.get(0);String data = address.toString();int startCity = data.indexOf("locality=") + "locality=".length();int endCity = data.indexOf(",", startCity);String city = data.substring(startCity, endCity);int startPlace = data.indexOf("feature=") + "feature=".length();int endplace = data.indexOf(",", startPlace);String place = data.substring(startPlace, endplace);return city + place ;}} catch (IOException e) {e.printStackTrace();}return "获取失败";}
}
如获取不到位置信息
加一判断即可
if (location==null){locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER,//指定GPS定位提供者5000,//指定数据更新的间隔时间10,//位置间隔的距离为1mnew LocationListener() {//监听GPS信息是否改变@Overridepublic void onLocationChanged(Location location) {//GPS信息发送改变时回调Log.i("lgq","onLocationChanged===="+location.getProvider());}@Overridepublic void onStatusChanged(String provider, int status, Bundle extras) {//GPS状态发送改变时回调}@Overridepublic void onProviderEnabled(String provider) { //定位提供者启动时回调}@Overridepublic void onProviderDisabled(String provider) { //定位提供者关闭时回调}});location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);//获取最新的定位信息 }