我如何设置随时出现的警报?(How can I set up an alert that appears at all times?)

编程入门 行业动态 更新时间:2024-10-23 06:26:36
我如何设置随时出现的警报?(How can I set up an alert that appears at all times?)

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

我设置了大部分代码,但问题在于,例如,如果我运行应用程序并插入设备,屏幕上会显示充电指示。 如果我拔下设备,充电不再可见,但如果我重新插入设备,充电仍然不可见。 电池警报应始终显示设备插入或拔出,​​高或低电池。 应始终显示信息更改。

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

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

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

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?

最满意答案

你的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); } 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. 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); }

更多推荐

本文发布于:2023-07-08 20:05:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1080277.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:警报   如何设置   set   times   appears

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!