-1

I am trying to freeze background running processes when mobile screen is off and when screen get on processes should restart automatically in lower android versions. But i am not able to find any code to achieve this functionality.So if anyone knows about this then please help.

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
p.k.
  • 21
  • 2

2 Answers2

1

Try this :)

IntentFilter intentFilter = new IntentFilter(Intent.ACTION_SCREEN_ON);
intentFilter.addAction(Intent.ACTION_SCREEN_OFF);
registerReceiver(new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {
        if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) {
            //Stop your service
        } else if (intent.getAction().equals(Intent.ACTION_SCREEN_ON)) {
            // Start your service
        }
    }
}, intentFilter);
Juan Sancho
  • 321
  • 1
  • 7
0

You can stop your background service in onStop() method of activity life cycle by using stopService(intent); and again start in onResume() method of activity life cycle by using startService(intent);

Sanjay Bhalani
  • 2,424
  • 18
  • 44