21 changed files with 177 additions and 34 deletions
Binary file not shown.
@ -0,0 +1,118 @@
@@ -0,0 +1,118 @@
|
||||
package com.kanglai.push.service; |
||||
|
||||
import android.app.Service; |
||||
import android.content.Intent; |
||||
import android.os.IBinder; |
||||
|
||||
import androidx.annotation.Nullable; |
||||
|
||||
import com.amap.api.location.AMapLocation; |
||||
import com.amap.api.location.AMapLocationClient; |
||||
import com.amap.api.location.AMapLocationClientOption; |
||||
import com.amap.api.location.AMapLocationListener; |
||||
import com.blankj.utilcode.util.LogUtils; |
||||
import com.kanglai.push.util.TimeFormatUtil; |
||||
|
||||
/** |
||||
* 高德长时间定位 |
||||
* @Author: liusixiang007 |
||||
* @since: 2024/2/28 |
||||
*/ |
||||
public class LocationService extends Service implements AMapLocationListener { |
||||
|
||||
private AMapLocationClient aMapLocationClient = null; |
||||
|
||||
@Nullable |
||||
@Override |
||||
public IBinder onBind(Intent intent) { |
||||
return null; |
||||
} |
||||
|
||||
@Override |
||||
public void onCreate() { |
||||
super.onCreate(); |
||||
} |
||||
|
||||
|
||||
/** |
||||
* 返回位置信息 |
||||
* @param aMapLocation 返回具体的位置信息对象 |
||||
*/ |
||||
@Override |
||||
public void onLocationChanged(AMapLocation aMapLocation) { |
||||
if (aMapLocation != null) { |
||||
if (aMapLocation.getErrorCode() == 0) { |
||||
LogUtils.d( |
||||
"设备定位成功,设备所在位置:"+ |
||||
aMapLocation.getCountry() // 国家信息
|
||||
+ aMapLocation.getProvince() // 省份信息
|
||||
+ aMapLocation.getCity() // 城市信息
|
||||
+ aMapLocation.getDistrict() // 区信息
|
||||
+ aMapLocation.getStreet() // 街道信息
|
||||
+ aMapLocation.getStreetNum() // 门牌号信息
|
||||
+ "当前时间" |
||||
+ TimeFormatUtil.getCurrentTime() |
||||
); |
||||
|
||||
LogUtils.d("----------------------------------纬度:" |
||||
+ aMapLocation.getLatitude() |
||||
+ "经度" |
||||
+ aMapLocation.getLongitude()); |
||||
}else LogUtils.e("设备定位失败 - 错误信息>>>>>>>>>>>>>" + aMapLocation.getErrorCode() + ",错误信息: " + aMapLocation.getErrorInfo()); |
||||
} |
||||
} |
||||
|
||||
/** |
||||
* 通过startService()的方式启动Service的时候被调用的生命周期 |
||||
* @param intent startService时传入的inten |
||||
* @param flags lags有三个可以传入的值:0,START_FLAG_REDELIVERY和START_FLAG_RETRY |
||||
* @param startId 唯一的启动请求 |
||||
* @return |
||||
*/ |
||||
@Override |
||||
public int onStartCommand(Intent intent, int flags, int startId) { |
||||
// todo 开始定位
|
||||
locationConfigure(true); |
||||
return super.onStartCommand(intent, flags, startId); |
||||
} |
||||
|
||||
@Override |
||||
public void onDestroy() { |
||||
locationConfigure(false); |
||||
super.onDestroy(); |
||||
} |
||||
|
||||
/** |
||||
* 配置定位服务参数 |
||||
* @param start 定位状态 |
||||
*/ |
||||
private void locationConfigure(boolean start) { |
||||
if (start) { // 开始定位
|
||||
try { |
||||
aMapLocationClient = new AMapLocationClient(getApplicationContext()); |
||||
} catch (Exception e) { |
||||
throw new RuntimeException(e); |
||||
} |
||||
/** 这里要实现AMapLocationListener接口,AMapLocationListener接口只有onLocationChanged方法可以实现,用于接收异步返回的定位结果,参数是AMapLocation类型。 */ |
||||
aMapLocationClient.setLocationListener(this); // 设置定位回调监听
|
||||
AMapLocationClientOption aMapLocationClientOption = new AMapLocationClientOption(); |
||||
/** Hight_Accuracy高精度模式,Battery_Saving为低功耗模式,Device_Sensors是仅设备模式 */ |
||||
// aMapLocationClientOption.setLocationMode(AMapLocationClientOption.AMapLocationMode.Hight_Accuracy);
|
||||
// aMapLocationClientOption.setLocationMode(AMapLocationClientOption.AMapLocationMode.Battery_Saving);
|
||||
aMapLocationClientOption.setLocationMode(AMapLocationClientOption.AMapLocationMode.Device_Sensors); |
||||
aMapLocationClientOption.setNeedAddress(true); // 是否返回位置信息
|
||||
aMapLocationClientOption.setOnceLocation(false); // 是否只定位一次(默认为false)
|
||||
aMapLocationClientOption.setWifiScan(true); // 允许调用WIFI刷新-默认true
|
||||
// aMapLocationClientOption.setMockEnable(false); // 是否允许虚拟定位
|
||||
aMapLocationClientOption.setHttpTimeOut(1000 * 20); // http超时时间 20s
|
||||
aMapLocationClientOption.setInterval(1000 * 5); // 返回定位间隔时间 3s返回一次
|
||||
aMapLocationClientOption.setLocationCacheEnable(false); // 缓存机制
|
||||
|
||||
aMapLocationClient.setLocationOption(aMapLocationClientOption); |
||||
aMapLocationClient.startLocation(); // 开始定位
|
||||
}else { // 结束定位 关闭定位服务
|
||||
if (aMapLocationClient != null) aMapLocationClient.onDestroy(); |
||||
LogUtils.d("aMapLocation ----------- 定位服务已关闭"); |
||||
} |
||||
} |
||||
} |
After Width: | Height: | Size: 272 KiB |
After Width: | Height: | Size: 218 KiB |
Before Width: | Height: | Size: 176 KiB After Width: | Height: | Size: 176 KiB |
Loading…
Reference in new issue