7

i want to create such application in android which will be activated when i press sleep or power button twice , is it possible to do that , by running an application in background and listening events from power button ?

some times phone gets into sleep mode once it is idle , and to use any application user has to press sleep button and then he has to enter certain password to activate the phone. But i want to make make it activate my application when a power button is clicked without any other intervention

Hunt
  • 8,215
  • 28
  • 116
  • 256
  • you have to write a service to do that, I didn't try it but, here is a smiliar question.http://stackoverflow.com/questions/5907102/home-button-listener – Okan Kocyigit Jan 20 '12 at 11:59

2 Answers2

13

You can try this trick .

Register a Broadcast Receiver which is initiated when powerbutton is clicked. Now in OnReceive method of the Receiver do what you want.

For example:

in manifest file register a receiver:

 <receiver android:name="com.test.check.MyReceiver">
        <intent-filter>
            <action android:name="android.intent.action.SCREEN_OFF"></action>
            <action android:name="android.intent.action.SCREEN_ON"></action>
            <action android:name="android.intent.action.ACTION_POWER_CONNECTED"></action>
            <action android:name="android.intent.action.ACTION_POWER_DISCONNECTED"></action>
             <action android:name="android.intent.action.ACTION_SHUTDOWN"></action>
        </intent-filter>
    </receiver>

&& in onReceive() method of the Receiver

 public class MyReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context arg0, Intent arg1) {
        // TODO Auto-generated method stub

         Log.v("#@%@%#", "Power button is pressed.");  

         Toast.makeText(arg0, "power button clicked",Toast.LENGTH_LONG).show();

        //perform what you want here
    }
}

Now perform any operation in onReceive() method of the Receiver.

Dinesh Sharma
  • 11,533
  • 7
  • 42
  • 60
  • 1
    Hey the above tricked worked .... I did this code and its working and shows the toast when power button is connected or disconnected .. I modified the code a bit , so I am editing the above post a bit.. hope it can help you ... – Dinesh Sharma Jan 20 '12 at 12:43
  • Can you please tell what changes you have made in the above code. and please paste a full code of menifiest file. thanks – Qadir Hussain Mar 08 '13 at 10:00
  • from this 5 actions the only one working while telefone is locked is the SCREEN_ON-Event. – bofredo Aug 21 '13 at 11:28
  • @DineshSharma It is not working for me. I have created a broadcast reciever extended class and added above code in Manifest file. What am I missing? – Umer Farooq Aug 21 '13 at 16:51
  • 1
    @Dinesh Sharma This Broadcast Receiver will work for power connected or DISCONNECTED (power cable)not for power button press event. – Dipak Sonnar Oct 09 '14 at 16:17
0

this is an key down event of power you can get some idea from this just try on this you'll get some idea... i didnt try such an act but i found this in power manager.

public boolean onKeyDown(int keyCode, KeyEvent event) {
    if (event.getKeyCode() == KeyEvent.KEYCODE_POWER) {
        // do what you want with the power button
        return true;
    }
    return super.onKeyDown(keyCode, event);
}
Didier Ghys
  • 30,396
  • 9
  • 75
  • 81
SilentKiller
  • 6,944
  • 6
  • 40
  • 75