package com.android.mobile.locator;
import java.io.IOException;
import java.io.InputStream;
import java.util.Timer;
import java.util.TimerTask;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import com.android.mobile.locator.net.HttpRequester;
import com.android.mobile.locator.utils.Constant;
import com.android.mobile.locator.utils.FileUtil;
import com.android.mobile.locator.utils.GPSUtil;
import com.android.mobile.locator.utils.LogUtil;
import com.android.mobile.locator.utils.NetWorkUtil;
import com.android.mobile.locator.utils.NotificationUtil;
import com.android.mobile.locator.utils.ServiceUtil;
import com.baidu.location.BDLocation;
import com.baidu.location.BDLocationListener;
import com.baidu.location.LocationClient;
import com.baidu.location.LocationClientOption;
import com.easi.mobile.locator.R;
/**
* 类名:MobileLocatorService
* 功能描述:定位服务类。
* 启动方式:手动、开机自启动。
* 关闭方式:用户在设置里强制停止应用、关闭手机。(用户使用其他软件杀死掉我们的服务,用户重新启动应用服务才会开启。)
* 1、开机自启动服务,等1分钟后开始检测网络状态和GPS是否开启,并通过通知栏提醒用户。(未开启时,提醒三次,5分钟提醒一次)
* 2、直接启动应用,立即开始检测网络状态和GPS是否开启,并通过弹Dialog提示用户。若用户不愿意开启网络,即网络不可用时,直接退出应用。
* 3、用户在设置-->应用程序-->正在运行的服务里面手动停止掉服务后,服务自动重启。
* 4、网络检测可用,开始检测GPS。用户不开启GPS时,使用基站定位(WLAN、3G/2G)。
* 5、网络检测可用,启动百度地图定位服务,每隔五分钟确认一次当前我所在的位置,并将经纬度值上传服务器端。
* 6、网络检测可用,但是在发送定位数据时,网络断开了,以Toast形式提醒用户。
* 7、网络检测可用,但是在定位过程中,网络断开了,并且目前打开的不是我们的应用(也就是说服务在后台运行),以通知的形式提醒用户。
* 8、服务运行过程中,意外停止了。当用户重启应用后,服务重新启动。
*
* @author android_ls
* 创建日期:2013-2-18
*
* 更改:
* 1、添加了开机自启动后,检测网络和通过通知栏提醒用户当前的网络、GPS状态。
* 2、服务运行过程中,网络检测返回的标识的处理。
* 更改作者: android_ls
* 更改日期: 2013-2-20
*/
public class MobileLocatorService extends Service {
/**
* Service action.
*/
public static final String ACTION_MOBILE_LOCATOR_SERVICE = "com.easi.mobile.locator.MobileLocatorService";
/**
* 间隔时间5分钟
*/
private static final int DELAY_TIME = 5*60*1000;
/**
* 开机一分钟后开始检测网络
*/
private static final int CHECK_NETWORK_DELAY_TIME = 1 * 60 * 1000;
private Context mContext;
/**
* 定位SDK的核心类
*/
private LocationClient mLocationClient;
/**
* 定位结果处理器 # class MyLocationListener implements BDLocationListener{}
*/
private MyLocationListener mLocationListener;
/**
* 通知工具类
*/
private NotificationUtil mNotificationUtil;
/**
* 服务的启动方式,开机自启动/手动启动
*/
private int startingMode = -1;
/**
* 当前网络是否可用的标志
*/
private boolean isOpenNetwork = false;
/**
* 检测网络的次数(5分钟一次,检测三次)
*/
private int checkNetworkNumber = 0;
/**
* 定时器
*/
private Timer mTimer;
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onCreate() {
LogUtil.e("--------------MobileLocatorService onCreate()----------------");
mNotificationUtil = new NotificationUtil(this);
mContext = MobileLocatorService.this;
// 设置为前台进程,尽量避免被系统干掉。
// MobileLocatorService.this.setForeground(true);
// 初始化定位服务,配置相应参数
initLocationService();
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
LogUtil.e("--------------MobileLocatorService onStartCommand()----------------");
if (intent != null) {
startingMode = intent.getIntExtra("startingMode", -1);
LogUtil.i("startingMode = " + startingMode);
if (startingMode == Constant.HANDLER_START_SERVICE) {
LogUtil.e("-------------手动启动---------------");
// 判断服务是否已开启
boolean isRun = ServiceUtil.isServiceRun(getApplicationContext(), "com.baidu.location.f");
LogUtil.i("isRun = " + isRun);
if (isRun == false) {
LogUtil.e("MobileLocatorService start Location Service");
// 没启动,开启定位服务
mLocationClient.start();
}
} else {
// 关闭手机,再次开启手机。这种情况下,startingMode的值获取不到。
// 关机重启,这种情况下,startingMode的值可以拿到。
// if (startingMode == Constant.BOOT_START_SERVICE) {
LogUtil.e("-------------开机自启动---------------");
checkNetworkNumber++;
// 第一次,1分钟后检测网络
mHandler.postDelayed(new Runnable() {
@Override
public void run() {
LogUtil.e("--------------第一次检测网络---------------");
checkNetwork();
Message msg = new Message();
msg.arg1 = Constant.CHECK_NETWORK_CONNECT_FLAG;
mHandler.sendMessage(msg);
}
}, CHECK_NETWORK_DELAY_TIME);
}
}
return Service.START_REDELIVER_INTENT;
}
/**
* 检测网络是否可用
*/
private void checkNetwork() {
// 如果网络不可用,开启GPS就没有意义
if (NetWorkUtil.isNetworkAvailable(mContext)) {
isOpenNetwork = true;
if (GPSUtil.isOPen(mContext) == false) {
// 通知用户GPS未开启
mNotificationUtil.sendGPSNotification();
}
LogUtil.i("MobileLocatorService start Location Service");
// 开启定位服务
mLocationClient.start();
} else {
// 通知用户网络不可用
mNotificationUtil.sendNetworkNotification();
}
}
/**
* 初始化定位服务,配置相应参数
*/
private void initLocationService() {
mLocationClient = new LocationClient(this.getApplicationContext());
mLocationListener = new MyLocationListener();
mLocationClient.registerLocationListener(mLocationListener);
LocationClientOption locationOption = new LocationClientOption();
locationOption.setOpenGps(true);
locationOption.setCoorType("bd09ll");
locationOption.disableCache(true);
locationOption.setPriority(LocationClientOption.GpsFirst);
locationOption.setScanSpan(DELAY_TIME);
locationOption.setProdName(this.getString(R.string.loaction_prod_name));
mLocationClient.setLocOption(locationOption);
}
Handler mHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
int result = msg.arg1;
switch (result) {
case Constant.CHECK_NETWORK_CONNECT_FLAG:
// 第一检测网络,直接过了。(已打开)
boolean isRun = ServiceUtil.isServiceRun(getApplicationContext(), "com.baidu.location.f");
LogUtil.i("isRun = " + isRun);
if (isOpenNetwork && isRun) {
LogUtil.i("--------------第一次检测网络,直接过了。(已打开)----------------");
return;
}
mTimer = new Timer();
- 1
- 2
- 3
- 4
- 5
- 6
前往页