android 监听GPS、蓝牙、数据漫游

GPS,蓝牙,数据漫游的开启关闭,会修改系统的数据表,通过监听数据表中数据变化来判断打开,关闭操作。


// GPS 及 网络定位
        application.getContentResolver().registerContentObserver(
                Settings.Secure.getUriFor(Settings.Secure.LOCATION_PROVIDERS_ALLOWED), false,
                settingsObserver);
        // 启用蓝牙
        application.getContentResolver().registerContentObserver(
                Settings.Secure.getUriFor(Settings.Secure.BLUETOOTH_ON), false, settingsObserver);
        // 数据漫游
        application.getContentResolver().registerContentObserver(
                Settings.Secure.getUriFor(Settings.Secure.DATA_ROAMING), false, settingsObserver);


private SettingsObserver settingsObserver = new SettingsObserver(new Handler(
            application.getMainLooper()));
 public class SettingsObserver extends ContentObserver {
        public SettingsObserver(Handler handler) {
            super(handler);
        }

        @Override
        public void onChange(boolean paramBoolean) {
            super.onChange(paramBoolean);
		
        }
    }
       //true GPS开启状态;false 关闭状态
	boolean bool1 = Settings.Secure.isLocationProviderEnabled(application.getContentResolver(), LocationManager.GPS_PROVIDER);

	
 /**
     * 评估和设置蓝牙
     */
    public boolean evaluateAndSetBluetooth() {
   	 
	BluetoothAdapter localBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
		//需要等待
            Thread.sleep(500);
	//打开蓝牙
	localBluetoothAdapter.enable();
	//关闭蓝牙
	//localBluetoothAdapter.disable();
   }
 /** 是否数据漫游 */
    public boolean isDataRoamingEnabled() {
        try {
            if (Settings.Secure.getInt(context.getContentResolver(), "data_roaming") == 0) {
                return false;
            } else {
                return true;
            }
        } catch (SettingNotFoundException e) {
            e.printStackTrace();
            return false;
        }
    }



你可能感兴趣的:(Android)