Android中如何监听GPS开启和关闭
2017-01-03 09:36
776 查看
转自 chenming 原文 Android中如何监听GPS开启和关闭
摘要: 本文简单总结了如何监听GPS开关的小技巧
有时需要监听GPS的开关(这种需求并不多见)。实现的思路是监听代表 GPS 开关的 URI 的数据是否有发生变化
GPS的开关是系统设置,android.provider.Settings.Secure 代表了app可以读取但不能修改的系统设置。可以从这个类中获取到代表GPS开关的URI。
关键代码如下
完整的代码如下:
摘要: 本文简单总结了如何监听GPS开关的小技巧
有时需要监听GPS的开关(这种需求并不多见)。实现的思路是监听代表 GPS 开关的 URI 的数据是否有发生变化
GPS的开关是系统设置,android.provider.Settings.Secure 代表了app可以读取但不能修改的系统设置。可以从这个类中获取到代表GPS开关的URI。
关键代码如下
getContentResolver() .registerContentObserver( Settings.Secure .getUriFor(Settings.System.LOCATION_PROVIDERS_ALLOWED), false, mGpsMonitor);
完整的代码如下:
public class MyActivity extends Activity { private final ContentObserver mGpsMonitor = new ContentObserver(null) { @Override public void onChange(boolean selfChange) { super.onChange(selfChange); boolean enabled = mLocationManager .isProviderEnabled(LocationManager.GPS_PROVIDER); System.out.println("gps enabled? " + enabled); } }; private LocationManager mLocationManager; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); mLocationManager = (LocationManager) getSystemService(LOCATION_SERVICE); } protected void onStart() { getContentResolver() .registerContentObserver( Settings.Secure .getUriFor(Settings.System.LOCATION_PROVIDERS_ALLOWED), false, mGpsMonitor); }; @Override protected void onStop() { super.onStop(); getContentResolver().unregisterContentObserver(mGpsMonitor); } }
相关文章推荐
- Android中如何监听GPS开启和关闭
- Android中如何监听GPS开启和关闭
- Android中如何监听GPS开启和关闭
- Android--监听GPS开启和关闭
- 进阶:如何编程实现开启或关闭GPS?
- 【Android】代码开启/关闭GPS .
- Android中如何打开或关闭GPS
- Android中如何通过程序打开或关闭GPS
- 如何编程实现开启或关闭GPS
- 如何编程实现开启或关闭GPS?
- [Android]开启、关闭GPS/Wifi/Bluetooth/Sync,调节屏幕亮度
- Android 屏幕亮度调节(2.0以后出现亮度自动调节)如何开启、关闭亮度自动调节
- Android 如何打开和关闭GPS设备
- 如何编程实现开启或者关闭GPS
- 进阶:如何编程实现开启或关闭GPS?
- Android开启/关闭/监听 飞行模式
- 如何编程实现开启或关闭GPS?
- 【转载】如何开启Android的虚拟GPS功能
- Android监听键盘开启关闭的方法
- Android 如何开启与关闭adb 的认证机制(google adb secure) (adb RSA 指纹认证)