I am creating an app in which when user logged in he can set settings according to his need.I have preference xml(named settings.xml). There are two options there 1) Background Music(on or off through switch preference) 2) Choose the song(list preference).Below is my main activity code:-
public class SettingsActivity extends PreferenceActivity {
MediaPlayer mp;
SharedPreferences pref;
Preference p;
MyProfile myProfile;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
pref = PreferenceManager.getDefaultSharedPreferences(this);
getFragmentManager().beginTransaction().replace(android.R.id.content,
new PrefsFragment()).commit();
PreferenceManager.setDefaultValues(SettingsActivity.this, R.xml.settings, false);
}
@SuppressLint("ValidFragment")
public class PrefsFragment extends PreferenceFragment{
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.settings);
}
@Override
public void onResume(){
super.onResume();
// Set up a listener whenever a key changes
getPreferenceScreen().getSharedPreferences().registerOnSharedPreferenceChangeListener(listener);
for (int i = 0; i < getPreferenceScreen().getPreferenceCount(); ++i) {
Preference preference = getPreferenceScreen().getPreference(i);
if (preference instanceof PreferenceGroup) {
PreferenceGroup preferenceGroup = (PreferenceGroup) preference;
for (int j = 0; j < preferenceGroup.getPreferenceCount(); ++j) {
updatePrefSummary(preferenceGroup.getPreference(j));
}
} else {
updatePrefSummary(preference);
}
}
}
@Override
public void onPause() {
super.onPause();
// Unregister the listener whenever a key changes
stopPlay();
getPreferenceScreen().getSharedPreferences().unregisterOnSharedPreferenceChangeListener(listener);
}
public void onDestroy() {
super.onDestroy();
// Unregister the listener whenever a key changes
getPreferenceScreen().getSharedPreferences().unregisterOnSharedPreferenceChangeListener(listener);
};
OnSharedPreferenceChangeListener listener = new OnSharedPreferenceChangeListener() {
@Override
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences,
String key) {
// TODO Auto-generated method stub
updatePrefSummary(findPreference(key));
String ringTone = sharedPreferences.getString("prefRingtone", "0");
int num = Integer.parseInt(ringTone);
if(num == 0){
mp= MediaPlayer.create(getApplicationContext(), R.raw.airtel);
}
else if(num == 1){
mp= MediaPlayer.create(getApplicationContext(), R.raw.dance);
}
else if(num == 2){
mp= MediaPlayer.create(getApplicationContext(), R.raw.dhoom);
}
else{
mp= MediaPlayer.create(getApplicationContext(), R.raw.guitar);
}
mp.setLooping(false);
if(sharedPreferences.getBoolean("prefMusic", false)){
mp.start();
}
else {
mp.stop();
// mp.release();
}
}
};
private void updatePrefSummary(Preference p){
if(p instanceof ListPreference){
ListPreference listPref = (ListPreference) p;
p.setSummary(listPref.getEntry());
}
else{
SwitchPreference swPref = (SwitchPreference)p;
if(pref.getBoolean("prefMusic", true)){
swPref.setSwitchTextOn("ON");
p.setSummary(swPref.getSwitchTextOn());
}
else{
swPref.setSwitchTextOff("OFF");
p.setSummary(swPref.getSwitchTextOff());
}
}
}
public void stopPlay(){
/* if(myProfile.isFinishing()){
mp.stop();
//mp.release();
}*/
Context context = getApplicationContext();
ActivityManager am = (ActivityManager)context.getSystemService(Context.ACTIVITY_SERVICE);
List<RunningTaskInfo> taskinfo = am.getRunningTasks(1);
if(!taskinfo.isEmpty()){
ComponentName topActivity = taskinfo.get(0).topActivity;
if(!topActivity.getPackageName().equals(context.getPackageName())){
mp.stop();
//mp.release();
}
else{
}
}
super.onPause();
}
}
}
My problem is that background music is not working properly when I switch on and off them respectively. Some time does not play and when it start playing, it does not stop(whether I choose off).Please help me to get rid of this.Any help would be highly appreciated.
Thanks.
ok I got the working code:-
public class SettingsActivity extends PreferenceActivity {
MediaPlayer mp;
SharedPreferences pref;
Preference p;
MyProfile myProfile;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
pref = PreferenceManager.getDefaultSharedPreferences(this);
getFragmentManager().beginTransaction().replace(android.R.id.content,
new PrefsFragment()).commit();
PreferenceManager.setDefaultValues(SettingsActivity.this, R.xml.settings, false);
}
@SuppressLint("ValidFragment")
public class PrefsFragment extends PreferenceFragment{
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.settings);
}
@Override
public void onResume(){
super.onResume();
// Set up a listener whenever a key changes
getPreferenceScreen().getSharedPreferences().registerOnSharedPreferenceChangeListener(listener);
for (int i = 0; i < getPreferenceScreen().getPreferenceCount(); ++i) {
Preference preference = getPreferenceScreen().getPreference(i);
if (preference instanceof PreferenceGroup) {
PreferenceGroup preferenceGroup = (PreferenceGroup) preference;
for (int j = 0; j < preferenceGroup.getPreferenceCount(); ++j) {
updatePrefSummary(preferenceGroup.getPreference(j));
}
} else {
updatePrefSummary(preference);
}
}
}
@Override
public void onPause() {
super.onPause();
// Unregister the listener whenever a key changes
// stopPlay();
// getPreferenceScreen().getSharedPreferences().unregisterOnSharedPreferenceChangeListener(listener);
}
public void onDestroy() {
super.onDestroy();
// Unregister the listener whenever a key changes
// stopPlay();
getPreferenceScreen().getSharedPreferences().unregisterOnSharedPreferenceChangeListener(listener);
};
OnSharedPreferenceChangeListener listener = new OnSharedPreferenceChangeListener() {
@Override
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences,
String key) {
// TODO Auto-generated method stub
updatePrefSummary(findPreference(key));
String ringTone = sharedPreferences.getString("prefRingtone", "0");
int num = Integer.parseInt(ringTone);
if(mp!=null){
if(mp.isPlaying()){
mp.stop();
}
if(num == 0){
mp= MediaPlayer.create(getApplicationContext(), R.raw.airtel);
}
else if(num == 1){
mp= MediaPlayer.create(getApplicationContext(), R.raw.dance);
}
else if(num == 2){
mp= MediaPlayer.create(getApplicationContext(), R.raw.dhoom);
}
else{
mp= MediaPlayer.create(getApplicationContext(), R.raw.guitar);
}
}
else{
if(num == 0){
mp= MediaPlayer.create(getApplicationContext(), R.raw.airtel);
}
else if(num == 1){
mp= MediaPlayer.create(getApplicationContext(), R.raw.dance);
}
else if(num == 2){
mp= MediaPlayer.create(getApplicationContext(), R.raw.dhoom);
}
else{
mp= MediaPlayer.create(getApplicationContext(), R.raw.guitar);
}
}
// mp= MediaPlayer.create(getApplicationContext(), R.raw.airtel);
mp.setLooping(true);
Boolean onORoff = sharedPreferences.getBoolean("prefMusic", false);
if(onORoff){
mp.start();
}
else {
mp.pause();
// mp.release();
}
}
};
private void updatePrefSummary(Preference p){
if(p instanceof ListPreference){
ListPreference listPref = (ListPreference) p;
p.setSummary(listPref.getEntry());
}
else{
SwitchPreference swPref = (SwitchPreference)p;
if(pref.getBoolean("prefMusic", true)){
swPref.setSwitchTextOn("ON");
p.setSummary(swPref.getSwitchTextOn());
}
else{
swPref.setSwitchTextOff("OFF");
p.setSummary(swPref.getSwitchTextOff());
}
}
}
}
}