Reputation: 157
I am trying to develop an Android service that runs when a button is pressed and stops when a button is pressed. I am trying to make it run even when the screen turns off whether by screen timeout or power button press. Pretty common concept I bet, but I haven't found a post that makes me realize what I am (most likely obviously) doing wrong.
Manifest has permission
<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
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.
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_STICKY;
}
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.
@Override
public void 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 stops itself. So I am confused about how the wakelock works with services that are supposed to run when the screen is off.
I have tried foreground services:
in the fragment
// 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
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_STICKY;
}
private void setWakeLock()
{
mPowerManager = (PowerManager) getSystemService(getApplicationContext().POWER_SERVICE);
wakeLock = mPowerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK,
"NetworkService::WakelockTag");
wakeLock.acquire();
}
Same results though. Open to any ideas and willing to share more info if needed.
Upvotes: 2
Views: 476