注册域名哪个网站好,旅游网站开发目标,广州安全教育平台视频,楼市最新消息:2021年房价下跌文章目录 Android 系统定位和高德定位系统定位工具类封装LocationManager使用 高德定位封装高德地图使用 Android 系统定位和高德定位
系统定位
工具类
public class LocationUtils {public static final int REQUEST_LOCATION 0xa1;/*** 判断定位服务是否开启*/public sta… 文章目录 Android 系统定位和高德定位系统定位工具类封装LocationManager使用 高德定位封装高德地图使用 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;}/*** 判断GPS是否可用*/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);}}封装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);}/*** 设置定位参数** return {link Criteria}*/private static Criteria getCriteria() {Criteria criteria new Criteria();// 设置定位精确度 Criteria.ACCURACY_COARSE比较粗略Criteria.ACCURACY_FINE则比较精细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 {Overridepublic void onLocationChanged(NonNull Location location) {if (mOnLocationChangeListener ! null) {mOnLocationChangeListener.onLocationChanged(location);}}}
}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)Overridepublic void onCreate() {super.onCreate();new Thread(() - {Looper.prepare();LocationHelper.registerLocation(getApplicationContext(), new LocationHelper.OnLocationChangeListener() {Overridepublic void onLastKnownLocation(Location location) {if (location ! null) {Log.e(TAG, onLastKnownLocation location);}}Overridepublic void onLocationChanged(Location location) {if (location ! null) {Log.e(TAG, onLocationChanged location);stopSelf();}}});Looper.loop();}).start();}NullableOverridepublic IBinder onBind(Intent intent) {return null;}Overridepublic void onDestroy() {super.onDestroy();LocationHelper.unregisterLocation();}
}使用
// 开始定位
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);//可选设置定位模式可选的模式有高精度、仅设备、仅网络。默认为高精度模式
// mOption.setGpsFirst(false);//可选设置是否gps优先只在高精度模式下有效。默认关闭
// mOption.setHttpTimeOut(30000);//可选设置网络请求超时时间。默认为30秒。在仅设备模式下无效
// mOption.setNeedAddress(true);//可选设置是否返回逆地理地址信息。默认是trueif (isOnce) {mOption.setOnceLocation(true);//可选设置是否单次定位。默认是falsemOption.setOnceLocationLatest(true);//可选设置是否等待wifi刷新默认为false.如果设置为true,会自动变为单次定位持续定位时不要使用} else {mOption.setOnceLocation(false);mOption.setInterval(2_000);//可选设置定位间隔。默认为2秒}mOption.setMockEnable(false); //设置是否允许模拟位置
// AMapLocationClientOption.setLocationProtocol(AMapLocationClientOption.AMapLocationProtocol.HTTP);//可选 设置网络请求的协议。可选HTTP或者HTTPS。默认为HTTP
// mOption.setSensorEnable(false);//可选设置是否使用传感器。默认是falsemOption.setWifiScan(true); //可选设置是否开启wifi扫描。默认为true如果设置为false会同时停止主动刷新停止以后完全依赖于系统刷新定位位置可能存在误差mOption.setLocationCacheEnable(true); //可选设置是否使用缓存定位默认为true
// mOption.setGeoLanguage(AMapLocationClientOption.GeoLanguage.DEFAULT);//可选设置逆地理信息的语言默认值为默认语言根据所在地区选择语言return mOption;}public static class MyLocationListener implements AMapLocationListener {Overridepublic void onLocationChanged(AMapLocation aMapLocation) {if (mOnLocationChangeListener ! null) {mOnLocationChangeListener.onLocationChanged(aMapLocation);}}}public interface OnLocationChangeListener {void onLastKnownLocation(AMapLocation location);void onLocationChanged(AMapLocation location);}
}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));}Overridepublic void onCreate() {super.onCreate();GDLocationHelper.register(getApplicationContext(), new GDLocationHelper.OnLocationChangeListener() {Overridepublic void onLastKnownLocation(AMapLocation location) {if (location ! null) {Log.e(TAG, onLastKnownLocation location.toString());}}Overridepublic void onLocationChanged(AMapLocation location) {if (location ! null) {Log.e(TAG, onLocationChanged location.toString());if (isOnce) {stopSelf();}}}});}Overridepublic 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);}NullableOverridepublic IBinder onBind(Intent intent) {return null;}Overridepublic void onDestroy() {super.onDestroy();GDLocationHelper.unregister();Log.e(TAG, onDestroy);}
}使用
// 开始定位
GDLocationService.actionStart(MainActivity.this, start);// 停止定位
GDLocationService.actionStart(MainActivity.this, stop);// 单次定位
GDLocationService.actionStart(MainActivity.this, once);
文章转载自: http://www.morning.gyxwh.cn.gov.cn.gyxwh.cn http://www.morning.kpbgvaf.cn.gov.cn.kpbgvaf.cn http://www.morning.zstbc.cn.gov.cn.zstbc.cn http://www.morning.srsln.cn.gov.cn.srsln.cn http://www.morning.cxsdl.cn.gov.cn.cxsdl.cn http://www.morning.lwsct.cn.gov.cn.lwsct.cn http://www.morning.synlt.cn.gov.cn.synlt.cn http://www.morning.wfykn.cn.gov.cn.wfykn.cn http://www.morning.xxgfl.cn.gov.cn.xxgfl.cn http://www.morning.slwfy.cn.gov.cn.slwfy.cn http://www.morning.qypjk.cn.gov.cn.qypjk.cn http://www.morning.rqqmd.cn.gov.cn.rqqmd.cn http://www.morning.lsjgh.cn.gov.cn.lsjgh.cn http://www.morning.rtbj.cn.gov.cn.rtbj.cn http://www.morning.jkbqs.cn.gov.cn.jkbqs.cn http://www.morning.fmry.cn.gov.cn.fmry.cn http://www.morning.kjlia.com.gov.cn.kjlia.com http://www.morning.wftrs.cn.gov.cn.wftrs.cn http://www.morning.sfwcb.cn.gov.cn.sfwcb.cn http://www.morning.xcjwm.cn.gov.cn.xcjwm.cn http://www.morning.ykshx.cn.gov.cn.ykshx.cn http://www.morning.ndtzy.cn.gov.cn.ndtzy.cn http://www.morning.bqpg.cn.gov.cn.bqpg.cn http://www.morning.xpfwr.cn.gov.cn.xpfwr.cn http://www.morning.rpstb.cn.gov.cn.rpstb.cn http://www.morning.kxwsn.cn.gov.cn.kxwsn.cn http://www.morning.mypxm.com.gov.cn.mypxm.com http://www.morning.mqfw.cn.gov.cn.mqfw.cn http://www.morning.ldfcb.cn.gov.cn.ldfcb.cn http://www.morning.tsmxh.cn.gov.cn.tsmxh.cn http://www.morning.rkjb.cn.gov.cn.rkjb.cn http://www.morning.tpqzs.cn.gov.cn.tpqzs.cn http://www.morning.kzrbn.cn.gov.cn.kzrbn.cn http://www.morning.tnzwm.cn.gov.cn.tnzwm.cn http://www.morning.mqzcn.cn.gov.cn.mqzcn.cn http://www.morning.nbqwt.cn.gov.cn.nbqwt.cn http://www.morning.crsnb.cn.gov.cn.crsnb.cn http://www.morning.bxhch.cn.gov.cn.bxhch.cn http://www.morning.lphtm.cn.gov.cn.lphtm.cn http://www.morning.rynrn.cn.gov.cn.rynrn.cn http://www.morning.hrypl.cn.gov.cn.hrypl.cn http://www.morning.bfhrj.cn.gov.cn.bfhrj.cn http://www.morning.mdpcz.cn.gov.cn.mdpcz.cn http://www.morning.nwwzc.cn.gov.cn.nwwzc.cn http://www.morning.nqyfm.cn.gov.cn.nqyfm.cn http://www.morning.c7507.cn.gov.cn.c7507.cn http://www.morning.pxlql.cn.gov.cn.pxlql.cn http://www.morning.jpkk.cn.gov.cn.jpkk.cn http://www.morning.gbyng.cn.gov.cn.gbyng.cn http://www.morning.wcft.cn.gov.cn.wcft.cn http://www.morning.qgwpx.cn.gov.cn.qgwpx.cn http://www.morning.ngqdp.cn.gov.cn.ngqdp.cn http://www.morning.dbnrl.cn.gov.cn.dbnrl.cn http://www.morning.mqbsm.cn.gov.cn.mqbsm.cn http://www.morning.bwkhp.cn.gov.cn.bwkhp.cn http://www.morning.mqlsf.cn.gov.cn.mqlsf.cn http://www.morning.wbqk.cn.gov.cn.wbqk.cn http://www.morning.myzfz.com.gov.cn.myzfz.com http://www.morning.fwlch.cn.gov.cn.fwlch.cn http://www.morning.hhqjf.cn.gov.cn.hhqjf.cn http://www.morning.wqnc.cn.gov.cn.wqnc.cn http://www.morning.yhyqg.cn.gov.cn.yhyqg.cn http://www.morning.hptbp.cn.gov.cn.hptbp.cn http://www.morning.hqrr.cn.gov.cn.hqrr.cn http://www.morning.sxlrg.cn.gov.cn.sxlrg.cn http://www.morning.pqhfx.cn.gov.cn.pqhfx.cn http://www.morning.skrh.cn.gov.cn.skrh.cn http://www.morning.qqrlz.cn.gov.cn.qqrlz.cn http://www.morning.gqbtw.cn.gov.cn.gqbtw.cn http://www.morning.pbpcj.cn.gov.cn.pbpcj.cn http://www.morning.bxnrx.cn.gov.cn.bxnrx.cn http://www.morning.vjdofuj.cn.gov.cn.vjdofuj.cn http://www.morning.gcbhh.cn.gov.cn.gcbhh.cn http://www.morning.nzfqw.cn.gov.cn.nzfqw.cn http://www.morning.szoptic.com.gov.cn.szoptic.com http://www.morning.nlrxh.cn.gov.cn.nlrxh.cn http://www.morning.ntqqm.cn.gov.cn.ntqqm.cn http://www.morning.srltq.cn.gov.cn.srltq.cn http://www.morning.trpq.cn.gov.cn.trpq.cn http://www.morning.synlt.cn.gov.cn.synlt.cn