Raising a toast when Power Button is pressed twice

北战南征 提交于 2019-12-11 02:03:34

问题


What i am trying to do is ,double clicking the power button will raise a toast "Sending Message", doesn't matter if screen is On or Off.What i have done is , i have recorded the time duration of the clicks on power button & If the difference b/w the current click and previous click duration is less than a 1sec, then it will raise the toast. But its raising the toast on the single click also. Please Help me out

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);
}
}

2.CloseSystemDialogsIntentReceiver.java

public class CloseSystemDialogsIntentReceiver extends 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 - prevTime) < 1000 && (currTime - prevTime)>-1000 ) {
        if ((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;
        }
    }
   }
   }

回答1:


You should unregister the broadcast receiver in onDestroy and secondly this kind of system event should be handled in a Service not a foreground activity that can be destroyed when the screen goes off. Your activity can lose all state and get recreated between these presses.




回答2:


Edit your code like this:

public class CloseSystemDialogsIntentReceiver extends 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
    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");
    wasScreenOn = true;
}

if (prevTime == 0) {
// power button first time pressed or after you double-pressed
    prevTime = System.currentTimeMillis();
} else if (((currTime = System.currentTimeMillis()) - prevTime) < 1000 ) {
// second press under 1s(double-pressed), reset prevTime
    Toast.makeText(context, "double Clicked power button",
        Toast.LENGTH_LONG).show();
    Log.e("eciver ", "double Clicked power button");
    prevTime = 0;
} else {
// second press over 1s, considered as first press for next checking
    prevTime = currTime;
}
}
}


来源:https://stackoverflow.com/questions/21277324/raising-a-toast-when-power-button-is-pressed-twice

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!