- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
现在,我只想在用户访问应用程序时显示某种电池状态指示。例如,如果设备已插入,则设备应显示正在充电和电量。如果设备已拔出,则不应显示任何内容。如果设备未充电且电池电量不足,则应显示电池电量不足指示。
我已经设置了大部分代码,但问题是,例如,如果我运行应用程序并且设备已插入,屏幕上会显示充电指示。如果我拔下设备,充电将不再可见,但如果我重新插入设备,充电仍将不再可见。当设备插入或拔出、电池电量高或低时,电池警报应始终显示。应始终显示信息的变化。
这是我的广播接收器:
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 人吗?
最佳答案
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);
}
关于java - 如何设置始终显示的警报?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26100101/
我正在尝试将字符串转换为 float 。我知道 parseFloat() 可以做到这一点,但我也找到了下面的语法,但没有太多引用。 什么是正确的语法,因为它们似乎都有效。我可以在哪里了解更多相关信息?
我见过一些看起来很酷的“窗口”/“警报”/不管它们叫什么。我希望我知道。以下是其中的一些示例: 这些不应该是 Apple 独有的,因为我已经看到 3rd 方应用程序使用它们!我想知道这些 window
这个问题已经有答案了: What is the difference between a function call and function reference? (6 个回答) 已关闭 7 年前。
alert('test1'); var re = new RegExp("(http://(?:[A-Za-z0-9-]+\\.)?[A-Za-z0-9-]+\\.[A-Za-z0-9-]+/?)",
我有一个 Rails 应用程序,它与其他 Rails 应用程序通信以进行数据插入。我使用 jQuery $.post 方法进行数据插入。对于插入,我的其他 Rails 应用程序显示 200 OK。但在
我的作业有问题...我不知道我的代码有什么问题..我的作业是创建一个简单的学习数学和级别选择......我使用下拉菜单来选择级别和算术运算......现在我的问题是,当我单击按钮时,它将转到函数sta
我有一些复选框,其值属性来自数据库。我希望用户始终检查具有相同值的复选框(如果他不使用 javascript 发出警报)。我尝试使用以下 javascript 代码执行此操作,但没有用 fu
这有点难以解释,我的网站上有一个幻灯片形式的多部分表单。他们必须使用单选按钮从 3/4 选项中进行选择。 我对它们进行了一些验证,以确保用户在允许转到下一张幻灯片之前选择一个。 如果我单击一个对象来选
关闭。这个问题需要多问focused 。目前不接受答案。 想要改进此问题吗?更新问题,使其仅关注一个问题 editing this post . 已关闭 5 年前。 Improve this ques
我的页面上有一个click链接,我想在单击时播放通知或提示音。我如何使用jQuery做到这一点? 在此先感谢... :) 炸药 最佳答案 使用 jQuery sound 插件。 关于javascrip
我正在尝试在 Excel 列中创建 VBA -Alert 弹出窗口。在基于某些计算的 Excel 工作表中,将计算一些增长%(H 列),如果增长%> 20%,则会生成一个警报弹出窗口,询问原因代码,需
当用户滚动到网页的特定部分时,如何使用 JavaScript 显示警报。我尝试通过检查 document.body.clientWidth = document.documentElement.cli
我正在尝试制作一个脚本,其中会弹出一个提示窗口询问问题,并根据其中的答案,会弹出一个警告框,指出答案有效或无效。在我的代码中,我的提示框有效,但我的警报框没有。有人可以帮我解决这个问题吗?非常感谢!!
我正在尝试 Grafana 的警报和通知功能,它看起来真的很棒。 松弛通知示例。 但是有一个大问题。它需要使用 S3 进行配置,这使得任何人都可以公开访问图像。对于那些不希望公开访问其图像的公司来说,
我想知道是否有任何方法可以在 adobe reader 中通知用户pdf 表单已提交到服务器?我正在提交一个正常的 http/html 形式到 php 脚本没什么大不了的,直接,但文档、论坛等似乎存在
在 TFS 中构建失败后,是否可以通过电子邮件获取构建成功的信息? 当构建失败时(我确实如此),我可以收到电子邮件。当构建成功时,我可以收到电子邮件。 但我需要知道构建不再失败。如果我收到构建失败的电
我需要一些帮助来理解 jQuery 如何存储元素。请看一下这个链接: http://jsfiddle.net/NubWC/ 我试图从所有具有特定类的标题标签中获取元素 id,并将其放入数组中,以便我可
我想做 alert(this) 来进行演示(想看看代码中不同位置的“this”是什么)。 有什么想法可以实现这一目标吗? 现在它只返回[object Object]? 最佳答案 这样做: consol
当出现警告框时,有什么方法可以阻止 Enter 键盘吗?因此用户需要按 Esc 键或单击 Ok 按钮来删除警报。 alert('Hello'); 最佳答案 正如我之前的评论所述,标准的 javas
我正在尝试在 javascript 中创建一个函数并传入参数“name”,然后当用户点击一张照片时,会出现一条警告,类似于“这张照片是在 ____ 拍摄的” function photoWhere
我是一名优秀的程序员,十分优秀!