title: EventListener for Power Button press in a Service
body: <p>I want to listen the power key event in the service.</p>
  
  <p>How can in do that ?</p>
  
  <p>Currently I am working with an app,  where I need to listen the power button for some events, from a service which is running in a background, even when the app is killed or stopped.</p>
  
  <p>Somehow I can manage to get it. </p>
  
  <p>But when I kill/stop the app, the service is getting stopped.</p>
  
  <p>How can i overcome this ?</p>
  
  <p>Currently the code i am using this :</p>
  
  <p>Service Class:</p>
  
  <pre><code>public class SampleService extends Service
  {
  
      SettingContentObserver mSettingsContentObserver;
      AudioManager mAudioManager;
      private ComponentName mRemoteControlResponder;
      private Intent intent;
  
       @Override
          public int onStartCommand(Intent intent, int flags, int startId) 
          {        
              Log.v("StartServiceAtBoot", "StartAtBootService -- onStartCommand()");
              // We want this service to continue running until it is explicitly
              // stopped, so return sticky.
              return START_STICKY;            
          }
  
       @Override
       public void onStart(Intent intent, int startId) {
           boolean screenOn = intent.getBooleanExtra("screen_state", false);
           if (!screenOn) {
               Toast.makeText(getApplicationContext(), "On", Toast.LENGTH_SHORT).show();
           } else {
               Toast.makeText(getApplicationContext(), "Off", Toast.LENGTH_SHORT).show();
           }
       }
  
          public void onCreate()
          {
              mSettingsContentObserver = new SettingContentObserver(this,new Handler());
              getApplicationContext().getContentResolver().registerContentObserver
                  (android.provider.Settings.System.CONTENT_URI, true, mSettingsContentObserver );
              mAudioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
              mRemoteControlResponder = new ComponentName(getPackageName(),
                      StartAtBootServiceReceiver.class.getName());
  
              IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_ON);
              filter.addAction(Intent.ACTION_SCREEN_OFF);
              BroadcastReceiver mReceiver = new StartAtBootServiceReceiver();
              registerReceiver(mReceiver, filter);
          }
  
      @Override
      public IBinder onBind(Intent intent) {
          // TODO Auto-generated method stub
          return null;
      }
  
      public void onDestroy()
      {       
          getApplicationContext().getContentResolver().unregisterContentObserver(mSettingsContentObserver);
      }
  }
  </code></pre>
  
  <p>BroadcastReceiver Class:</p>
  
  <pre><code>public class StartAtBootServiceReceiver extends BroadcastReceiver
  {
      static boolean wasScreenOn;
      private boolean screenOff;
  
      public void onReceive(Context context, Intent intent) 
      {
          if(intent.getAction().equals(Intent.ACTION_SCREEN_OFF))
          {
              wasScreenOn = false;
              Toast.makeText(context, "Power Off", Toast.LENGTH_SHORT).show();
          }
          else if(intent.getAction().equals(Intent.ACTION_SCREEN_ON))
          {
              wasScreenOn = true;
          }
          Intent i = new Intent(context, SampleService.class);
          i.putExtra("screen_state", screenOff);
          i.setAction("com.example.antitheft.SampleService");
          context.startService(i);
  //      
          if (intent.getAction().equals(Intent.ACTION_BOOT_COMPLETED)) {
              Intent i1 = new Intent();
              i1.setAction("com.example.sampleonkeylistener.MainActivity");
              context.startService(i1);
          }
      }
  }
  </code></pre>
  
  <p>given above is the sample code and i have created AndroidManifest.xml files also with user's permission but i cannot get the app continue service if it is killed or stopped.</p>
  
  <p>Thanks in Advance.</p>