Battery optimization not available on WearOS - android

I'm developing a tracking application that runs on Wear OS watches. I have some troubles when the watch goes to sleep. As soon as the watch goes to Deep Idle mode, all sensors acquisition stops. For that I'll need to whitelist the app using:
Intent intent = new Intent();
intent.setAction(Settings.ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS);
intent.setData(Uri.parse("package:" + packageName));
context.startActivity(intent);
This piece of code works well on mobile version but nothing happen on Wear OS version. I realized that Wear OS as no UI to set the battery optimization mode which should be the cause of the fact that nothing happens when trying to change the optimization.
I also used a wake lock to keep the CPU partially awake with:
PowerManager powerManager = (PowerManager) getSystemService(POWER_SERVICE);
wakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "MY-WAKELOCK");
But even with that the watch goes to deep sleep after about 15min ?
The last solution I see is to force the screen on or at least dimmed but it will drain the battery very fast.
Is there another solution to keep sensors working ? How can I whitelist my app on a Wear OS device as no UI seems available ? As anyone already encountered this issue?

Related

how to disable the question (Do you want to ignore battery optimization?) android studio

I was having problem whit android media player because when I turn off the screen the system goes to sleep mode and stop it and I didnt want that because nobody wants to listen music and have the screen on (who do that?).
I noticed that the problem was on the battery optimizations because if the mobile was plug to power cable it keeps the music playin even if it goes to sleep mode.
I tried with a lot of posibilities to fix it (reading android documentation and trying every thing I was finding in the net) but what works was this code:
public void doPower() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
String packageName = getActivity().getPackageName();
PowerManager pm = (PowerManager) getActivity().getSystemService(Context.POWER_SERVICE);
if (!pm.isIgnoringBatteryOptimizations(packageName)) {
try {
//some device doesn't has activity to handle this intent
//so add try catch
Intent intent = new Intent();
intent.setAction(android.provider.Settings.ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS);
intent.setData(Uri.parse("package:" + packageName));
startActivity(intent);
} catch (Exception e) {
}
}
}
}
The problem here is: The app at the beginning shows an alert asking the user to disable the battery optimizations and I dont want that, because some users may thinks "this is a bad thing" or "why its asking this?" Because an app like spotify doesnt do it.
How can I avoid the alert message? If I cant, how can I add something else to the message? (something like this: "So you will able to listen the radio with the screen turn off")
Thanks in advance and best regards.
You can't avoid that dialog and asking to avoid battery optimizations is absolutely not needed.
What is needed is a wakelock, as described in the MediaPlayer Using wake locks documentation:
When designing applications that play media in the background, the device may go to sleep while your service is running. Because the Android system tries to conserve battery while the device is sleeping, the system tries to shut off any of the phone's features that are not necessary, including the CPU and the WiFi hardware. However, if your service is playing or streaming music, you want to prevent the system from interfering with your playback.
In order to ensure that your service continues to run under those conditions, you have to use "wake locks." A wake lock is a way to signal to the system that your application is using some feature that should stay available even if the phone is idle.
If you're using MediaPlayer, this only requires you use the setWakeMode() method:
mMediaPlayer.setWakeMode(getApplicationContext(), PowerManager.PARTIAL_WAKE_LOCK);
(Note the additional note on the documentation page about holding a wifi lock if you're streaming your media from the network.)

Loosing network android 6 and above if app run in backgroud

I am running service background to execute some tasks with some interval using AlaramManager (setExactAndAllowWhileIdle). If its connected the power charger its running. But if its unplugged device power after some i am loosing the network to my application.
Alaram is waking up the service even device is idle.But my app don't have network.
As per https://developer.android.com/training/monitoring-device-state/doze-standby.html# its restricting the network when phone is idle.I tested the same with applying phone idle ($ adb shell dumpsys battery unplug)
But is there any possible to get network access to my application even phone is idle mode.
Application is not of play store. Its really appropriated for suggestion.
I've used PowerManager.isDeviceIdleMode() in your running service to detect whether you're in Doze and if so just invoke setAlarmClock() method with 250 millis (for example) and random broadcast just to wake the device up, it'll completely wake up from doze and thus will get network access as usual. THe down side is that you lose the battery saving that comes with doze.. :D Please mark this as useful should it actually be usefull in your case.
Once your service (set up with setExactAndAllowWhileIdle()) you can go :
private boolean isDeviceInDozeMode() {
PowerManager pm = (PowerManager)context.getSystemService(Context.POWER_SERVICE);
return pm.isDeviceIdleMode();
So now you know that you're in DOZE or not.
Then you can just set up AlarmClock and set it with setAlarmClock(AlarmClockInfo a, PendingIntent p)
And you're out of Doze with normal network access.
NOTE: Please remember that using setExactAndAllowWhileIdle doesn't mean that your alarm will go off at specified time, it will go off sooner than normal set() method, but not exact (as the method name would suggest). Generally my way isn't perfect in terms of scheduling, but it does exit DOZE programmatically once it's detected on device.
Let me know should you need more update.
I tried many options but none of them works, But finally this works to me.It might help for others.
we can always ask the user to let your app ignore battery optimization functionality using
PowerManager powerManager = (PowerManager)
getSystemService(Context.POWER_SERVICE);
Intent intent=new Intent();
intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
if (powerManager.isIgnoringBatteryOptimizations(getPackageName())) {
intent.setAction(Settings.ACTION_IGNORE_BATTERY_OPTIMIZATION_SETTINGS);
}
else {
intent.setAction(Settings.ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS);
intent.setData(Uri.parse("package:" + getPackageName()));
startActivity(intent);
}
and in the manifest
<uses-permission android:name="android.permission.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS"></uses-permission>
But remember this might make your app not approved by Google Play
https://developer.android.com/training/monitoring-device-state/doze-standby.html#whitelisting-cases

Network access in doze mode

Is it possible to use network access when doze is active? If my app is ignoring battery optimization doesn't go in standby but it's affected by doze anyway. Am I missing anything?
Network access is disabled in doze mode, regardless if your application is ignoring battery optimizations. The only way to wake-up your device from doze mode and to get network access is by sending a high priority Google Cloud Message to your application.
Edit: it is possible to let Android ignore battery optimization for your application, which effectively disables doze mode for your application. However, this requires using the method setExactAndAllowWhileIdle (maximum one wakeup each 15 minutes) and user interaction, which can be done like this:
Intent intent = new Intent();
String packageName = context.getPackageName();
PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
//battery optimizations toggle
if (pm.isIgnoringBatteryOptimizations(packageName))
//give the user the option to enable battery optimizations again
intent.setAction(Settings.ACTION_IGNORE_BATTERY_OPTIMIZATION_SETTINGS);
else {
intent.setAction(Settings.ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS);
intent.setData(Uri.parse("package:" + packageName));
}
context.startActivity(intent);
Edit: it was suggested to add the permission android.permission.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS to the manifest. Besides that this is unnecessary for the suggested solution, it will result your app to be removed from the Play store.
According to the documentation, you can now have a foreground notification for your app which would defy the doze mode and your app should be able to access the network.

Wakelock works only when screen turned on

many things has been written about wakelocks in Android. I'm using wakelock this way:
PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
partialWakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "Partial");
partialWakeLock.acquire();
// few hours work
partialWakeLock.release?
In most of devices, PARTIAL_WAKE_LOCK can keep device running whole night. But there are few models (Samsung Galaxy ACE 2 (i8190) - 4.1.2 etc.), which ignore this kind of wakelock. Wakelock is held, but phone goes to sleep if device's screen is turned off.
Is there some way how to determine, if application is running in device with this problem? I can let user turn devices's screen off for a few seconds and check if work has been done, but is there some automatic way?
Thanks in advance.

How can I prevent Galaxy S3 from stopping my app when turning idle?

I am currently updating my Android app with Samsung Galaxy S3 and was shocked that I couldn't stop the phone pausing my app when turning idle. With the Galaxy S2 of our department there doesn't occur this particular problem, if the screen goes black the app still streams data to the sd-card and over the wifi-network. The S3 stops any data-stream.
I tried now fiddling with the energy- and display-settings but I have no solution to the problem so far. My Internet-search was not succesfull either.
Possible solutions are rooting the new phone and thus making advanced settings visible
or increasing the time-out (which i dont like so much as a solution).
Do you have any ideas how to solve the issue or general input that might enlighten me?
Thnx!
BTW: here is the app in question (no ad):
Google Play Link
I have an app which needs to do something similar (it's a running trainer, so it needs to keep talking while the user keeps their phone in their pocket for half an hour or so.)
First off, a caveat for other people reading: don't do this if you don't have to. If you only need to do something periodically, rather than continuously, consider using AlarmManager, which can wake the phone up from sleep every now and again to do something, so won't hit the user's battery so hard.
But, if you're sure you need to keep the phone awake, you need to use a WakeLock. Here's roughly what I do in my service's onStartCommand:
PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
mWakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK;, "RunClockService");
mWakeLock.acquire();
...where mWakeLock is an instance variable of type PowerManager.WakeLock. PARTIAL_WAKE_LOCK keeps the CPU running, but doesn't keep the screen on. The "RunClockService" tag is just used for debugging, according to the documentation. Change it to your class name.
Then, when I finish needing to keep the phone awake:
mWakeLock.release();
You'll also need to add WAKE_LOCK permission to your AndroidManifest.xml:
<uses-permission android:name="android.permission.WAKE_LOCK"/>

Categories

Resources