0

i am working on the an Application where i have to turn on or off the music using SwitchPreference
i have searched a lot but could not find any help of my level
i do not know how to do some thing when the SwitchPreference is Changed the code is given below

<?xml version="1.0" encoding="utf-8"?>

<SwitchPreference
    android:key="sound"
    android:summaryOff="Sound is Off"
    android:summaryOn="Sound is On"
    android:title="Sound" >
</SwitchPreference>
<SwitchPreference
    android:key="Music"
    android:summaryOff="Music is Off"
    android:summaryOn="Music is On"
    android:title="background Music" >
</SwitchPreference>

<CheckBoxPreference
    android:key="Time"
    android:summaryOff="Time Reduced to 1 mint"
    android:summaryOn="Time increased to 1:40 mint"
    android:title="Minimum Time" >
</CheckBoxPreference>

the preferences class is here

import android.content.SharedPreferences;
import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
import android.os.Bundle;
import android.preference.PreferenceActivity;
import android.preference.SwitchPreference;
import android.util.Log;

public class PrefrencesClass extends PreferenceActivity{

backGroundMusic bckMusic = new backGroundMusic();
SwitchPreference swichpref;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    addPreferencesFromResource(R.xml.pref);
    swichpref = (SwitchPreference) findPreference("Music");
}
}  

in the mainClass i Called this like this

public void onClick(View v) {  
switch (v.getId()) {  
case R.id.setting:
Intent prefIntent = new Intent(this,PrefrencesClass.class);  
startActivity(prefIntent);
break;
}}  

what is next
please help

Sultan Ali
  • 2,497
  • 28
  • 25

1 Answers1

0

You have several options:

Since this appears to be a "global preference", you can in onResume/Start check the value of your switch preferences by reading the key from shared preference.

You can also setup a SharedPreferences.onSharedPreferenceChangeListener, see this answer for some caveats.

Community
  • 1
  • 1
Finn K
  • 620
  • 3
  • 8
  • onSharedPreferenceChanged let me Try it – Sultan Ali Apr 16 '15 at 11:55
  • not working please share some code to make it work, i used onSharedPreferenceChanged, onResume(), onPuse() methods. Please share some code, i am beginner – Sultan Ali Apr 17 '15 at 15:09
  • For onSharedPreferenceChanged, do you see any calls to this? Did you register the listener? Without seeing your source it is hard to tell what is going on. – Finn K Apr 20 '15 at 15:23