我想做的是,双击电源按钮会举杯“发送消息”,不管屏幕是开还是关。不知何故设法编写了这段代码我所做的是,我已经记录了时间点击电源按钮的持续时间&如果它们的黑白差异小于 1 秒,那么它将举起祝酒词。但它只在屏幕关闭或锁定时才举杯,而不是在屏幕打开时......怎么办。请帮忙
MainActivity.java
public class MainActivity extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_ON); filter.addAction(Intent.ACTION_SCREEN_OFF); BroadcastReceiver mReceiver = new CloseSystemDialogsIntentReceiver(); registerReceiver(mReceiver, filter); } @Override protected void onPause() { // when the screen is about to turn off if (CloseSystemDialogsIntentReceiver.wasScreenOn) { // this is the case when onPause() is called by the system due to a screen state change System.out.println("SCREEN TURNED OFF"); } else { // this is when onPause() is called when the screen state has not changed } super.onPause(); } @Override protected void onResume() { // only when screen turns on if (!CloseSystemDialogsIntentReceiver.wasScreenOn) { // this is when onResume() is called due to a screen state change System.out.println("SCREEN TURNED ON"); } else { // this is when onResume() is called when the screen state has not changed } super.onResume(); } @Override public boolean onKeyDown(int keyCode, KeyEvent event) { if (keyCode == KeyEvent.KEYCODE_POWER) { // Do something here... Log.d("ONKEYDOWN", "ONKEYDOWN"); event.startTracking(); // Needed to track long presses return true; } return super.onKeyDown(keyCode, event); } @Override public boolean onKeyLongPress(int keyCode, KeyEvent event) { if (keyCode == KeyEvent.KEYCODE_POWER) { // Do something here... Log.d("onKeyLongPress", "ONKEYDOWN"); return true; } return super.onKeyLongPress(keyCode, event); } @Override public boolean dispatchKeyEvent(KeyEvent event) { if (event.getKeyCode() == KeyEvent.KEYCODE_POWER) { Log.d("dispatchKeyEvent", "ONKEYDOWN"); return true; } return super.dispatchKeyEvent(event); } }
CloseSystemDialogsIntentReceiver.java
公共类 CloseSystemDialogsIntentReceiver 扩展 BroadcastReceiver {
public static boolean wasScreenOn = true;
static long prevTime=0;
static long currTime=0;
@Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) {
// do whatever you need to do here
prevTime = System.currentTimeMillis();
Log.d("CHECK IN RECIVER WHEN ON","CHECK IN RECIVER WHEN ON");
wasScreenOn = false;
} else if (intent.getAction().equals(Intent.ACTION_SCREEN_ON)) {
// and do whatever you need to do here
Log.d("CHECK IN RECIVER WHEN ON","CHECK IN RECIVER WHEN OFF");
currTime = System.currentTimeMillis();
wasScreenOn = true;
}
if (currTime>0 && prevTime>0) {
if ((currTime - prevTime) < 1000 && (currTime - prevTime)>-1000) {
Toast.makeText(context, "double Clicked power button",
Toast.LENGTH_LONG).show();
Log.e("eciver ", "double Clicked power button");
currTime = 0;
prevTime = 0;
}
}
}
}
Android Manifest File
uses-permission android:name="android.permission.PREVENT_POWER_KEY" />