Android 使用电源按钮处理屏幕打开和关闭
Android Screen On And Off Handling Using Power Button
我正在尝试检测是否在 4 秒内按了 3 次电源按钮。以下代码无效。
public class PowerButtonReceiver extends BroadcastReceiver{
static int count = 0;
long initialTime,finishTime;
@Override
public void onReceive(final Context context, Intent intent) {
Log.v("onReceive", "Power button is pressed.");
if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) {
if (count == 0){
initialTime = System.currentTimeMillis();
}
count++;
if (count == 3){
finishTime = System.currentTimeMillis();
if (finishTime - initialTime <= 4000){
Toast.makeText(context, "POWER BUTTON CLICKED 3 TIMES", Toast.LENGTH_LONG).show();
count = 0;
}
}
} else if (intent.getAction().equals(Intent.ACTION_SCREEN_ON)) {
if (count == 0){
initialTime = System.currentTimeMillis();
}
count++;
if (count == 3){
finishTime = System.currentTimeMillis();
if (finishTime - initialTime <= 4000){
Toast.makeText(context, "POWER BUTTON CLICKED 3 TIMES", Toast.LENGTH_LONG).show();
count = 0;
}
}
}
}
}
代码执行无误,不显示 toast
- 希望您正在动态注册接收器。
ACTION_SCREEN_OFF --
You cannot receive this through components declared in manifests,...
以下情况除外:
if (finishTime - initialTime <= 4000){
Toast.makeText(context, "POWER BUTTON CLICKED 3 TIMES",
Toast.LENGTH_LONG).show();
count = 0;
}
没有其他方法将计数重新分配为 0,因此,如果您错过了一次敬酒..在单个 运行 和 <=4000
的以下事件中,计数器不会重置为 0条件不会进入 if
,如果它大于 3 (?)
,也许你应该将计数重新分配为 0
- 如果您的 toast 在屏幕关闭时点击期间出现会怎样——不要认为它会可见
应该考虑使用 ||
替换 if-else,如果你只想检测电源按钮事件而不考虑屏幕关闭或打开,效果会更好;喜欢:
if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)
|| intent.getAction().equals(Intent.ACTION_SCREEN_ON)) { ...
如果 1. 没有问题,目前你的代码看起来没问题,它是 2. 和 3. 的组合。
4.只是一个建议
我正在尝试检测是否在 4 秒内按了 3 次电源按钮。以下代码无效。
public class PowerButtonReceiver extends BroadcastReceiver{
static int count = 0;
long initialTime,finishTime;
@Override
public void onReceive(final Context context, Intent intent) {
Log.v("onReceive", "Power button is pressed.");
if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) {
if (count == 0){
initialTime = System.currentTimeMillis();
}
count++;
if (count == 3){
finishTime = System.currentTimeMillis();
if (finishTime - initialTime <= 4000){
Toast.makeText(context, "POWER BUTTON CLICKED 3 TIMES", Toast.LENGTH_LONG).show();
count = 0;
}
}
} else if (intent.getAction().equals(Intent.ACTION_SCREEN_ON)) {
if (count == 0){
initialTime = System.currentTimeMillis();
}
count++;
if (count == 3){
finishTime = System.currentTimeMillis();
if (finishTime - initialTime <= 4000){
Toast.makeText(context, "POWER BUTTON CLICKED 3 TIMES", Toast.LENGTH_LONG).show();
count = 0;
}
}
}
}
}
代码执行无误,不显示 toast
- 希望您正在动态注册接收器。
ACTION_SCREEN_OFF --You cannot receive this through components declared in manifests,...
以下情况除外:
if (finishTime - initialTime <= 4000){ Toast.makeText(context, "POWER BUTTON CLICKED 3 TIMES", Toast.LENGTH_LONG).show(); count = 0; }
没有其他方法将计数重新分配为 0,因此,如果您错过了一次敬酒..在单个 运行 和 <=4000
的以下事件中,计数器不会重置为 0条件不会进入 if
,如果它大于 3 (?)
- 如果您的 toast 在屏幕关闭时点击期间出现会怎样——不要认为它会可见
应该考虑使用
||
替换 if-else,如果你只想检测电源按钮事件而不考虑屏幕关闭或打开,效果会更好;喜欢:if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF) || intent.getAction().equals(Intent.ACTION_SCREEN_ON)) { ...
如果 1. 没有问题,目前你的代码看起来没问题,它是 2. 和 3. 的组合。 4.只是一个建议