Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class ScreenReceiver extends BroadcastReceiver {
- long seconds_screenoff, seconds_screenon, OLD_TIME,actual_diff;
- boolean OFF_SCREEN, ON_SCREEN;
- @Override
- public void onReceive(final Context context, final Intent intent) {
- Log.i("LOB", "Power button is pressed.");
- if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) {
- seconds_screenoff = System.currentTimeMillis();
- OLD_TIME = seconds_screenoff;
- OFF_SCREEN = true;
- new CountDownTimer(5000, 200) {
- public void onTick(long millisUntilFinished) {
- if (ON_SCREEN) {
- if (seconds_screenon != 0 && seconds_screenoff != 0) {
- actual_diff = cal_diff(seconds_screenon, seconds_screenoff);
- if (actual_diff <= 4000) {
- Log.i("LOB", "POWER BUTTON CLICKED 2 TIMES");
- seconds_screenon = 0L;
- seconds_screenoff = 0L;
- } else {
- seconds_screenon = 0L;
- seconds_screenoff = 0L;
- }
- }
- }
- }
- public void onFinish() {
- seconds_screenoff = 0L;
- }
- }.start();
- } else if (intent.getAction().equals(Intent.ACTION_SCREEN_ON)) {
- seconds_screenon = System.currentTimeMillis();
- OLD_TIME = seconds_screenoff;
- new CountDownTimer(5000, 200) {
- public void onTick(long millisUntilFinished) {
- if (OFF_SCREEN) {
- if (seconds_screenon != 0 && seconds_screenoff != 0) {
- actual_diff = cal_diff(seconds_screenon, seconds_screenoff);
- if (actual_diff <= 4000) {
- Log.i("LOB", "POWER BUTTON CLICKED 2 TIMES");
- seconds_screenon = 0L;
- seconds_screenoff = 0L;
- } else {
- seconds_screenon = 0L;
- seconds_screenoff = 0L;
- }
- }
- }
- }
- public void onFinish() {
- seconds_screenon = 0L;
- }
- }.start();
- }
- }
- private long cal_diff(long seconds_screenon2, long seconds_screenoff2) {
- long diffrence;
- if (seconds_screenon2 >= seconds_screenoff2) {
- diffrence = (seconds_screenon2) - (seconds_screenoff2);
- seconds_screenon2 = 0;
- seconds_screenoff2 = 0;
- } else {
- diffrence = (seconds_screenoff2) - (seconds_screenon2);
- seconds_screenon2 = 0;
- seconds_screenoff2 = 0;
- }
- return diffrence;
- }
- }
Add Comment
Please, Sign In to add comment