Android GPS状态改变与监听
2016-04-21 14:53
483 查看
/** * 获取GPS当前状态 * @param context * @return */ private boolean getGPSState(Context context){ LocationManager locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE); boolean on = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER); return on; } /** * 注册监听广播 * @param context * @throws Exception */ public void ready(Context context)throws Exception{ IntentFilter filter = new IntentFilter(); filter.addAction(LocationManager.PROVIDERS_CHANGED_ACTION); context.registerReceiver(new GpsStatusReceiver(), filter); } boolean currentGPSState = false; /** * 监听GPS 状态变化广播 */ private class GpsStatusReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { String action = intent.getAction(); if (action.equals(LocationManager.PROVIDERS_CHANGED_ACTION)){ currentGPSState = getGPSState(context); } } } /** * 改变GPS状态 * @param context * @throws Exception */ public void changeGPSState(Context context)throws Exception { boolean before = getGPSState(context); ContentResolver resolver = context.getContentResolver(); if (before){ Settings.Secure.putInt(resolver,Settings.Secure.LOCATION_MODE,Settings.Secure.LOCATION_MODE_OFF); }else { Settings.Secure.putInt(resolver,Settings.Secure.LOCATION_MODE,Settings.Secure.LOCATION_MODE_HIGH_ACCURACY); } currentGPSState = getGPSState(context); }
相关文章推荐
- Android:Layout_weight的深刻理解
- 安卓活动(Activity)和碎片(Fragment)的生命周期
- 一起动手编写Android图片加载框架
- Android之View覆盖问题
- Android中Binder机制实现进程间通信
- Android language(简体,繁体)
- android studio首次打开
- color用法大全--Android布局背景颜色设置
- Android生成带LOGO图片二维码的方法
- Android天气预报程序(七)
- 关于AndroidStudio 配置gradle和gradle插件的问题
- (转)Android屏幕适配全攻略(最权威的官方适配指导)
- Android AutoLayout全新的适配方式
- Android 学习资料整理(一)
- Android中的内存管理机制以及正确的使用方式
- Android Studio导入修改
- Android PagerAdapter简介
- Android getWidth和getMeasuredWidth 区别
- Android从零开发目录
- Android 里面的小坑