0

I am making a small project in which i am setting the wifi to disable if the battery is less than 30%. But if the user enables the wifi again i want it must not be disable again. I did this whole work in service class The code i used is... Kindly give any suggestion. Your suggestions are very valuable for me..

public class BatteryService extends Service
    boolean checkwifi;
    WifiManager mainWifi;

    public void onCreate() {
            // TODO Auto-generated method stub
            super.onCreate();
            checkwifi = true;
            mainWifi = (WifiManager) getSystemService(Context.WIFI_SERVICE);    
        }


    if (level <= 30 && !isCharging) {
                    tts.speak(" Battery LOw Connect Phone to Charger",
                            TextToSpeech.QUEUE_FLUSH, null);
                    if (checkwifi) {
                        mainWifi.setWifiEnabled(false);
                        bt.disable();
                        closemobiledata();
                        checkwifi = false;
                    }
}

Update 1:

I found the problem, the problem is service is starting again and again What to do so that service must not start again and again

public class BatteryService extends Service
        boolean checkwifi;
        WifiManager mainWifi;

        public void onCreate() {
                // TODO Auto-generated method stub
                super.onCreate();
              Toast.makeText(getApplicationContext(), "Service created", Toast.LENGTH_SHORT).show();
                checkwifi = true;
                mainWifi = (WifiManager) getSystemService(Context.WIFI_SERVICE);    
            }


        if (level <= 30 && !isCharging) {
                        tts.speak(" Battery LOw Connect Phone to Charger",
                                TextToSpeech.QUEUE_FLUSH, null);
                        if (checkwifi) {
                            mainWifi.setWifiEnabled(false);
                            bt.disable();
                            closemobiledata();
                            checkwifi = false;
                        }
    }
nawaab saab
  • 1,892
  • 2
  • 20
  • 36
  • 1
    what you need is a Broadcast receiver to get notified when the Network connection type changes..see the following: http://stackoverflow.com/questions/10733121/broadcastreceiver-when-wifi-or-3g-network-state-changed – Alécio Carvalho May 15 '14 at 13:28

2 Answers2

0

Create a receiver for android.net.wifi.WIFI_STATE_CHANGED event. Use SharedPreferences to set userEnabledWifi to true if the wifi gets activated after you disabled it. In your service check userEnabledWifi before you disable the wifi. If battery goes above %30 again reset userEnabledWifito false so you can disable the wifi if battery goes below %30 again.

Onur
  • 5,617
  • 3
  • 26
  • 35
0

What's the difference ? You should be able to register a receiver for battery events in the service, the same way as you do with an activity. For example:

private final BroadcastReceiver mBatInfoReceiver = new BroadcastReceiver(){
@Override
public void onReceive(Context arg0, Intent intent) {
    int rawLevel = intent.getIntExtra("level", -1);
    int scale = intent.getIntExtra("scale", -1);
    int status = intent.getIntExtra("status", -1);

if (rawLevel <= 30 && !isCharging) {
                    tts.speak(" Battery LOw Connect Phone to Charger",
                            TextToSpeech.QUEUE_FLUSH, null);
                    if (checkwifi) {
                        mainWifi.setWifiEnabled(false);
                        bt.disable();
                        closemobiledata();
                        checkwifi = false;
                    }
        // Do something
    }
};

public void onCreate() {
    registerReceiver(this.mBatInfoReceiver, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
}

public void onDestroy() {
     unregisterReceiver(this.mBatInfoReceiver);
}
Santhi Bharath
  • 2,818
  • 3
  • 28
  • 42