Android监听Gps设置变化方法二

清华大佬耗费三个月吐血整理的几百G的资源,免费分享!....>>>

上一篇提到用内容观察者通过来监听数据库变化获取GPS状态,其实在GPS状态或模式改变时,

会发出相应的广播LocationManager.MODE_CHANGED_ACTION,LocationManager.PROVIDERS_CHANGED_ACTION

这里贴出关键代码:

////注册广播

IntentFilter filter = new IntentFilter();
filter.addAction(LocationManager.MODE_CHANGED_ACTION);
filter.addAction(LocationManager.PROVIDERS_CHANGED_ACTION);
mServiceBroadcast = new ServiceBroadcast();
registerReceiver(mServiceBroadcast,filter);

///接收广播并进行相应处理

String action =  intent.getAction();

if(action.equals(LocationManager.MODE_CHANGED_ACTION) || action.equals(LocationManager.PROVIDERS_CHANGED_ACTION)){

LocationManager manager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
boolean gpsEnabled = manager.isProviderEnabled(LocationManager.GPS_PROVIDER);
if(gpsEnabled){
if(!Pedometer.isServiceStart(context,"")){
bindService(new Intent(PedometerService.this,GpsService.class),gpsconnection,Context.BIND_AUTO_CREATE);
}
}
}