0

Im making an application where users are able to enable/disable the vibration of the phone. For this, I put a switch. Here is my code:

    public class settings extends Activity {


        public static boolean vibrationOnOff;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_settings);

            final Switch tButton = (Switch) findViewById(R.id.vibration);

            tButton.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

                @Override
                public void onCheckedChanged(CompoundButton buttonView,boolean isChecked) {
                    if(isChecked){
                        tButton.setChecked(true);
                        vibrationOnOff = true;
                    }else{

                        tButton.setChecked(false);
                        vibrationOnOff = false;
                    }

                }
            });

        }
}

My question: everytime when I start this activity, the switch button is always isChecked()/always on. When I uncheck the switch and go back to another acvitiy and open settins activity again, it is isChecked again.

How can I solve this?

Here is a similar question, but it did not solve my problem: Android ToggleButton always check

Community
  • 1
  • 1
Simple
  • 23
  • 2
  • 8
  • why did it not solve your problem ? What happened ? – Blackbelt Dec 12 '14 at 16:37
  • 1
    Are you using [SharedPreferences](http://developer.android.com/reference/android/content/SharedPreferences.html)? If you don't save the current settings that the user has configured, the activity won't be able to retain this information; thus it would always be created with the switch checked. – Chris Sprague Dec 12 '14 at 16:40

2 Answers2

0

You could do the following;

public class MainActivity extends Activity {
    private AudioManager mAudioManager;
    private boolean mPhoneIsSilent;

    @Override public void onCreate(Bundle savedInstanceState){
     super.onCreate(savedInstanceState);
     setContentView(R.layout.main);
     mAudioManager = (AudioManager)getSystemService(AUDIO_SERVICE);  
     checkIfPhoneIsSilent();
     setButtonClickListener();
}

    private void setButtonClickListener() {
    Button toggleButton = (Button)findViewById(R.id.toggleButton);    
    toggleButton.setOnClickListener(new View.OnClickListener() { 
    public void onClick(View v) {
        if (mPhoneIsSilent) { 
        // Change back to normal mode mAudioManager   
        .setRingerMode(AudioManager.RINGER_MODE_NORMAL); mPhoneIsSilent = false; 
        } else { 
        // Change to silent mode mAudioManager  
        .setRingerMode(AudioManager.RINGER_MODE_SILENT); mPhoneIsSilent = true; 
}
// Now toggle the UI again 
toggleUi(); 
        } 
    }); 
}

// Checks to see if the phone is currently in silent mode.

private void checkIfPhoneIsSilent() { 
    int ringerMode = mAudioManager.getRingerMode();
    if (ringerMode == AudioManager.RINGER_MODE_SILENT) { 
    mPhoneIsSilent = true; 
       } else { 
    mPhoneIsSilent = false; 
    } 
}

//Toggles the UI images from silent to normal and vice versa.

private void toggleUi() {
    ImageView imageView = (ImageView) findViewById(R.id.phone_icon);
    Drawable newPhoneImage;
    if (mPhoneIsSilent) {
    newPhoneImage = getResources().getDrawable(R.drawable.phone_silent);
    } else {
    newPhoneImage = getResources().getDrawable(R.drawable.phone_on); } 
    imageView.setImageDrawable(newPhoneImage);
}       
    @Override protected void onResume() {
    super.onResume();
    checkIfPhoneIsSilent(); 
    toggleUi(); 
}
Jax
  • 402
  • 7
  • 24
  • You would have to check if the RINGER_MODE_VIBRATE is true by declaring it as an int – Jax Dec 12 '14 at 18:20
  • int ringerMode = A.getRingerMode(); if (ringerMode == AudioManager.RINGER_MODE_VIBRATE) – Jax Dec 12 '14 at 18:28
  • Maybe I did not explain my question good. The app is vibrating in some circumstances. Users can enable and disable this vibration (thats why vibrationOnOff is public static). The vibration function is in another class. When vibrationOnOff = true, the phone will be vibrated – Simple Dec 12 '14 at 18:32
  • But as parveen says you first need to check if it is in vibrate mode or, as I understand it, it is automatically set to true whether this is accurate or not. The way I would try would be to set it as an int or a Boolean and check if it is true. It's bulkier but it worked for me in Eclipse. – Jax Dec 12 '14 at 18:38
  • Do you want it to start thinking vibrate mode is on? It seems to me(although I might be wrong) that you want it to check if it is in ringer mode or if it is not as soon as the activity is created. – Jax Dec 12 '14 at 18:43
  • So I start this activity. Users can switch this togglebutton to off. When the button is off, I got a public static variable X. When the switch is off, X = 3. When switch is on, X = 2. In another acvitivity, I call if(settings.X = 3){ vibrator.vibrate(500); } else{do not vibrate}. I hope this clarifies it a little bit. I think my question was badly described, sorry for that – Simple Dec 12 '14 at 18:49
  • I don't want to be 'spamming' but the Android Application Development for Dummies book covers a similar app, the Ringer toggles app. I could copy the code and paste it into my answer if you want.It explains it way better than I can. – Jax Dec 12 '14 at 19:08
  • Tell me if it helps ;) – Jax Dec 12 '14 at 19:50
0

First you have to determine the current state of the phone. So if the phone is in Virbration mode, keep the switch checked else unchecked.

To determine the phone state,

boolean isVibrateOn = false;
AudioManager mAudioManager = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
int ringerMode = mAudioManager.getRingerMode();
if(ringerMode == AudioManager.RINGER_MODE_VIBRATE)
    isVibrateOn = true;
else
    isVibrateOn = false;

Then set the state of switch appropriately,

final Switch tButton = (Switch) findViewById(R.id.vibration);
tButton.setChecked(isVibrateOn);

tButton.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
       @Override
       public void onCheckedChanged(CompoundButton buttonView,boolean isChecked) {
           if(isChecked){
               tButton.setChecked(true);
               vibrationOnOff = true;
           }else{
               tButton.setChecked(false);
               vibrationOnOff = false;
           }

       }
 });

Hope this solves your problem. ;) :)

Praveen Kishore
  • 436
  • 1
  • 3
  • 14
  • It did not work. Maybe I did not explain my question good. The app is vibrating in some circumstances. Users can enable and disable this vibration (thats why vibrationOnOff is public static) – Simple Dec 12 '14 at 18:16