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"); } }
private Location getlocation(Context context) {
LocationManager locationManager = (LocationManager) context.getSystemService(LOCATION_SERVICE);
// gps
@SuppressLint("MissingPermission") Location location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
// 网络定位
@SuppressLint("MissingPermission") Location lastKnownLocation = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
return lastKnownLocation;
}
private void initLoad() {
Location getlocation = getlocation(MyApplication.getContext());
// 获取经纬度
double latitude = getlocation.getLatitude();
double longitude = getlocation.getLongitude();
// 地理编辑器 如果想获取地理位置 使用地理编辑器将经纬度转换为省市区
Geocoder geocoder = new Geocoder(MyApplication.getContext(), Locale.getDefault());
try {
List<Address> fromLocation = geocoder.getFromLocation(latitude, longitude, 1);
Address address = fromLocation.get(0);
String mAddressLine = address.getAddressLine(0);
Log.i("xxx",mAddressLine.toString());
} catch (IOException e) {
e.printStackTrace();
}
}
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;
@Override
protected 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 calling
return;
}
locationManager.requestLocationUpdates(
LocationManager.GPS_PROVIDER,//指定GPS定位提供者
1000,//指定数据更新的间隔时间
1,//位置间隔的距离为1m
new LocationListener() {//监听GPS信息是否改变
@Override
public void onLocationChanged(Location location) {//GPS信息发送改变时回调
Log.i("lgq","onLocationChanged===="+location.getProvider());
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {//GPS状态发送改变时回调
}
@Override
public void onProviderEnabled(String provider) { //定位提供者启动时回调
}
@Override
public 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,//位置间隔的距离为1m new LocationListener() {//监听GPS信息是否改变 @Override public void onLocationChanged(Location location) {//GPS信息发送改变时回调 Log.i("lgq","onLocationChanged===="+location.getProvider()); } @Override public void onStatusChanged(String provider, int status, Bundle extras) {//GPS状态发送改变时回调 } @Override public void onProviderEnabled(String provider) { //定位提供者启动时回调 } @Override public void onProviderDisabled(String provider) { //定位提供者关闭时回调 } } ); location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);//获取最新的定位信息 }