PialKanti

ScreenReceiver.java

Oct 9th, 2016
276
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.11 KB | None | 0 0
  1. public class ScreenReceiver extends BroadcastReceiver {
  2. long seconds_screenoff, seconds_screenon, OLD_TIME,actual_diff;
  3. boolean OFF_SCREEN, ON_SCREEN;
  4.  
  5. @Override
  6. public void onReceive(final Context context, final Intent intent) {
  7. Log.i("LOB", "Power button is pressed.");
  8. if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) {
  9.  
  10. seconds_screenoff = System.currentTimeMillis();
  11. OLD_TIME = seconds_screenoff;
  12. OFF_SCREEN = true;
  13.  
  14. new CountDownTimer(5000, 200) {
  15.  
  16. public void onTick(long millisUntilFinished) {
  17.  
  18.  
  19. if (ON_SCREEN) {
  20. if (seconds_screenon != 0 && seconds_screenoff != 0) {
  21.  
  22. actual_diff = cal_diff(seconds_screenon, seconds_screenoff);
  23. if (actual_diff <= 4000) {
  24.  
  25. Log.i("LOB", "POWER BUTTON CLICKED 2 TIMES");
  26. seconds_screenon = 0L;
  27. seconds_screenoff = 0L;
  28.  
  29. } else {
  30. seconds_screenon = 0L;
  31. seconds_screenoff = 0L;
  32.  
  33. }
  34. }
  35. }
  36. }
  37.  
  38. public void onFinish() {
  39.  
  40. seconds_screenoff = 0L;
  41. }
  42. }.start();
  43.  
  44.  
  45. } else if (intent.getAction().equals(Intent.ACTION_SCREEN_ON)) {
  46. seconds_screenon = System.currentTimeMillis();
  47. OLD_TIME = seconds_screenoff;
  48.  
  49. new CountDownTimer(5000, 200) {
  50.  
  51. public void onTick(long millisUntilFinished) {
  52. if (OFF_SCREEN) {
  53. if (seconds_screenon != 0 && seconds_screenoff != 0) {
  54. actual_diff = cal_diff(seconds_screenon, seconds_screenoff);
  55. if (actual_diff <= 4000) {
  56.  
  57. Log.i("LOB", "POWER BUTTON CLICKED 2 TIMES");
  58. seconds_screenon = 0L;
  59. seconds_screenoff = 0L;
  60.  
  61.  
  62. } else {
  63. seconds_screenon = 0L;
  64. seconds_screenoff = 0L;
  65.  
  66. }
  67. }
  68. }
  69.  
  70. }
  71.  
  72. public void onFinish() {
  73.  
  74. seconds_screenon = 0L;
  75. }
  76. }.start();
  77.  
  78.  
  79. }
  80. }
  81.  
  82. private long cal_diff(long seconds_screenon2, long seconds_screenoff2) {
  83. long diffrence;
  84. if (seconds_screenon2 >= seconds_screenoff2) {
  85. diffrence = (seconds_screenon2) - (seconds_screenoff2);
  86. seconds_screenon2 = 0;
  87. seconds_screenoff2 = 0;
  88. } else {
  89. diffrence = (seconds_screenoff2) - (seconds_screenon2);
  90. seconds_screenon2 = 0;
  91. seconds_screenoff2 = 0;
  92. }
  93.  
  94. return diffrence;
  95. }
  96.  
  97. }
Add Comment
Please, Sign In to add comment