Reputation: 26
I am writing an android app in java which contains a stop watch counter to record time spent on an activity. After the activity is finished, the user is supposed to stop the counter so that the counter reading can be recorded.
I have deployed a partial wake lock acquire on onCreate and release on onDestroy. I have also entered the required permission in the manifest file.
The wake lock gets acquired successfully, but gets released when the app is running in background, that is, it's no longer active onResume.
When the app is running in background and screen is turned off, all the recent apps, including this one get wiped off the recent items screen after some time. I am testing it on a Xiaomi Redmi Note 7 pro with MIUI 12.5.1.
Suggestions are welcome.
The code used to deploy wake lock is:
Context context = getApplicationContext();
PowerManager pm = (PowerManager)context.getSystemService(Context.POWER_SERVICE);
final PowerManager.WakeLock wakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "sachin:sachin");
wakeLock.acquire();
The permission entered in the manifest file:
uses-permission android:name="android.permission.WAKE_LOCK"
android:noHistory="false" android:excludeFromRecents="false"
Upvotes: 1
Views: 566
Reputation: 59
A wakelock, in layman’s terms, is just a way for an app to keep the CPU/screen/other things awake when the phone is idle in order to perform a specific background task. Some apps do legitimately need wakelocks in order to function properly, but the problem comes when some applications hold wakelocks repeatedly, hold them for a long time without dropping them, or do excessive/unnecessary network and CPU tasks taking advantage of these wakelocks.
Case in point: apps like Snapchat, Facebook, Messenger or other social media apps include misbehaving wakelocks. This tutorial is simply a way to stop these wakelocks from happening again without uninstalling the app. If, however, you notice that the app stops functioning correctly after using this ADB command, you can change things back to the way they were by re-running the command and changing “ignore” to “allow”, or by simply uninstalling then re-installing the app again.
Upvotes: 0