1

I am trying to create an application that could respond when the power button is pressed. To be more specific, which would respond to it when pressed 2 times and sends the SMS.

I tried using BroadcastReceiver also, but it did not work.

Below is the code I tried using to override the power button :

@Override
public boolean dispatchKeyEvent(KeyEvent event) {
    if (event.getKeyCode() == KeyEvent.KEYCODE_POWER) {
        Intent i = new Intent(this, MainActivity.class);
        startActivity(i);
        return true;
    }

    return super.dispatchKeyEvent(event);
}

I tried this also:

public boolean onKeyDown(int keyCode, KeyEvent event) 
{
        if (event.getKeyCode() == KeyEvent.KEYCODE_POWER) 
        {
            // do what you want with the power button
            SmsManager smsManager = SmsManager.getDefault();
            smsManager.sendTextMessage(phoneNo, null, sms, null, null);
           Toast.makeText(getApplicationContext(), "SMS Sent!",
            Toast.LENGTH_LONG).show();

            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

And with BroadcastReceiver I tried this also

public BroadcastReceiver mReceiver = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {
        // Check action just to be on the safe side.
        if ((intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) ||
                (intent.getAction().equals(Intent.ACTION_SCREEN_ON)) ) {

            long delta = 0;
            int count = 0;
            if((System.currentTimeMillis() - delta) < DOUBLE_CLICK_INTERVAL) 
            {
                count++;
                Log.e("AAAAAAAA", "count= "+count);
            }else{
                count = 0;
            }

            if(count == 4){
                Log.e("AAAAAAAA", "EVENT TRIGGERED!!!!!");
                Intent alarmIntent = new Intent("com.helpme.EVENT_MyReceiver");
                sendBroadcast(alarmIntent);
            }

            delta = System.currentTimeMillis();
        }
    }
};

But none of the above worked for me.

Yushi
  • 416
  • 6
  • 24
  • 1
    @SpringBreaker, when correcting formatting, please also correct glaring grammatical and spelling issues. – Charles Jan 30 '14 at 05:35
  • 1
    @Charles: Thanks Charles for such a valuable suggestion. – Spring Breaker Jan 30 '14 at 05:37
  • 2
    @SpringBreaker + Charles: When correcting formatting, grammatical and spelling issues (and other users, in SpringBreaker's case) please ensure you fix all of them, not just a few. Otherwise... um... what happens if you only correct a few of them? It's bad, right? –  Jan 30 '14 at 12:59
  • @Yushi again have you done with this? check this: http://stackoverflow.com/questions/22298018/double-press-power-button-to-send-sms – Sun Mar 10 '14 at 10:46
  • @Moon i know about that but dint get any satisfactory answer so have to post it again and still no body has given me any guidance for that problem – Yushi Mar 11 '14 at 05:53
  • @Yushi ok.. i am trying to resolve, once i will get correct answer will let you know for sure – Sun Mar 11 '14 at 07:41

1 Answers1

0

I think you should call your event when the system(Android) shut downs that way you'll be able to intercept button pressed like event.

I recomend you build a broadcast receiver to catches the ACTION_SHUTDOWN intent and after that in the onReceive() method you can do what ever you like.

Umar Iqbal
  • 669
  • 1
  • 11
  • 31