2

I want to listen the power key event in the service.

How can in do that ?

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.

Somehow I can manage to get it.

But when I kill/stop the app, the service is getting stopped.

How can i overcome this ?

Currently the code i am using this :

Service Class:

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);
    }
}

BroadcastReceiver Class:

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);
        }
    }
}

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.

Thanks in Advance.

AADProgramming
  • 6,077
  • 11
  • 38
  • 58

1 Answers1

0
@Override
public void onDestroy() {

    super.onDestroy();
    startService(new Intent(this, SampleService.class));

}

This is one way to ensure that service will never stop even user want to destroy it. This is one Just ONE of ways to achieve what you are trying to achieve.

Secondly, you can try and run service in "foreground" by using startForeground().

Also, make sure that in you return "START_STICKY" (which you are doing in the sample code that you shared and I trust that you are also doing in App's code too :) ) in Services's onStartCommand().

This will ensure that If this service's process is killed while it is started (after returning from onStartCommand(Intent, int, int)), then leave it in the started state but don't retain this delivered intent. Later the system will try to re-create the service.

And you may find some additional pointers/hints to make sure your service is not stopped at below link.

How can we prevent a Service from being killed by OS?

Just pick and choose the approach that best suits YOUR Need/implementation.

Community
  • 1
  • 1
AADProgramming
  • 6,077
  • 11
  • 38
  • 58