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();
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
MobileLocator.zip (71个子文件)
MobileLocator
bin
classes.dex 201KB
res
drawable-ldpi
ic_launcher.png 2KB
drawable-hdpi
ic_launcher.png 4KB
drawable-mdpi
ic_launcher.png 2KB
jarlist.cache 119B
dexedLibs
annotations-0b451d6c597d60a1a0ddd033e109d956.jar 943B
locSDK_3.1-18cbfffe553200c185ebd9044cb695ce.jar 53KB
classes
com
easi
mobile
locator
BuildConfig.class 355B
R$layout.class 409B
R$id.class 462B
R$drawable.class 422B
R$attr.class 355B
R.class 566B
R$string.class 1KB
android
mobile
locator
MobileLocatorActivity$3.class 2KB
MobileLocatorActivity$1.class 1KB
MobileLocatorService$2.class 1KB
MobileLocatorActivity$2.class 1KB
MobileLocatorService.class 9KB
MobileLocatorService$1.class 3KB
BootBroadcastReceiver.class 1KB
MobileLocatorService$MyLocationListener$1.class 2KB
net
HttpRequester.class 2KB
MobileLocatorService$1$1.class 2KB
MobileLocatorActivity.class 3KB
ShutdownBroadcastReceiver.class 2KB
utils
NetWorkUtil.class 1KB
DialogUtil.class 2KB
FileUtil.class 876B
DialogUtil$3.class 2KB
Constant.class 934B
DialogUtil$4.class 997B
NotificationUtil.class 3KB
GPSUtil.class 2KB
ServiceUtil.class 2KB
DialogUtil$1.class 1KB
LogUtil.class 1KB
DialogUtil$2.class 824B
MobileLocatorService$MyLocationListener.class 3KB
resources.ap_ 13KB
MobileLocator.apk 99KB
AndroidManifest.xml 3KB
res
drawable-ldpi
ic_launcher.png 2KB
drawable-hdpi
ic_launcher.png 4KB
values
strings.xml 2KB
drawable-mdpi
ic_launcher.png 3KB
layout
main.xml 814B
assets
gen
com
easi
mobile
locator
BuildConfig.java 165B
R.java 2KB
proguard.cfg 1KB
.settings
org.eclipse.jdt.ui.prefs 132B
org.eclipse.jdt.core.prefs 22KB
src
com
android
mobile
locator
MobileLocatorService.java 16KB
MobileLocatorActivity.java 5KB
net
HttpRequester.java 2KB
BootBroadcastReceiver.java 1KB
ShutdownBroadcastReceiver.java 1KB
utils
DialogUtil.java 3KB
GPSUtil.java 2KB
LogUtil.java 1KB
FileUtil.java 789B
Constant.java 1KB
ServiceUtil.java 1KB
NetWorkUtil.java 898B
NotificationUtil.java 4KB
.project 849B
.classpath 422B
project.properties 360B
AndroidManifest.xml 3KB
libs
armeabi
liblocSDK3.so 39KB
locSDK_3.1.jar 97KB
共 71 条
- 1
android_ls
- 粉丝: 659
- 资源: 9
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
- 1
- 2
- 3
- 4
- 5
- 6
前往页