1. Are you ready for the Galaxy S20? Here is everything we know so far!

Android Service With Wake Lock and Intent Redelivery Still Gets Killed When Screen Turns off

Discussion in 'Android Development' started by Alex Marasco, Oct 25, 2020.

  1. Alex Marasco

    Alex Marasco Lurker
    Thread Starter

    I created a service that is supposed to run in the background on a button press and stop on a button press. It's started from a fragment and not an Activity. I can get it to start and run while the screen is on, but the moment my screen turns off, its onDestroy method is called and stops itself. I haven't found a post that makes me realize what I am (most likely obviously) doing wrong.

    I began giving it a partial wake lock, yet that doesn't do anything.

    Manifest has permission
    Code (Text):
    1. <uses-permission android:name="android.permission.WAKE_LOCK" />
    Service gets started from fragment with boolean intent extra to specify if it should acquire wake lock

    Code (Java):
    1. Intent intent = new Intent(mainActivity.getApplicationContext(), NetworkService.class);
    2. intent.putExtra("Wake Lock", wakeLockBox.isEnabled());
    3. mainActivity.startService(intent);
    My Service creation acquires the wakelock with Partial tag and return redeliver intent.
    Code (Java):
    1. private PowerManager mPowerManager;
    2.     private PowerManager.WakeLock wakeLock;
    3.     private boolean wakeLockSet;
    4.  
    5.     // service doing its thing code here
    6.  
    7.     @Override
    8.     public int onStartCommand(Intent intent, int flags, int startId) {
    9.         Log.d("Service", "Started");
    10.         wakeLockSet = intent.getBooleanExtra("Wake Lock", false);
    11.         // ...
    12.         setWakeLock();
    13.         // ...
    14.         return START_REDELIVER_INTENT;
    15.     }
    16.  
    17.  
    18.    private void setWakeLock()
    19.    {
    20.         mPowerManager = (PowerManager) getSystemService(getApplicationContext().POWER_SERVICE);
    21.         wakeLock = mPowerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK,
    22.                 "NetworkService::WakelockTag");
    23.         wakeLock.acquire();
    24.    }
    Based on what I have seen in previous posts and from testing, the only way for the wakelock to be released is when the Service is destroyed.
    Code (Java):
    1.  
    2.     @Override
    3.     public void onDestroy()
    4.     {
    5.         super.onDestroy();
    6.         // ...
    7.         if (wakeLockSet) wakeLock.release();
    8.         Log.d("Service", "Shutdown");
    9.         stopSelf();
    10.     }
    11.  
    Whenever my screen turns off, the service onDestroy() method is called and the service releases the lock then stops itself. So I am confused about how the wakelock works with services that are supposed to run when the screen is off.

    I even log when the service is started to track if intent was getting redelivered, and it's not.

    I turned off my phones battery optimization and that didn't help. The only time I got it to run when the screen was off was when I just commented out onDestroy, which isn't gonna be a good solution.

    Also I tried making it a foreground service.

    in the fragment
    Code (Java):
    1.  
    2. // notification builder made earlier
    3.  
    4. Intent intent = new Intent(mainActivity.getApplicationContext(), NetworkService.class);
    5. intent.putExtra("Notification", builder.build());
    6. intent.putExtra("Wake Lock", wakeLockBox.isEnabled());
    7. mainActivity.startForegroundService(intent);
    8.  
    then the service creation
    Code (Java):
    1.  
    2.     private PowerManager mPowerManager;
    3.     private PowerManager.WakeLock wakeLock;
    4.     private boolean wakeLockSet;
    5.  
    6.     // service doing its thing code here
    7.  
    8.     @Override
    9.     public int onStartCommand(Intent intent, int flags, int startId) {
    10.         startForeground(400, intent.getExtras().getParcelable("Notification"));
    11.         Log.d("Service", "Started");
    12.         wakeLockSet = intent.getBooleanExtra("Wake Lock", false);
    13.         // ...
    14.         setWakeLock();
    15.         // ...
    16.         return START_REDELIVER_INTENT;
    17.     }
    18.  
    19.  
    20.    private void setWakeLock()
    21.    {
    22.         mPowerManager = (PowerManager) getSystemService(getApplicationContext().POWER_SERVICE);
    23.         wakeLock = mPowerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK,
    24.                 "NetworkService::WakelockTag");
    25.         wakeLock.acquire();
    26.    }
    27.  
    Same results though.


    Any ideas of what I am doing wrong? The project's min SDK is 27.
     


Loading...

Share This Page

Loading...