I'm making an app, and i'm including some stuff that require to check every minute .
So, here's the quesiton .. let's take for example the WiFi, I've added the check for wifi code, and if it's enabled = make it off, ok for now ?
My problem is, this method only happens once, i want it to check every minute, if wifi is on => make it off .
But, i don't want my app to eat the battery, the main idea in the app is to save battery, not to kill it .
I've added the method in a service, and when the user click apply, it runs, but only for one time, if he enabled the wifi .. nothing happen, he needs to re-enable the option .
the title may be long, but didn't come with anything better :p
Just used AlarmManager, now im experiencing a problem, I've added SwitchPreference, and when it's enabled it will run the Alarm, but because it's too long / complex to make, I've used " sharedpreferences " with boolean, as the following code :
        boolean WiFiEnabled = prefs.getBoolean("WiFiEnabled", false);
        prefs.getBoolean("WiFiLowSpeedEnabled", false);
        if(pref == mWiFiEnable)
        {
            SharedPreferences.Editor editor = prefs.edit();
            editor.putBoolean("WiFiEnabled", true);
            editor.commit();
        }
And My alarm is as the following :
public class Alarm extends BroadcastReceiver
{
 @Override
public void onReceive(Context context, Intent intent)
{
    // Put here YOUR code.
    SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
    boolean WiFiEnabled = prefs.getBoolean("WiFiEnabled", false);
    if(WiFiEnabled)
    {
        Toast.makeText(context,"WiFi Enabled, Alarm",Toast.LENGTH_LONG).show();
        if(!MainService.isConnectedWifi(context))
        {
            WifiManager wifiManager = (WifiManager) context.getApplicationContext().getSystemService(Context.WIFI_SERVICE);
            if(wifiManager.isWifiEnabled()){
                wifiManager.setWifiEnabled(false);
            }
        }
    }
}
public void SetAlarm(Context context)
{
    AlarmManager am =( AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
    Intent i = new Intent(context, Alarm.class);
    PendingIntent pi = PendingIntent.getBroadcast(context, 0, i, 0);
    am.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), 1000 * 60 * 5, pi); // Millisec * Second * Minute
}
}
The problem that I'm having is, when the switch is on, the code will works ( which is what i want ) but when i disable the switch, it keeps running, it won't cancel .. So how to stop the alarm when the switch is off?
I've used the shared preferences as explained above.