I have a question about Shread Preferences.

Asked 1 years ago, Updated 1 years ago, 100 views

// Get value
public Boolean checkPreferences(String key){
    return pref.contains(key);
}


// Load Value
public String getPreferences(String key){
    return pref.getString(key, "");
}

// Save Value
public void savePreferences(String key, String value){
    SharedPreferences.Editor editor = pref.edit();
    editor.putString(key,value);

    editor.commit();
}

// Delete Key Data
public void removePreferences(String key){
    SharedPreferences.Editor editor = pref.edit();
    editor.remove(key);
    editor.commit();
}

// To delete a value (ALL Data
public void removeAllPreferences(){
    SharedPreferences.Editor editor = pref.edit();
    editor.clear();
    editor.commit();
}

Save Value

public void savePreferences(String key, String value){ SharedPreferences.Editor editor = pref.edit(); editor.putString(key,value); editor.commit(); }

Most of the time, putString(key, value) is stored and loaded in this part.

I want to use Shread Preferences to realize the sound when counting occurs on the main screen.

The count is currently implemented on the main screen.

When implementing the sound, how to save the value and how to load the value I'm curious and there are four sounds, but I want to choose one whenever I want.

android sharedpreferences

2022-09-22 16:18

1 Answers

The data that can be stored in Shared Preferences are booleans, floats, ints, longs, strings. Therefore, it does not seem appropriate to store Shared Preferences for the sound source itself. Also, you don't need Shared Preferences to implement what you post.

I don't know exactly what counting means, but if you understand that you want to play a song under certain conditions, put the four songs you want to play in the res/raw/ folder and use MediaPlayer to play them.

MediaPlayer mediaPlayer = MediaPlayer.create(context, R.raw.sound_file_1);
mediaPlayer.start();

Read the link below for this.


2022-09-22 16:18

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.