转载请注明出处:http://blog.csdn.net/sctu_vroy/article/details/50253457
Android开发过程中,我们通常会为不同组件间互相通知讯息而烦恼>~<比如说,activity与其多个附着的fragment之间,按传统方法就是用“回调”,在fragment中写接口,在activity中实现接口的方法。但,这样子做略麻烦。这里涉及到的就是“观察者模式”(JDK1.5已经实现该模式,Observable、Observer),再例如广播以及listView中notifyDatasetChanged()也都是观察者模式的典范。
greenRobot的EventBus框架就可以愉快的帮我们解决一系列相关问题,地址:https://github.com/greenrobot/EventBus,使用AndroidStudio开发则直接在Module中的build.gradle中dependencies中添加:compile 'de.greenrobot:eventbus:2.4.0'
即可使用该库。下面结合实例GPS定位代码讲解:
Step 1:创建观察者:
在activity中onCreate()中先注册EventBus,表明该activity为观察者;与此同时,在activity销毁时需要取消订阅,在onDestroy()中实现。
@Override
protected void onCreate(Bundle savedInstanceState) {
requestWindowFeature(Window.FEATURE_NO_TITLE);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//注册EventBus
EventBus.getDefault().register(this);
initView();
initData();
initEvent();
}
@Override
protected void onDestroy() {
//取消订阅
EventBus.getDefault().unregister(this);
super.onDestroy();
}
Step 2:观察者的订阅函数(个人觉得可以理解为如:AsyncTask中的onPostExcute()或者Handler中的handleMessage())。EventBus有四种订阅函数:onEventPostThread, onEventMainThread,onEventBackgroundThread,onEventAsync,具体的:
1)onEventMainThread代表这个方法会在UI线程执行;
2)onEventPostThread代表这个方法会在当前发布事件的线程执行;
3)onEventBackgroundThread这个方法,如果在非UI线程发布的事件,则直接执行,和发布在同一个线程中。如果在UI线程发布的事件,则加入后台任务队列,使用线程池一个接一个调用;
4)onEventAsync加入后台任务队列,使用线程池调用,注意没有BackgroundThread中的一个接一个。
一旦注册了EventBus,则其会通过反射机制搜索观察者中所有公有非静态的以onEvent开头的方法,并以键值对形式存储到一个map中。
本例订阅函数用于更新UI,所以在activity中创建公有(public)方法更新位置信息:
public void onEventMainThread(LocationMessage msg) {
Log.i("activity", "eventbus on");
if(msg != null) {
tvAltitude.setText(msg.getMsgString());
Toast.makeText(getApplicationContext(), "eventBus response", Toast.LENGTH_SHORT).show();
}
}
Step 3:创建被观察者对象(即观察者关注的事情),这里是GPS定位服务:
1)首先创建一个位置信息model(LocationMessage)
public class LocationMessage {
//经度
private double longitude;
//纬度
private double latitude;
//海拔
private double altitude;
//精度
private float accuracy;
//速度
private float speed;
//经纬度信息
private String msgString;
public LocationMessage() {
msgString = "GPS is closed!";
}
public LocationMessage(Location location) {
longitude = location.getLongitude();
latitude = location.getLatitude();
accuracy = location.getAccuracy();
altitude = location.getAltitude();
speed = location.getSpeed();
StringBuffer sb = new StringBuffer();
sb.append("经度:" + longitude + "\n").append("纬度:" + latitude + "\n")
.append("海拔:" + altitude + "\n").append("速度:" + speed + "\n").append("精度:" + accuracy);
msgString = sb.toString();
}
}
2)开启service用于位置信息获取、更新,此处使用网络以及GPS定位的方式,在service中实现LocationListener获取定位信息:
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;
}
@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) {
}
}
Step 4:眼尖的童鞋应该发现Step3中有updateMessage()方法,此方法就是用于EventBus发布事件的!一旦事件发布,EventBus会根据post()方法中object参数从Step2中提到的map中找到订阅了该事件的观察者中的onEvent订阅函数。
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());
}
}
凤毛菱角的补充:
1)activity中开启服务:
//开启GPS服务
Intent intent_Start = new Intent(this, LocationService.class);
Log.i("activity", "service start intent");
startService(intent_Start);
2)AndroidManifest中添加权限以及在application中注册service:
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.INTERNET"/>
<application
<service android:name=".service.LocationService"/>
</application>
小结:在activity中注册EventBus,以及订阅函数;service作为事件的发布者,通过总线EventBus定时发布位置信息(即发布事件);观察者activity更新位置信息;activity销毁时取消订阅EventBus。