0

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.

Jaeger
  • 1,646
  • 8
  • 27
  • 59
  • Use an AlarmManager to schedule a call to your service every 60 seconds. – Rick Sanchez Dec 27 '15 at 21:35
  • 1
    But, is that going to make my app battery-eater ? plus, if the phone is closed ( screen off ), will it wake the phone up? I want the service to run in the background even if the phone is locked, but don't want it to eat the battery. – Jaeger Dec 27 '15 at 21:52
  • Well it will eat up a bit of battery everytime the service runs, but it won't be running continually. Though on second thought, you might be better of registering a broadcastReceiver to be notified of WiFi state change. See : http://stackoverflow.com/questions/10733121/broadcastreceiver-when-wifi-or-3g-network-state-changed – Rick Sanchez Dec 27 '15 at 21:57
  • I've updated the OP and included a service example which i used, and for the screen off, if i want my code to run only once in screen off mod, but i want the code to check every minute or a period of time about wifi in screen on mode, how to add both of these in one code ? – Jaeger Dec 27 '15 at 22:00
  • Check the above comment about the BroadcastReceiver. You won't have to check many times whether the WiFi is off/on, you will be notified of the state change. – Rick Sanchez Dec 27 '15 at 22:03
  • Just tested it out, used AlarmManager for it, and it's working, but only with a problem, it's repeating it self without if condition, I've set boolean with shared preference, to make the AlarmManager works only if boolean = true, but it's just repeating it itself even if it's false, updated the title & code . – Jaeger Dec 28 '15 at 03:16
  • Can u add your comment as an answer ? I've managed to make it with "if" and other stuff, and added conditions . – Jaeger Dec 31 '15 at 22:22
  • Why would you want to poll for internet every 60 seconds? Why not make it event driven according to changes in the internet by knowing its connected or not by listening to CONNECTIVITY_CHANGED event and checking there? – JoxTraex Dec 31 '15 at 22:33
  • You mean starting the whole service only if it's not connected to WiFi, rather than checking for wifi state in the alarm ? I'm sorry but didn't get your comment . – Jaeger Jan 01 '16 at 01:36
  • 1
    @AboHani Just a heads up, alarms reset on device shutdown/restart, so you should set them again. Maybe you know this, but it's a common mistake, so I thought I'd point it out. – Rick Sanchez Jan 01 '16 at 01:37
  • Yeah, I've noticed that, and found to set the Alarm on Boot, just testing it out to find out if it will work or not. – Jaeger Jan 01 '16 at 01:40
  • Just a question please, if the user removed my app from Recent Apps, that will stop my service, right? plus, if he opened the app again, the service will continue its work ? or he must re-enable it ? – Jaeger Jan 01 '16 at 02:19

2 Answers2

1

In your service class you need to override the onStartCommand methode, then start the service by using Intent serviceIntent = new Intent(this, ServiceName.class); startService(serviceIntent);

Hope that helped you :)

1

You can use an AlarmManager to schedule a call to your service every 60 seconds, or perhaps a BroadcastReceiver to be notified of WiFi state change.

Rick Sanchez
  • 4,528
  • 2
  • 27
  • 53