Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Android SDK doesn't intend for you to be able to do this while your app isn't running in the foreground. Maybe it is possible with the NDK or if you know how to touch the operating system with root access (but this would require the user to root their device).</p> <p>I researched this quite a bit a while back and this was the best I could come up with, it relies on the fact that the screen turns off and on when the power button is pressed (I haven't seen any devices where this isn't the case). This event IS something you can receive when your app is not running.</p> <hr> <p>Register a BroadcastReceiver or Service to receive intents with the following IntentFilter:</p> <pre><code>IntentFilter f = new IntentFilter(); f.addAction(Intent.ACTION_SCREEN_ON); f.addAction(Intent.ACTION_SCREEN_OFF); </code></pre> <p>When the Intent is received use</p> <pre><code>System.currentTimeMillis() </code></pre> <p>to get the timestamp that this intent was received.</p> <p>To make sure that the user really intend to trigger the event you have implemented, you need to chain multiple presses of the power button. Save the last time that the event was received with SharedPreferences. </p> <p>Upon subsequent power button presses check that the current press did not occur too long after the previous one. If it did, restart the number of presses that have occurred (with SharedPreferences again).</p> <p>If the current power button press received is within this maximum time frame, and the number of times that have been pressed reaches a certain amount (I use 4 as the default, 3 seems to cause unintentional triggers), then trigger your event.</p> <hr> <p>Here is the code I have for the receiver: <a href="https://github.com/eskimoapps/count_stuff/blob/master/Receiver.java" rel="nofollow">https://github.com/eskimoapps/count_stuff/blob/master/Receiver.java</a></p> <p>It's not very good and is from the first app I published a long time ago. If I ever get around to it I will rewrite it and put the whole thing on GitHub.</p> <p>If you want to see it in action here is the store listing:</p> <p><a href="https://play.google.com/store/apps/details?id=count.stuff&amp;hl=en" rel="nofollow">https://play.google.com/store/apps/details?id=count.stuff&amp;hl=en</a></p> <p>I would have listed someone else's app to avoid self-advertising, but I don't know any other apps that do this.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload