7

Here i mentioned the code for waking up the screen. i want the code is listen still the app is closed and the cpu is cleared and user can click the power button when ever my screen is unlock the app is sync like whatsapp.

    PowerManager pm =  (PowerManager)getSystemService(Context.POWER_SERVICE); 
    wl = pm.newWakeLock(PowerManager.SCREEN_DIM_WAKE_LOCK, "whatever");
    super.onCreate(savedInstanceState);
    wl.acquire();
KOUSIK daniel
  • 305
  • 1
  • 3
  • 11

2 Answers2

7

FULL_WAKE_LOCK is already deprecated and it's better to use the PARTIAL_WAKE_LOCK. This is the standard way to do this,

PowerManager powerManager = (PowerManager) getSystemService(POWER_SERVICE);
Wakelock wakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK,
        "MyWakelockTag");
wakeLock.acquire();

For more way of implementation kindly visit the official link,

https://developer.android.com/training/scheduling/wakelock.html

Prokash Sarkar
  • 11,723
  • 1
  • 37
  • 50
0

Well you can consider an alternate, using JobScheduler for running background tasks while the app is still sleep for longer time thus preserving the battery. JobScheduler provides many methods to define job-execution conditions, therefore we can leverage that but it can be used for apps which are using Android 5.0 (API level 21) & above.

Ajay Deepak
  • 471
  • 5
  • 9
  • 2
    We are coding code, not literature. Can you please post code? – AlexS Dec 19 '18 at 06:40
  • Try this to safely implement a bg task without losing device's battery life and performance https://android.jlelse.eu/modern-background-execution-in-android-bfae13c61b6b – Ajay Deepak Dec 21 '18 at 12:22
  • thank you for comment. It's so hard without any examples. I spent 2 days to explain JobScheduler and it not take effect on Oreo+ api.. – AlexS Dec 21 '18 at 12:26