1

I have Created a methods to send SMS and call under function callfunction(); I want to call that function when the user have pressed four time power button, It should not required that app should be in launched state.

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115

2 Answers2

2

Try using the Power Manager

PARTIAL_WAKE_LOCK

added in API level 1 int PARTIAL_WAKE_LOCK Wake lock level: Ensures that the CPU is running; the screen and keyboard backlight will be allowed to go off.

If the user presses the power button, then the screen will be turned off but the CPU will be kept on until all partial wake locks have been released.

Constant Value: 1 (0x00000001)

Example:

//Initialize the Power Manager
PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);

 //Create a PARTIAL_WAKE_LOCK
 //This will keep the cpu running in the Background, so that the function will be called on the desired Time
 PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "My Tag");

//Check if the WackLock is held (may throw erro if you try to acquire twice)
//TRUE --> Do nothing, all good
//FALSE --> Acquire the WakeLock
 if(!wl.isHeld()){
    wl.acquire();
 }

//*****
//You code to handel the Powerbutton comes here
//*****

//If the Repeating task is not active, release the Lock

//Check if the WackLock is held (may throw error if you try to release a none acquired Lock)
//TRUE --> Release Lock
//FALSE --> Do nothing, all good
 if(wl.isHeld()){
    wl.release();
 }

To handle the Power Button, check this Post: How to hook into the Power button in Android?

This is just an assumption, if it still doesnt work, could you post some Logs or more code snippets of your project :)

0

I think this is not possible to keep track of power button press for 4 times and to start your expected functionality. Since i don't find any app on play store which has this kind of functionality so as to start there respected jobs. But you will get trigger for power button press for 1 time.

Nikhil Lotke
  • 605
  • 7
  • 15