8 changed files with 270 additions and 96 deletions
@ -0,0 +1,113 @@
@@ -0,0 +1,113 @@
|
||||
package com.kanglai.push.util; |
||||
|
||||
import static android.content.Context.ALARM_SERVICE; |
||||
|
||||
import android.app.AlarmManager; |
||||
import android.app.PendingIntent; |
||||
import android.content.BroadcastReceiver; |
||||
import android.content.Context; |
||||
import android.content.Intent; |
||||
import android.os.SystemClock; |
||||
|
||||
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; |
||||
|
||||
/** |
||||
* 报警广播 |
||||
* |
||||
*在一定时间之后 获取/上传一次定位数据 |
||||
* @Author: liusixiang007 |
||||
* @since: 2024/3/8 |
||||
*/ |
||||
public class AlarmReceiver extends BroadcastReceiver implements AMapLocationListener { |
||||
|
||||
private AMapLocationClient aMapLocationClient = null; |
||||
|
||||
/** 在特定时间点或经过一定时间后执行某个操作 */ |
||||
private AlarmManager mAlarmManager = null; |
||||
|
||||
/** 用于表示即将发生的操作的意图 */ |
||||
private PendingIntent pendingIntent; |
||||
|
||||
/** 每次定位间隔时间 */ |
||||
private int TIME_INTERVAL = 30 * 1000; |
||||
|
||||
@Override |
||||
public void onReceive(Context context, Intent intent) { |
||||
|
||||
if (intent.getAction().equals("com.kanglai.push.util.AlarmReceiver")) { // 判断从哪里打开的广播
|
||||
if (HistoryMsgUtil.noneId()) context.startService(new Intent(context, LocationService.class)); |
||||
return; |
||||
} |
||||
|
||||
mAlarmManager = (AlarmManager) context.getSystemService(ALARM_SERVICE); // 获取系统级别的 AlarmManager 服务实例
|
||||
Intent alarmIntent = new Intent(context, AlarmReceiver.class); |
||||
alarmIntent.setAction("youxing_warn"); |
||||
/** 且如果已经存在相同 Intent 的 PendingIntent,则更新当前的 PendingIntent */ |
||||
pendingIntent = PendingIntent.getBroadcast(context, 0, alarmIntent, PendingIntent.FLAG_UPDATE_CURRENT); |
||||
|
||||
// 这里需要设置一个类似于闹钟的配置 间隔一定时间之后 唤醒
|
||||
// Android系统 大于等于6时 - setExactAndAllowWhileIdle 设置精确的闹钟,即使应用处于低功耗模式(Doze 模式)或应用处于后台并且在 Idle 状态下也可以触发
|
||||
mAlarmManager.setExactAndAllowWhileIdle(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime() + TIME_INTERVAL, pendingIntent); |
||||
// Android 大于等于4 - setExact 设置精确的闹钟,即在指定的时间精确唤醒设备执行任务
|
||||
// mAlarmManager.setExact(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime() + TIME_INTERVAL, pendingIntent);
|
||||
|
||||
LogUtils.i("--------------------------------AlarmReceiver---------------------------------------"); |
||||
/** 上传之前需要判断一次用户登录状态 即 id 时候存在 */ |
||||
locationConfigure(HistoryMsgUtil.noneId(),context); |
||||
} |
||||
|
||||
/** 获取定位数据 */ |
||||
private void locationConfigure(boolean start, Context context) { |
||||
if (start) { // 开始定位
|
||||
try { |
||||
aMapLocationClient = new AMapLocationClient(context); |
||||
} 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(true); // 是否只定位一次(默认为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("-----------定位服务未开始"); |
||||
} |
||||
} |
||||
|
||||
@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() + "-----" + TimeFormatUtil.getCurrentTime()); |
||||
}else LogUtils.e("设备定位失败 - 错误信息>>>>>>>>>>>>>" + aMapLocation.getErrorCode() + ",错误信息: " + aMapLocation.getErrorInfo()); |
||||
} |
||||
} |
||||
} |
Loading…
Reference in new issue