package com.scut.eventbusdemo.service;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.os.IBinder;
import android.util.Log;
import com.scut.eventbusdemo.model.LocationMessage;
import de.greenrobot.event.EventBus;
/**
* GPS定位服务
* victor shi
* 2015/12/9
*/
public class LocationService extends Service implements LocationListener{
// flag for GPS status
boolean isGPSEnabled = false;
// flag for network status
boolean isNetworkEnabled = false;
// flag for GPS status
boolean canGetLocation = false;
Location location; // location
String provider;
// The minimum distance to change Updates in meters
private static final long MIN_DISTANCE_CHANGE_FOR_UPDATES = 0; // 0 meters
// The minimum time between updates in milliseconds
private static final long MIN_TIME_BW_UPDATES = 1000 * 10 * 1; // 10 seconds
//LocationManager对象
private LocationManager locationManager;
@Override
public void onCreate() {
super.onCreate();
Log.i("service", "onCreate");
//获取系统locationManager服务
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
Criteria criteria = new Criteria();
// 获得最好的定位效果
criteria.setAccuracy(Criteria.ACCURACY_FINE);
criteria.setAltitudeRequired(false);
criteria.setBearingRequired(false);
criteria.setCostAllowed(false);
// 使用省电模式
criteria.setPowerRequirement(Criteria.POWER_LOW);
// 获得当前的位置提供者
provider = locationManager.getBestProvider(criteria, true);
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.i("service", "onStartCommand");
getLocation();
return super.onStartCommand(intent, flags, startId);
}
/*
通过网络或GPS获取定位信息
*/
public Location getLocation() {
try {
// getting GPS status
isGPSEnabled = locationManager
.isProviderEnabled(LocationManager.GPS_PROVIDER);
// getting network status
isNetworkEnabled = locationManager
.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
if (!isGPSEnabled && !isNetworkEnabled) {
// no network provider is enabled
} else {
this.canGetLocation = true;
// First get location from Network Provider
if (isNetworkEnabled) {
locationManager.requestLocationUpdates(
LocationManager.NETWORK_PROVIDER,
MIN_TIME_BW_UPDATES,
MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
if (locationManager != null) {
location = locationManager
.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
}
}
// if GPS Enabled get lat/long using GPS Services
if (isGPSEnabled) {
if (location == null) {
locationManager.requestLocationUpdates(
LocationManager.GPS_PROVIDER,
MIN_TIME_BW_UPDATES,
MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
if (locationManager != null) {
location = locationManager
.getLastKnownLocation(provider);
}
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
return location;
}
private void updateMessage(Location location) {
if(location != null) {
Log.i("service", "msg != null");
EventBus.getDefault().post(new LocationMessage(location));
} else {
Log.i("service", "msg = null");
EventBus.getDefault().post(new LocationMessage());
}
}
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onLocationChanged(Location location) {
Log.i("locationListener", "onLocationChanged");
updateMessage(location);
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
@Override
public void onProviderEnabled(String provider) {
}
@Override
public void onProviderDisabled(String provider) {
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
android观察者模式:EventBus实现GPS定位
共19个文件
xml:7个
java:4个
png:4个
5星 · 超过95%的资源 需积分: 9 12 下载量 73 浏览量
2015-12-10
19:50:42
上传
评论
收藏 32KB ZIP 举报
温馨提示
资源参考博客:http://blog.csdn.net/sctu_vroy/article/details/50253457
资源推荐
资源详情
资源评论
收起资源包目录
eventbusdemo.zip (19个子文件)
eventbusdemo
src
androidTest
java
com
scut
eventbusdemo
ApplicationTest.java 364B
main
AndroidManifest.xml 921B
res
mipmap-hdpi
ic_launcher.png 3KB
mipmap-xhdpi
ic_launcher.png 5KB
menu
menu_main.xml 367B
mipmap-mdpi
ic_launcher.png 2KB
drawable
layout
activity_main.xml 760B
values-w820dp
dimens.xml 364B
values
strings.xml 182B
styles.xml 202B
dimens.xml 216B
mipmap-xxhdpi
ic_launcher.png 8KB
java
com
scut
eventbusdemo
service
LocationService.java 5KB
model
LocationMessage.java 2KB
MainActivity.java 2KB
eventbusdemo.iml 7KB
libs
.gitignore 8B
build.gradle 655B
proguard-rules.pro 678B
共 19 条
- 1
资源评论
- bitera2018-02-03de.greenrobot.event.EventBus 缺少这个包,网上找了类似的,但大报后运行出错。楼主提供下这个包的下载,谢谢!
scuter_victor
- 粉丝: 49
- 资源: 15
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功