package com.jqyd.android.module.lbs;
import java.text.SimpleDateFormat;
import java.util.Date;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.os.IBinder;
import android.os.Looper;
import android.util.Log;
import com.jqyd.android.module.lbs.Interface.IAlarm;
import com.jqyd.android.module.lbs.Interface.ILocationListener;
import com.jqyd.android.module.lbs.bean.LocationInfo;
import com.jqyd.android.module.lbs.bean.LocationTimerTask;
import com.jqyd.android.module.lbs.util.JqydDateUtil;
import com.jqyd.android.module.lbs.util.Optsharepre_interface;
import com.jqyd.android.module.lbs.util.WriteFile;
/**
* 回调定时定位服务
* @author 荆林枭
*
*/
public abstract class LocationService extends Service implements
ILocationListener {
private long jgsj;
private long startTime;
private long endTime;
private Context context;
private int locType;
private boolean isUsePoint;
private TimingLocating tl;
private LocationTimerTask task=null;
private Optsharepre_interface share;
private WriteFile file;
@Override
public void onDestroy() {
super.onDestroy();
if(tl!=null)tl.stop();
file.writeToFile("LocationService-------------" + "onDestroy()");
}
@Override
public void onCreate() {
super.onCreate();
this.context = getApplicationContext();
share = new Optsharepre_interface(context);
file= new WriteFile("LocationServiceLog"+JqydDateUtil.getDateDayOne(new Date()));
file.writeToFile("LocationService-------------" + "onCreate()");
Log.e("xiao", "oncreate____locationService");
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.e("xiao", "onstartcommand____locationService");
file.writeToFile("判断Intent是否为空::"+intent);
if(intent!=null){
task = (LocationTimerTask) intent.getSerializableExtra("task");
}
if(task!=null){
file.writeToFile("传送过来的task::"+task.getStartTime());
this.jgsj = task.getPeriod()*60000;
this.startTime = task.getStartTime();
this.endTime = task.getEndTime();
this.locType = task.getLocationWay();
this.isUsePoint = task.isUsePoint();
file.writeToFile("间隔时间:"+jgsj+"开始时间:"+startTime+"结束时间:"+endTime+"定位模式:"+locType);
Log.e("UpLoc", "间隔时间:"+jgsj+"开始时间:"+startTime+"结束时间:"+endTime+"定位模式:"+locType);
share.editPres("KSSJ", this.startTime+"");
share.editPres("JSSJ", this.endTime+"");
share.editPres("DWLX", this.locType+"");
share.editPres("JGSJ", this.jgsj+"");
share.editPres("isUsePoint", this.isUsePoint+"");
}else{
this.jgsj = Long.parseLong(share.getDataFromPres("JGSJ"));
this.startTime = Long.parseLong(share.getDataFromPres("KSSJ"));
this.endTime = Long.parseLong(share.getDataFromPres("JSSJ"));
this.locType = Integer.parseInt(share.getDataFromPres("DWLX"));
this.isUsePoint = Boolean.parseBoolean(share.getDataFromPres("isUsePoint"));
Log.e("UpLoc", "使用本地存储的task:"+"间隔时间:"+share.getDataFromPres("JGSJ")+"开始时间:"+share.getDataFromPres("KSSJ")+"结束时间:"+share.getDataFromPres("JSSJ")+"定位模式:"+share.getDataFromPres("DWLX"));
new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss").format(this.startTime);
new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss").format(this.endTime);
file.writeToFile("使用本地存储的task:"+"间隔时间:"+share.getDataFromPres("JGSJ")+"开始时间:"+share.getDataFromPres("KSSJ")+"结束时间:"+share.getDataFromPres("JSSJ")+"定位模式:"+share.getDataFromPres("DWLX"));
}
if(isUsePoint){
tl = new TimingLocating(jgsj, startTime, endTime,LocationService.this,isUsePoint);
}else{
tl = new TimingLocating(jgsj, startTime, endTime,LocationService.this);
}
tl.registerLintener(new IAlarm() {
@Override
public void isExecute(boolean execute) {
file.writeToFile("是否可以定位:"+execute);
Log.e("UpLoc", "是否可以定位:"+execute);
if (execute) {
new Thread() {
@Override
public void run() {
Looper.prepare();
new LocationUtils(context).getLocData(locType,
LocationService.this);
Looper.loop();
}
}.start();
}
}
});
file.writeToFile("开始执行,定时任务");
tl.start();
return super.onStartCommand(intent, flags, startId);
}
protected abstract void onReceive(int locType, LocationInfo bean);
@Override
public void onReceiveLocInfo(int locType, LocationInfo bean) {
Log.e("UpLoc", "super.onLoc");
file.writeToFile("定位结果显示:"+bean.getSuccess());
onReceive(locType, bean);
}
@Override
public IBinder onBind(Intent intent) {
return null;
}
}