3

I have written an app in which the user can send messages whenever they press a button given in the activity, and it works fine for me, but now my target is to send SMS by using double press on power button.

but I don't have any idea, how to do that ?

   Send SMS by using double press on power button

below is the code, which I am using to send SMS:

 btnPanic.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            String message = "My current location is:" + "\t" + currentLocation ;

            String phoneNo = editContacts.getText().toString();

            StringTokenizer st=new StringTokenizer(phoneNo,",");
            while (st.hasMoreElements())
            {
                String tempMobileNumber = (String)st.nextElement();
                if(tempMobileNumber.length()>0 && message.trim().length()>0) {
                    sendSMS(tempMobileNumber, message);
            }
            else 
            {
              Toast.makeText(getBaseContext(), 
                "Please enter both phone number and message.", 
                 Toast.LENGTH_SHORT).show();
            }
        }
    }
});
Sun
  • 6,768
  • 25
  • 76
  • 131
  • duplicate http://stackoverflow.com/questions/21448564/twice-power-button-press-sends-sms – Alex van den Hoogen Mar 10 '14 at 10:46
  • 4
    @AlexvandenHoogen that's not the solution – Sun Mar 10 '14 at 10:47
  • 1
    i'm not a fan of using the power button for an event when writing a mobile app. You won't see that either at computers ... – KarelG Mar 10 '14 at 10:48
  • No, but it is a duplicate. However this should be a solution: http://stackoverflow.com/questions/3703071/how-to-hook-into-the-power-button-in-android - KarelG is however right. This is unexpected behaviour for an app and certainly not recommended. – Alex van den Hoogen Mar 10 '14 at 10:48
  • but moon requires @KarelG – Sun Mar 10 '14 at 10:49
  • 2
    you can't say this duplicate, because that question asked by other person and does not contain solution as well @AlexvandenHoogen i think you should understand this for you – Sun Mar 10 '14 at 10:51
  • i was researching and it seems possible. I [found this](http://stackoverflow.com/a/10365166/2412895), but you have to work it out further to detect a twice press. – KarelG Mar 10 '14 at 11:13
  • @KarelG already checked that but not helpful – Sun Mar 10 '14 at 11:25

3 Answers3

4

I can give you Idea about how can you implement this well it's hardcoded.

Pardon me if I went wrong.!!

This is kind of conversion of key event to double click.

So here I am considering key pressed twice as Double click

First see key event of power button.

1

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    if(KeyEvent.KEYCODE_POWER == event.getKeyCode()){
      if(time interval is less)//Consider as double click
       //code to send messages
        else            //Consider as single click
       //nothing to do   
    }
    return super.onKeyDown(keyCode, event);
}

2. You should use System.currentTimeMillis() to find time of two clicks in which Double click can be validated.

Try Doubleclick and record time and do it many times as you will get precise time.

So in short you have to first find how much maximum time allowed beetween two click which is considered as doubleClick.

So that we can differentiate beetween click and doubleclick.

3 So at key event you need to do this

long l=0;//at start not in key event handler

if(l=0){l=System.currentTimeMillis();}//clicked first time
else{l=System.currentTimeMillis()-l;}//second time

And you are done use this long values to decide is it click or double click.

akash
  • 22,664
  • 11
  • 59
  • 87
1
    public boolean onKeyDown(int keyCode, KeyEvent event) {
     if (event.getKeyCode() == KeyEvent.KEYCODE_POWER) {
    // do what you want with the power button

        String message = "My current location is:" + "\t" + currentLocation ;

        String phoneNo = editContacts.getText().toString();

        StringTokenizer st=new StringTokenizer(phoneNo,",");
        while (st.hasMoreElements())
        {
            String tempMobileNumber = (String)st.nextElement();
            if(tempMobileNumber.length()>0 && message.trim().length()>0) {
                sendSMS(tempMobileNumber, message);
        }
        else 
        {
          Toast.makeText(getBaseContext(), 
            "Please enter both phone number and message.", 
             Toast.LENGTH_SHORT).show();
        }
    return true;
}
return super.onKeyDown(keyCode, event);

}

Zied R.
  • 4,964
  • 2
  • 36
  • 67
1

This is probably what you are looking for http://www.nkonecny.com/blog/2012/02/16/capture-power-button-keypress/

same thing you can check twice since you want the power button to be pressed twice so I think it will serve your purpose.

upenpat
  • 685
  • 3
  • 12