Search code examples
javaandroidbroadcastreceiveralertsbatterymanager

How can I set up an alert that appears at all times?


Right now, I just want some sort of indication of battery status when the user access the apps. For example, if the device is plugged in, then the device should say charging and the level it's at. If the device is unplugged, nothing should be displayed. If the device is not charging and low battery, low battery indication should appear.

I've setup most of the code but the problem is, e.g., if I run the app and the device is plugged in, charging indication is shown on screen. If I unplug the device, charging is no longer visible but if I replug the device, charging is still no longer visible. The battery alerts should always display when the device is plugged or unplugged, high or low battery. Change in information should always be displayed.

So here's my broadcast receiver:

  private BroadcastReceiver mBatInfoReceiver = new BroadcastReceiver()  {

        @Override
        public void onReceive(Context arg0, Intent intent) {

            //Battery level
            int level = intent.getIntExtra("level", 0);

            //Plugged in Status
            int plugged = intent.getIntExtra(BatteryManager.EXTRA_PLUGGED, -1);

            //Battery Status
            int status = intent.getIntExtra(BatteryManager.EXTRA_STATUS, -1);

            //If the device is charging or contains a full status, it's charging
            boolean isCharging = status == BatteryManager.BATTERY_STATUS_CHARGING ||
                    status == BatteryManager.BATTERY_STATUS_FULL;

            //If the device isCharging and plugged in, then show that the battery is charging
            TextView batteryTextView = ((TextView) findViewById(R.id.charging));
            TextView batteryLowTextView = ((TextView) findViewById(R.id.low_battery));
            ImageView batteryLowImageView= ((ImageView) findViewById(R.id.low_battery_icon));
            ImageView batteryImageView =((ImageView) findViewById(R.id.charging_battery_icon));

            if (isCharging && plugged == BatteryManager.BATTERY_PLUGGED_AC || plugged == BatteryManager.BATTERY_PLUGGED_USB) {


                //Gets the 'last synced' string and sets to datetime of the last sync
                Resources resources = context.getResources();
                String chargingString = String.format(resources.getString(R.string.charging), level);

                //Dynamically sets the value of the battery level
                batteryLowTextView.setVisibility(TextView.INVISIBLE);
                batteryLowImageView.setVisibility(ImageView.INVISIBLE);
                batteryTextView.setText(chargingString + "%");

            } else if (level < LOW_BATTERY_LEVEL && !isCharging) {

                Resources resources = context.getResources();
                String lowBatteryString = String.format(resources.getString(R.string.low_battery));

                batteryTextView.setVisibility(TextView.INVISIBLE);
                batteryImageView.setVisibility(ImageView.INVISIBLE);
                batteryLowTextView.setText(lowBatteryString);

            } else if (!isCharging && level > LOW_BATTERY_LEVEL) {

                //do nothing
                batteryTextView.setVisibility(TextView.GONE);
                batteryImageView.setVisibility(ImageView.GONE);

            }

        }

    };

In my OnCreate method, I call

 //calls registerReceiver to receive the broadcast for the state of battery
            this.registerReceiver(this.mBatInfoReceiver,new
                    IntentFilter(Intent.ACTION_BATTERY_CHANGED));

So, I am wondering what I am doing wrong? Is it the bools?


Solution

    1. Your if-then-else logic has a lot of holes (and has the issue mentioned by @Vikram). That can cause the receiver to end up doing nothing. Try simplifying the logic to remove all the holes.
    2. When you update a text view, you also have to remember to make it (and its image view) visible first.

    Here is a replacement for your if-then-else logic:

            if (isCharging) {
    
                //Gets the 'last synced' string and sets to datetime of the last sync
                Resources resources = context.getResources();
                String chargingString = String.format(resources.getString(R.string.charging), level);
    
                //Dynamically sets the value of the battery level
                batteryLowTextView.setVisibility(TextView.INVISIBLE);
                batteryLowImageView.setVisibility(ImageView.INVISIBLE);
                batteryTextView.setText(chargingString + "%");
                batteryTextView.setVisibility(TextView.VISIBLE);
                batteryImageView.setVisibility(ImageView.VISIBLE);
    
            } else if (level <= LOW_BATTERY_LEVEL) {
    
                Resources resources = context.getResources();
                String lowBatteryString = String.format(resources.getString(R.string.low_battery));
    
                batteryTextView.setVisibility(TextView.INVISIBLE);
                batteryImageView.setVisibility(ImageView.INVISIBLE);
                batteryLowTextView.setText(lowBatteryString);
                batteryLowTextView.setVisibility(TextView.VISIBLE);
                batteryLowImageView.setVisibility(ImageView.VISIBLE);
    
            } else {
    
                //show nothing
                batteryTextView.setVisibility(TextView.GONE);
                batteryImageView.setVisibility(ImageView.GONE);
                batteryLowTextView.setVisibility(TextView.GONE);
                batteryLowImageView.setVisibility(ImageView.GONE);
            }