简单,一个广播搞定
IntentFilter mIntentFilter = new IntentFilter(); mIntentFilter.addAction(LocationManager.GPS_FIX_CHANGE_ACTION); mIntentFilter.addAction(LocationManager.GPS_ENABLED_CHANGE_ACTION); Intent intent = mContext.registerReceiver(this, mIntentFilter);
@Override public void onReceive(Context context, Intent intent) {final String action = intent.getAction(); final boolean enabled = intent.getBooleanExtra( LocationManager.EXTRA_GPS_ENABLED, false); if (null == mStatusBarManager) { return; } if (action.equals(LocationManager.GPS_FIX_CHANGE_ACTION) && enabled) { // GPS is getting fixes } else if (action.equals(LocationManager.GPS_ENABLED_CHANGE_ACTION) && !enabled) { // GPS is off } else { Log.i(TAG, "" + mContext.getString(R.string.accessibility_location_active)); // GPS is on, but not receiving fixes } }