简体   繁体   中英

BroadCast Receiver Battery Status error

i'm trying to use in my project a broadcast receiver which listens to battery status of charging/not charging and throw a toast in each of the options . every time i change the charger status in the app ,the app crash. (if i start the app with the charger connected it's show me the right toast but when i uncharge the phone the app crashes) here is the code thanks in advance

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
IntentFilter ifilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
Intent batteryStatus = this.registerReceiver(null, ifilter);
int status = batteryStatus.getIntExtra(BatteryManager.EXTRA_STATUS, -1);

boolean isCharging = status == BatteryManager.BATTERY_STATUS_CHARGING ||
                     status == BatteryManager.BATTERY_STATUS_FULL;

    if(isCharging==true){

        Toast.makeText(this, "Charging", Toast.LENGTH_SHORT).show();
    }
    else{
        Toast.makeText(this, "Charger not connected", Toast.LENGTH_SHORT).show();

UPDATE i'm having an hard time to understand what i suppose to do. i'm pretty new so be patient with me :)

here is the code i made

    public class MainActivity extends Activity  {

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);

            checkBatteryState(null);


        public void checkBatteryState(View sender) {
            IntentFilter filter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
            Intent batteryStatus = registerReceiver(null, filter);

            int chargeState = batteryStatus.getIntExtra(BatteryManager.EXTRA_STATUS, -1);
            String strState;

            switch (chargeState) {
                case BatteryManager.BATTERY_STATUS_CHARGING:
                case BatteryManager.BATTERY_STATUS_FULL:
                    strState = "charging";
                    Toast.makeText(this, strState, Toast.LENGTH_LONG).show();
                    break;
                default:
                    strState = "not charging";
                    Toast.makeText(this, strState, Toast.LENGTH_LONG).show();
            }
        }
    }

http://www.vogella.com/tutorials/AndroidBroadcastReceiver/article.html

is this what you're using?

Maybe you haven't ensured your Intent is sticky.

My advice would be not to use a registerreceiver with a null argument.

Try this method for creating a broadcastreceiver: How to send data to another app which is not started

put your Toasts in the onReceive() function.

The code is not actually registering a receiver, just getting a sticky broadcast. If the broadcast has never been sent the this will return null which will cause a NPE in the remaining code.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM