Question

I am a beginner of the android and I try to making a simple flashlight app with preferences activity. So my problem is; I want to make light and sound settings are optional with checkboxes.

settings.xml

  <PreferenceCategory
     android:title="@string/sct_behaviour">

     <CheckBoxPreference
       android:key="pref_light"
       android:title="@string/st_pref_light"
       android:summary="@string/ss_pref_light"
       android:defaultValue="true"
       />
    <CheckBoxPreference
       android:key="pref_switch_sound"
       android:title="@string/st_pref_sound"
       android:summary="@string/ss_pref_sound"
       android:defaultValue="true"
       />
    <CheckBoxPreference
       android:key="pref_notification_sound"
       android:title="@string/st_pref_notification_sound"
       android:summary="@string/ss_pref_notification_sound"
       android:defaultValue="true"
       />

settings.java

public class Settings extends PreferenceActivity{

@SuppressWarnings("deprecation")
@Override
public void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    addPreferencesFromResource(R.xml.settings);

    CheckBoxPreference pLight = (CheckBoxPreference)getPreferenceManager().findPreference("pref_light");

    pLight.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {

        @Override
        public boolean onPreferenceChange(Preference preference, Object newValue) {

            boolean myValue = (Boolean) newValue;

            if(myValue){
            }

                    //NEED SOME HELP IN THERE


            return true;
        }
    });

my turnOnFlash function;

 private void turnOnFlash() {
    if (!isFlashOn) {
        if (camera == null || params == null) {
            return;
        }

        params = camera.getParameters();
        params.setFlashMode(Parameters.FLASH_MODE_TORCH);
        camera.setParameters(params);
        camera.startPreview();
        isFlashOn = true;

        // changing button/switch image
        toggleButtonImage();

     // play sound
        playSound();
    }
}

onStart function

@Override
protected void onStart() {
    super.onStart();
    if(VERBOSE) Log.v(TAG, "++ ON START ++");
    // on starting the app get the camera params
    getCamera();
turnOnFlash(value);
}

I want to set this true or false value to onStart cycle by the user choice. and don't know how to make this implementation.

Was it helpful?

Solution 2

I think you should use; clicklistener

    Preference pLight = (Preference)findPreference("pref_light");
    Preference pSwitchSound = (Preference)findPreference("pref_switch_sound");
    Preference pNotificationSound = (Preference)findPreference("pref_notification_sound");

    pLight.setOnPreferenceClickListener(this);
    pSwitchSound.setOnPreferenceClickListener(this);
    pNotificationSound.setOnPreferenceClickListener(this);

OTHER TIPS

if i understand correctly you should get preferences onResume() and act accordingly :

protected void onResume() {
    super.onResume();
    if(VERBOSE) Log.v(TAG, "++ ON START ++");
    // on starting the app get the camera params

    SharedPreferences prefs =   PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
    boolean lightPref = prefs.getBoolean("pref_light", true);
    getCamera();
    turnOnFlash(lightPref);
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top