Android 系统定位和高德定位
系统定位
工具类
public class LocationUtils {
public static final int REQUEST_LOCATION = 0xa1;
public static boolean isOpenLocationServer(Context context) {
int locationMode = 0;
try {
locationMode = Settings.Secure.getInt(context.getContentResolver(), Settings.Secure.LOCATION_MODE);
} catch (Settings.SettingNotFoundException e) {
e.printStackTrace();
return false;
}
return locationMode != Settings.Secure.LOCATION_MODE_OFF;
}
public static boolean isGPSEnabled(Context context) {
LocationManager manager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
return manager.isProviderEnabled(LocationManager.GPS_PROVIDER);
}
public static boolean isLocationEnabled(Context context) {
LocationManager manager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
return manager.isProviderEnabled(LocationManager.NETWORK_PROVIDER) || manager.isProviderEnabled(LocationManager.GPS_PROVIDER);
}
public static void openLocation(Activity activity) {
activity.startActivityForResult(new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS), REQUEST_LOCATION);
}
public static void openLocation(Fragment fragment) {
fragment.startActivityForResult(new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS), REQUEST_LOCATION);
}
}
- 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
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
封装LocationManager
public class LocationHelper {
private static OnLocationChangeListener mOnLocationChangeListener;
private static MyLocationListener mLocationListener;
private static LocationManager mLocationManager;
private static String mProvider;
public static void registerLocation(Context context, @Nullable OnLocationChangeListener listener) {
mOnLocationChangeListener = listener;
mLocationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
mProvider = mLocationManager.getBestProvider(getCriteria(), true);
Location lastKnownLocation = mLocationManager.getLastKnownLocation(mProvider);
if (listener != null && lastKnownLocation != null) {
listener.onLastKnownLocation(lastKnownLocation);
}
mLocationListener = new MyLocationListener();
startLocation();
}
public static void startLocation() {
mLocationManager.requestLocationUpdates(mProvider, 0, 0, mLocationListener);
}
public static void stopLocation() {
mLocationManager.removeUpdates(mLocationListener);
}
private static Criteria getCriteria() {
Criteria criteria = new Criteria();
criteria.setAccuracy(Criteria.ACCURACY_FINE);
criteria.setSpeedRequired(false);
criteria.setCostAllowed(false);
criteria.setBearingRequired(false);
criteria.setAltitudeRequired(false);
criteria.setPowerRequirement(Criteria.POWER_LOW);
return criteria;
}
public static void unregisterLocation() {
if (mLocationManager != null) {
if (mLocationListener != null) {
mLocationManager.removeUpdates(mLocationListener);
}
}
mLocationManager = null;
mLocationListener = null;
mOnLocationChangeListener = null;
}
public interface OnLocationChangeListener {
void onLastKnownLocation(Location location);
void onLocationChanged(Location location);
}
public static class MyLocationListener implements LocationListener {
@Override
public void onLocationChanged(@NonNull Location location) {
if (mOnLocationChangeListener != null) {
mOnLocationChangeListener.onLocationChanged(location);
}
}
}
}
- 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
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
public class LocationService extends Service {
public static void actionStart(Context context) {
context.startService(new Intent(context, LocationService.class));
}
public static void actionStop(Context context) {
context.stopService(new Intent(context, LocationService.class));
}
@SuppressLint("MissingPermission")
@Override
public void onCreate() {
super.onCreate();
new Thread(() -> {
Looper.prepare();
LocationHelper.registerLocation(getApplicationContext(), new LocationHelper.OnLocationChangeListener() {
@Override
public void onLastKnownLocation(Location location) {
if (location != null) {
Log.e("TAG", "onLastKnownLocation= " + location);
}
}
@Override
public void onLocationChanged(Location location) {
if (location != null) {
Log.e("TAG", "onLocationChanged= " + location);
stopSelf();
}
}
});
Looper.loop();
}).start();
}
@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onDestroy() {
super.onDestroy();
LocationHelper.unregisterLocation();
}
}
- 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
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
使用
LocationService.actionStart(MainActivity.this, "start");
LocationService.actionStart(MainActivity.this, "stop");
LocationService.actionStart(MainActivity.this, "once");
高德定位
封装高德地图
public class GDLocationHelper {
private static AMapLocationClient locationClient;
private static OnLocationChangeListener mOnLocationChangeListener;
private static MyLocationListener mListener;
public static void register(Context context, @Nullable OnLocationChangeListener onLocationChangeListener) {
mOnLocationChangeListener = onLocationChangeListener;
initLocation(context);
}
private static void initLocation(Context context) {
locationClient = new AMapLocationClient(context);
AMapLocation lastKnownLocation = locationClient.getLastKnownLocation();
if (lastKnownLocation != null && mOnLocationChangeListener != null) {
mOnLocationChangeListener.onLastKnownLocation(lastKnownLocation);
}
mListener = new MyLocationListener();
locationClient.setLocationListener(mListener);
}
public static void startLocation() {
locationClient.stopLocation();
locationClient.setLocationOption(getDefaultOption(false));
locationClient.startLocation();
}
public static void startOnceLocation() {
locationClient.stopLocation();
locationClient.setLocationOption(getDefaultOption(true));
locationClient.startLocation();
}
public static void stopLocation() {
locationClient.stopLocation();
}
public static void unregister() {
stopLocation();
locationClient.onDestroy();
locationClient = null;
mOnLocationChangeListener = null;
mListener = null;
}
public static AMapLocationClientOption getDefaultOption(boolean isOnce) {
AMapLocationClientOption mOption = new AMapLocationClientOption();
mOption.setLocationMode(AMapLocationClientOption.AMapLocationMode.Hight_Accuracy);
if (isOnce) {
mOption.setOnceLocation(true);
mOption.setOnceLocationLatest(true);
} else {
mOption.setOnceLocation(false);
mOption.setInterval(2_000);
}
mOption.setMockEnable(false);
mOption.setWifiScan(true);
mOption.setLocationCacheEnable(true);
return mOption;
}
public static class MyLocationListener implements AMapLocationListener {
@Override
public void onLocationChanged(AMapLocation aMapLocation) {
if (mOnLocationChangeListener != null) {
mOnLocationChangeListener.onLocationChanged(aMapLocation);
}
}
}
public interface OnLocationChangeListener {
void onLastKnownLocation(AMapLocation location);
void onLocationChanged(AMapLocation location);
}
}
- 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
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
public class GDLocationService extends Service {
private volatile boolean isOnce = false;
public static void actionStart(Context context) {
actionStart(context, null);
}
public static void actionStart(Context context, String command) {
context.startService(new Intent(context, GDLocationService.class).putExtra("command", command));
}
public static void actionStop(Context context) {
context.stopService(new Intent(context, GDLocationService.class));
}
@Override
public void onCreate() {
super.onCreate();
GDLocationHelper.register(getApplicationContext(), new GDLocationHelper.OnLocationChangeListener() {
@Override
public void onLastKnownLocation(AMapLocation location) {
if (location != null) {
Log.e("TAG", "onLastKnownLocation= " + location.toString());
}
}
@Override
public void onLocationChanged(AMapLocation location) {
if (location != null) {
Log.e("TAG", "onLocationChanged= " + location.toString());
if (isOnce) {
stopSelf();
}
}
}
});
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
String command = intent.getStringExtra("command");
switch (command) {
case "once":
isOnce = true;
GDLocationHelper.startOnceLocation();
break;
case "stop":
GDLocationHelper.stopLocation();
break;
case "start":
default:
isOnce = false;
GDLocationHelper.startLocation();
break;
}
return super.onStartCommand(intent, flags, startId);
}
@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onDestroy() {
super.onDestroy();
GDLocationHelper.unregister();
Log.e("TAG", "onDestroy");
}
}
- 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
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
使用
GDLocationService.actionStart(MainActivity.this, "start");
GDLocationService.actionStart(MainActivity.this, "stop");
GDLocationService.actionStart(MainActivity.this, "once");