title: How to detect device power button press twice in android programmatically
body: <p>I am making an android application which needs to detect the event of device power button press twice/thrice &amp; send an SMS in the background. The listener should run in the background (i.e even if my app is not open, it should detect the keypress event and act accordingly).</p>
  
  <p>Below is my tried code which is not working...</p>
  
  <p>My Code:</p>
  
  <pre><code>public class MyBroadCastReciever extends BroadcastReceiver {
  
       int Count=0;
  
       @Override
          public void onReceive(Context context, Intent intent) {
              if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) {
                 Count++;
                 if(Count==2){
                    //Send SMS code..
                   }
  
  
              } else if (intent.getAction().equals(Intent.ACTION_SCREEN_ON)) {
                   //This is for screen ON option.
              }
          }
  </code></pre>
  
  <p>Manifest file:</p>
  
  <pre><code>  &lt;application
         android:icon="@drawable/ic_launcher"
         android:label="@string/app_name"
         android:theme="@style/AppTheme" &gt;
  
  
      &lt;receiver android:name=".MyBroadCastReciever" &gt;
          &lt;intent-filter&gt;
              &lt;action android:name="android.intent.action.SCREEN_OFF" /&gt;
              &lt;action android:name="android.intent.action.SCREEN_ON" /&gt;
          &lt;/intent-filter&gt;
      &lt;/receiver&gt;
  &lt;/application&gt;
  </code></pre>