Question

My application have a switchview. It used to lock some messages.It works fine, but the problem is in the state of switch..if i on the switch means message is locked, after i close the app and when i restart the app, its state changed to previous one, means message is unlocked.

I want to save state where i drag the switch earlier. Here is my code:

@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
    // TODO Auto-generated method stub      
    if (isChecked) {
        try{
            Database_SMS info = new Database_SMS(this);
            String data = info.getData();
            info.close();

            Global.lock = true;
            Toast.makeText(getApplicationContext(), "Message is locked", Toast.LENGTH_LONG).show();
            Vibrator v = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
            // Vibrate for 500 milliseconds
            v.vibrate(500);

            }catch(Exception e)
            {
                Toast.makeText(getApplicationContext(), "Message is not selected", Toast.LENGTH_LONG).show();
            }
    } 
    else {
        Global.lock = false;
        Toast.makeText(getApplicationContext(), "Message is unlocked", Toast.LENGTH_LONG).show();
        Vibrator v = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
        // Vibrate for 500 milliseconds
        v.vibrate(500);
    }
}

Please help me.

Was it helpful?

Solution

You should use SharedPreferences for saving this type of information and you can lock/unlock messages in init time.

Storing value you can do something like this:

SharedPreferences prefs = this.getSharedPreferences("com.example.app", Context.MODE_PRIVATE);
prefs.edit().putBoolean("value_name", true).commit();

And reading value:

boolean value = prefs.getBoolean("value_name", false); 
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top