ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

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

2019-07-28 11:01:42  阅读:266  来源: 互联网

标签:android batterymanager java broadcastreceiver alerts


现在,我只想在用户访问应用程序时获得某种电池状态指示.例如,如果设备已插入,则设备应该说充电和它的电平.如果设备已拔下,则不应显示任何内容.如果设备未充电且电池电量不足,则应显示电池电量不足指示.

我设置了大部分代码,但问题是,例如,如果我运行应用程序并且设备已插入,屏幕上会显示充电指示.如果我拔下设备,则不再显示充电但如果我重新插入设备,则仍然无法再显示充电.当设备插入或拔出,​​电池电量不足或电量不足时,应始终显示电池警报.应始终显示信息更改.

所以这是我的广播接收器:

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

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

解决方法:

>你的if-then-else逻辑有很多漏洞(并且有@Vikram提到的问题).这可能导致接收器最终无所事事.尝试简化逻辑以删除所有漏洞.
>更新文本视图时,还必须记住首先使其(及其图像视图)可见.

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

标签:android,batterymanager,java,broadcastreceiver,alerts
来源: https://codeday.me/bug/20190728/1561131.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有