java - 如何设置始终显示的警报?

标签 java android broadcastreceiver alerts batterymanager

现在,我只想在用户访问应用程序时显示某种电池状态指示。例如,如果设备已插入,则设备应显示正在充电和电量。如果设备已拔出,则不应显示任何内容。如果设备未充电且电池电量不足,则应显示电池电量不足指示。

我已经设置了大部分代码,但问题是,例如,如果我运行应用程序并且设备已插入,屏幕上会显示充电指示。如果我拔下设备,充电将不再可见,但如果我重新插入设备,充电仍将不再可见。当设备插入或拔出、电池电量高或低时,电池警报应始终显示。应始终显示信息的变化。

这是我的广播接收器:

  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);

            }

        }

    };

在我的 OnCreate 方法中,我调用

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

所以,我想知道我做错了什么?是 boolean 人吗?

最佳答案

  1. 您的if-then-else 逻辑有很多漏洞(并且有@Vikram 提到的问题)。这可能导致接收者最终什么都不做。尝试简化逻辑以消除所有漏洞。
  2. 更新 TextView 时,还必须记住首先使其(及其 ImageView )可见。

这是您的 if-then-else 逻辑的替代品:

        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);
        }

关于java - 如何设置始终显示的警报?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26100101/

相关文章:

广播接收器的 Android java->kotlin 转换失败

带有使用 gsl 的 C 函数的 Java JNI。

java - 如何在图像按钮下显示网页 View ?

java - 在 Android 中创建用于检查互联网连接的对话框

java - 更多线程还是空循环?

Android:在库中注册接收器

android - 使用 AlarmManager 从 BroadcastReceiver 启动服务

java - 使用 jQuery ajax 的 RESTful Web 服务

java - 为什么我不能制作这个我的世界模组?

android - gmail 中文本选择工具栏/选择标记使用的绿色颜色代码是什么?