Reputation: 9135
I have added a mute button to a menu on my application and am wondering if it is possible to store the user's latest preference of either muted or unmuted for use when he/she reopens the application.
Here is the code I am using for setting mute or umute:
public void isMute() {
if(mIsMute){
mAm.setStreamMute(AudioManager.STREAM_MUSIC, false);
mIsMute = false;
}else{
mAm.setStreamMute(AudioManager.STREAM_MUSIC, true);
mIsMute = true;
}
}
Upvotes: 1
Views: 1556
Reputation: 18387
Use SharedPreferences to store the state. Read it when application starts and set current state.
I modified a little example from android documentation
public class Calc extends Activity {
public static final String PREFS_NAME = "MyPrefsFile";
@Override
protected void onCreate(Bundle state){
super.onCreate(state);
. . .
// Restore preferences
SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
mIsMute = settings.getBoolean("IsMute", false);
isMute();
}
@Override
protected void onStop(){
super.onStop();
// We need an Editor object to make preference changes.
// All objects are from android.context.Context
SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
SharedPreferences.Editor editor = settings.edit();
editor.putBoolean("IsMute", mIsMute);
// Commit the edits!
editor.commit();
}
}
Upvotes: 1