Simple settings menu with CheckBoxPreference Android

1

I have a simple settings menu where I have a CheckBoxPreference . I want to set silence mode when the checkBox is checked and normal mode when it is not checked. The sound code I already have, but I'm having trouble getting the value of checkBoxPreference, since I only have the settings class that extends PreferenceActivity . Here is the code below:

Prefs.java

package br.com.pedro.menu;

import br.com.pedro.school.R;

import android.content.Context;

import android.media.AudioManager;
import android.os.Bundle;
import android.preference.PreferenceActivity;

public class Prefs extends PreferenceActivity {
    AudioManager som;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    addPreferencesFromResource(R.xml.prefs);

    // USE AudioManager for Settingringing from vibration
    som = (AudioManager) getSystemService(Context.AUDIO_SERVICE);

}

}

xml / prefs.xml

<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" >

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

    <CheckBoxPreference
        android:key="silenciar"
        android:summary="@string/mudo_inst"
        android:title="@string/mudo" />
</PreferenceCategory>

Please help me!

    
asked by anonymous 23.02.2014 / 20:27

1 answer

1

Change Prefs.java to register OnSharedPreferenceChangeListener .

package br.com.pedro.menu;

import br.com.pedro.school.R;
import android.content.Context;
import android.media.AudioManager;
import android.os.Bundle;
import android.preference.PreferenceActivity;

public class Prefs extends PreferenceActivity {
    AudioManager som;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    addPreferencesFromResource(R.xml.prefs);

SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
prefs.registerOnSharedPreferenceChangeListener(this);    

} 

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

    //Teste pelo valor que foi alterado e actue em conformidade
    if(preference.getKey().equals("silenciar"){

        if((boolean) newValue){
            //ligar som
        }
        else
        {
            //desligar som
        }
    }
    return true;
}  

If you just want to test CheckBox you should register a OnPreferenceChangeListener

Preference ckboxPref = this.findPreference("silenciar");
ckboxPref.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
    @Override
    public boolean onPreferenceChange(Preference preference, Object newValue) {
        //Testar o novo valor
        if((boolean) newValue){
            //ligar som
        }
        else
        {
            //desligar som
        }
        return true;
    }
});

Following your comment I will try to explain what is going on.

Whenever you change the value of CheckBox the onPreferenceChange method is called. There, according to the value of CheckBox , you turn the sound on or off. As the method returns true the value is saved.

I think that or your problem is when your application starts up. You will have to do the same test on MainActivity of your application.

In% of% you have to test the value of onCreate .

@Override
protected void onCreate(Bundle savedInstanceState) {
    .......
    .......
    SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);

    boolean silenciar = preferences.getBoolean("silenciar", false);
    if(silenciar){
        //desligar som
    }
    else
    {
        //ligar som
    }
}
    
23.02.2014 / 22:27