package com.zmlxj.getcurrentlocation;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import android.Manifest;
import android.content.Context;
import android.content.pm.PackageManager;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.location.LocationProvider;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;
import java.util.List;
public class MainActivity extends AppCompatActivity {
private String locationStr = "";
private String message = "";
private static final int REQUEST_CODE = 10;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getLocation();
}
private void getLocation() {
if (Build.VERSION.SDK_INT >= 23) {// android6 执行运行时权限
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
// Activity#requestPermissions
// here to request the missing permissions, and then overriding
// public void onRequestPermissionsResult(int requestCode, String[] permissions,
// int[] grantResults)
// to handle the case where the user grants the permission. See the documentation
// for Activity#requestPermissions for more details.
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION,Manifest.permission.ACCESS_COARSE_LOCATION}, REQUEST_CODE);
}
}
Criteria criteria = new Criteria();
criteria.setAccuracy(Criteria.ACCURACY_COARSE);//低精度,如果设置为高精度,依然获取不了location。
criteria.setAltitudeRequired(true);//不要求海拔
criteria.setBearingRequired(true);//不要求方位
criteria.setCostAllowed(true);//允许有花费
criteria.setPowerRequirement(Criteria.POWER_LOW);//低功耗
//获取LocationManager
LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
// 获取最好的定位方式
String provider = locationManager.getBestProvider(criteria, true); // true 代表从打开的设备中查找
// 获取所有可用的位置提供器
List<String> providerList = locationManager.getProviders(true);
// 测试一般都在室内,这里颠倒了书上的判断顺序
if (providerList.contains(LocationManager.NETWORK_PROVIDER)) {
provider = LocationManager.NETWORK_PROVIDER;
} else if (providerList.contains(LocationManager.GPS_PROVIDER)) {
provider = LocationManager.GPS_PROVIDER;
} else {
// 当没有可用的位置提供器时,弹出Toast提示用户
Toast.makeText(this, "Please Open Your GPS or Location Service", Toast.LENGTH_SHORT).show();
return;
}
LocationListener locationListener = new LocationListener(){
//当位置改变的时候调用
@Override
public void onLocationChanged(Location location) {
//经度
double longitude = location.getLongitude();
//纬度
double latitude = location.getLatitude();
//海拔
double altitude = location.getAltitude();
locationStr = longitude+"_"+latitude;
// launcher.callExternalInterface("getLocationSuccess", locationStr);
}
//当GPS状态发生改变的时候调用
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
switch (status) {
case LocationProvider.AVAILABLE:
message = "当前GPS为可用状态!";
break;
case LocationProvider.OUT_OF_SERVICE:
message = "当前GPS不在服务内!";
break;
case LocationProvider.TEMPORARILY_UNAVAILABLE:
message = "当前GPS为暂停服务状态!";
break;
}
// launcher.callExternalInterface("GPSStatusChanged", message);
}
//GPS开启的时候调用
@Override
public void onProviderEnabled(String provider) {
message = "GPS开启了!";
// launcher.callExternalInterface("GPSOpenSuccess", message);
}
//GPS关闭的时候调用
@Override
public void onProviderDisabled(String provider) {
message = "GPS关闭了!";
// launcher.callExternalInterface("GPSClosed", message);
//
}
};
//获取上次的location
Location location = locationManager.getLastKnownLocation(provider);
/**
* 参1:选择定位的方式
* 参2:定位的间隔时间
* 参3:当位置改变多少时进行重新定位
* 参4:位置的回调监听
*/
locationManager.requestLocationUpdates(provider, 10000, 0, locationListener);
while(location == null){
location = locationManager.getLastKnownLocation(provider);
} //移除更新监听
locationManager.removeUpdates(locationListener);
if (location != null) {
// 不为空,显示地理位置经纬度 //
// 经度
double longitude = location.getLongitude();
//纬度
double latitude = location.getLatitude();
//海拔
double altitude = location.getAltitude();
locationStr = longitude+ "_"+latitude+" "+altitude;
Log.i("当前获取的值",locationStr);
// launcher.callExternalInterface( "getLocationSuccess", locationStr);
}
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if (requestCode == REQUEST_CODE) {
if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
// Permission Granted准许
getLocation();
} else {
// Permission Denied拒绝
}
}
}
}