• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

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

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:
<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

Java:
Intent intent = new Intent(mainActivity.getApplicationContext(), NetworkService.class);
intent.putExtra("Wake Lock", wakeLockBox.isEnabled());
mainActivity.startService(intent);

My Service creation acquires the wakelock with Partial tag and return redeliver intent.
Java:
private PowerManager mPowerManager;
    private PowerManager.WakeLock wakeLock;
    private boolean wakeLockSet;

    // service doing its thing code here

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Log.d("Service", "Started");
        wakeLockSet = intent.getBooleanExtra("Wake Lock", false);
        // ...
        setWakeLock();
        // ...
        return START_REDELIVER_INTENT;
    }


   private void setWakeLock()
   {
        mPowerManager = (PowerManager) getSystemService(getApplicationContext().POWER_SERVICE);
        wakeLock = mPowerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK,
                "NetworkService::WakelockTag");
        wakeLock.acquire();
   }

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.
Java:
    @Override
    public void onDestroy()
    {
        super.onDestroy();
        // ...
        if (wakeLockSet) wakeLock.release();
        Log.d("Service", "Shutdown");
        stopSelf();
    }

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
Java:
// notification builder made earlier

Intent intent = new Intent(mainActivity.getApplicationContext(), NetworkService.class);
intent.putExtra("Notification", builder.build());
intent.putExtra("Wake Lock", wakeLockBox.isEnabled());
mainActivity.startForegroundService(intent);

then the service creation
Java:
    private PowerManager mPowerManager;
    private PowerManager.WakeLock wakeLock;
    private boolean wakeLockSet;

    // service doing its thing code here

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        startForeground(400, intent.getExtras().getParcelable("Notification"));
        Log.d("Service", "Started");
        wakeLockSet = intent.getBooleanExtra("Wake Lock", false);
        // ...
        setWakeLock();
        // ...
        return START_REDELIVER_INTENT;
    }


   private void setWakeLock()
   {
        mPowerManager = (PowerManager) getSystemService(getApplicationContext().POWER_SERVICE);
        wakeLock = mPowerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK,
                "NetworkService::WakelockTag");
        wakeLock.acquire();
   }

Same results though.


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