我在fragment开启一个service,当高德开始定位之后,会自动触发service的onDestroy方法.代码如下
public class MyService extends Service implements AMapLocationListener {
private static ScheduledExecutorService mExecutorService;
private PowerManager pm;
private PowerManager.WakeLock wakeLock;
//声明mlocationClient对象
public AMapLocationClient mlocationClient;
//声明mLocationOption对象
public AMapLocationClientOption mLocationOption = null;
@Override
public void onCreate() {
super.onCreate();
initLocal();
NotificationManager manager =
(NotificationManager) getSystemService(NOTIFICATION_SERVICE);
Notification notification = null;
//第二步:实例化通知栏构造器NotificationCompat.Builder:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {//判断API
@SuppressLint("WrongConstant") NotificationChannel mChannel = new NotificationChannel("1", "配送通知",
NotificationManager.IMPORTANCE_HIGH);
manager.createNotificationChannel(mChannel);
notification = new NotificationCompat.Builder(this, "1")
.setContentTitle("订单正在配送中")//设置通知栏标题
.setContentText("请不要关闭此通知") //设置通知栏显示内容
.setWhen(System.currentTimeMillis())//通知产生的时间。
// 会在通知信息里显示,通常是系统获取到的时间
.setSmallIcon(R.mipmap.icon)//设置通知小ICON
.setLargeIcon(BitmapFactory.decodeResource(getResources()
, R.mipmap.icon))//设置通知大ICON
.build();
} else {
NotificationCompat.Builder notificationBuilder =
new NotificationCompat.Builder(this, "1")
.setContentTitle("订单正在配送中")
.setContentText("请不要关闭此通知")
.setSmallIcon(R.mipmap.icon)
.setLargeIcon(BitmapFactory.decodeResource(getResources()
, R.mipmap.icon));
// .setOngoing(true);
notification = notificationBuilder.build();
}
//第三步:对Builder进行配置:
startForeground(1, notification);
mExecutorService = Executors.newSingleThreadScheduledExecutor();
mExecutorService.scheduleAtFixedRate(new Runnable() {
@Override
public void run() {
}
}, 0, 60000, TimeUnit.MILLISECONDS);
}
下面是开启定位
private void initLocal() {
mlocationClient = new AMapLocationClient(getApplicationContext());
//初始化定位参数
mLocationOption = new AMapLocationClientOption();
//设置定位监听
mlocationClient.setLocationListener(this);
//设置定位模式为高精度模式,Battery_Saving为低功耗模式,Device_Sensors是仅设备模式
mLocationOption.setLocationMode(AMapLocationClientOption.AMapLocationMode.Hight_Accuracy);
//设置定位间隔,单位毫秒,默认为2000ms
mLocationOption.setInterval(60 * 1000);
mLocationOption.setMockEnable(false);
mLocationOption.setNeedAddress(true);
mLocationOption.setGpsFirst(true);
mLocationOption.setOnceLocation(false);
mLocationOption.setOnceLocationLatest(false);
//设置定位参数
mlocationClient.setLocationOption(mLocationOption);
// 此方法为每隔固定时间会发起一次定位请求,为了减少电量消耗或网络流量消耗,
// 注意设置合适的定位时间的间隔(最小间隔支持为1000ms),并且在合适时间调用stopLocation()方法来取消定位请求
// 在定位结束后,在合适的生命周期调用onDestroy()方法
// 在单次定位情况下,定位无论成功与否,都无需调用stopLocation()方法移除请求,定位sdk内部会移除
//启动定位
mlocationClient.stopLocation();
mlocationClient.startLocation();
}
下面是销毁的方法
@Override
public void onDestroy() {
wakeLock.release();
super.onDestroy();
Log.e("--------->","onDestroy");
if(mExecutorService!=null){
mExecutorService.shutdown();
mExecutorService = null;
}
if (mlocationClient != null) {
mlocationClient.onDestroy();
}
}
下面是运行之后打印的log
最后一张图是打印的log,可以看到直接触发onDestroy方法了,
有没有人知道这个应该怎么处理
onDestroy():当Service不再使用时,由系统调用。
会不会因为下面这个导致的,如有帮助麻烦采纳