0

I have a toggle that when BATTERY_LOW is true turns off the wifi:

public void getToggle(View view) { 
    // Is the toggle on?
   boolean on = ((ToggleButton) view).isChecked(); 
    if (on) {
            PackageManager pm = getPackageManager();
    ComponentName compName = 
    new ComponentName(getApplicationContext(), 
        LowBatteryReceiver.class);
        pm.setComponentEnabledSetting(
        compName,
        PackageManager.COMPONENT_ENABLED_STATE_ENABLED, 
        PackageManager.DONT_KILL_APP);
    } else {                
           PackageManager pm = getPackageManager();
           ComponentName compName = 
           new ComponentName(getApplicationContext(), 
           LowBatteryReceiver.class);
           pm.setComponentEnabledSetting(
           compName,
           PackageManager.COMPONENT_ENABLED_STATE_DISABLED, 
           PackageManager.DONT_KILL_APP);           
      }
    }

here the wifi code:

public class LowBatteryReceiver extends BroadcastReceiver {

    public void onReceive(Context context, Intent intent) {
        wifiManager = (WifiManager) MainActivity.this.getSystemService(Context.WIFI_SERVICE); 
                        wifiManager.setWifiEnabled(false);
    }
}


public class OkBatteryReceiver extends BroadcastReceiver {

    public void onReceive(Context context, Intent intent) {
        wifiManager = (WifiManager) MainActivity.this.getSystemService(Context.WIFI_SERVICE); 
                        wifiManager.setWifiEnabled(true);
    }
}

How can i "remember" the choice of the toggle when i go out from my application and open it again? I know that i have to write something at onResume and in the onCreate but i don't know what when i use the BroadcastReceiver.I think i have to check if the COMPONENT_ENABLED_STATE_DISABLED or COMPONENT_ENABLED_STATE_ENABLED right?.Someone can help me? Thanks

David_D
  • 1,404
  • 4
  • 31
  • 65

1 Answers1

0

I recommend you to read about saving data in Android: http://developer.android.com/training/basics/data-storage/index.html

//set Preference
SharedPreferences sharedPref = getActivity().getPreferences(Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPref.edit();
editor.putInt(getString(R.string.saved_high_score), newHighScore);
editor.commit();

//get Preference
SharedPreferences sharedPref = getActivity().getPreferences(Context.MODE_PRIVATE);
int defaultValue = getResources().getInteger(R.string.saved_high_score_default);
long highScore = sharedPref.getInt(getString(R.string.saved_high_score), defaultValue);
terra
  • 66
  • 4
  • Sorry but i don't understand..where have i to put this code exactly? – David_D Jul 02 '13 at 16:28
  • I have no idea how can do it. What have i put onResume? – David_D Jul 02 '13 at 18:01
  • use the get Preference instead of ((ToggleButton) view).isChecked() and set a OnClickListener to the togglebutton in which you set the Preference. How to add a Listener see http://stackoverflow.com/questions/8386832/android-checkbox-listener – terra Aug 21 '13 at 15:06