Code example for CompoundButton

Methods: setChecked, setOnCheckedChangeListener

0
 
    public FlashlightController(Context context, CompoundButton checkbox) {
        mContext = context;
        mFlashLight = getFlashLight();
        mCheckBox = checkbox;
        checkbox.setChecked(mFlashLight);
        checkbox.setOnCheckedChangeListener(this);
    } 
 
    public void onCheckedChanged(CompoundButton view, boolean checked) {
        Intent i = new Intent("net.cactii.flash2.TOGGLE_FLASHLIGHT");
        i.putExtra("bright", !checked);
        mContext.sendBroadcast(i);
    } 
 
    private boolean getFlashLight() { 
        ContentResolver cr = mContext.getContentResolver();
        return Settings.System.getInt(cr, Settings.System.TORCH_STATE, 0) == 1;
    } 
}