[AC-6128] Android - REQUEST_IGNORE_BATTERY_OPTIMIZATIONS doesn't work
GitHub Issue | n/a |
---|---|
Type | Bug |
Priority | n/a |
Status | Resolved |
Resolution | Needs more info |
Resolution Date | 2019-02-23T16:23:28.000+0000 |
Affected Version/s | n/a |
Fix Version/s | n/a |
Components | n/a |
Labels | android, battery, optimization, permissions |
Reporter | josh.mocek |
Assignee | Shak Hossain |
Created | 2019-02-06T14:55:26.000+0000 |
Updated | 2019-02-23T16:23:29.000+0000 |
Description
In android 8.0 some background processes don't run because the app is no longer in the foreground. I would like to add this line to the tiapp.xml to ignore the battery optimization, but it doesn't work.
<uses-permission android:name="android.permission.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS" />
Attachments
File | Date | Size |
---|---|---|
tiapp.xml | 2019-02-06T14:46:03.000+0000 | 3811 |
Hello [~josh.mocek], Can you please share a sample code/project for us to test the issue? Thanks.
I can, but the only thing that's going to happen is you make the boiler plate app and replace that tiapp.xml with the one I attached.
You should be using a "foreground" service. That's what Google prefers because it involves displaying a notification in the top status bar which informs the end-user that your app is doing work in the background. We added foreground service support in Titanium 7.3.0. Have a look at the below for a code example. https://github.com/appcelerator/titanium_mobile/pull/10076 Also note that you'll need to add the following Android permission to use a foreground service.
So I have set an interval to create an http request to the server every 10 minutes. If I turn battery optimization off they run. If Android 8 is optimizing the battery it doesn't. I tested that foreground service and it didn't fix the problem.
Okay. I took a deeper dive into this. I think you have to worry about doze mode too which kill network communications. And doze mode ignores wake-locks too. https://developer.android.com/training/monitoring-device-state/doze-standby#restrictions The "REQUEST_IGNORE_BATTERY_OPTIMIZATIONS" manifest permission isn't enough. You also have to request the end-user's permission as well. Details about this can be found below. It involves displaying a system dialog via a
startActivity()
and anIntent
, which you *+can+* do in JavaScript. Just note that the end-user can say "no" and ban your app from ever requesting this again. https://commonsware.com/blog/2015/11/11/google-anti-trust-issues.html So, you need to add the permission in your "tiapp.xml". Titanium blindly copies whatever permission you add here, so this part was already working.And you can request the end-user to give your app permission as follows.
Unfortunately, Google doesn't appear to provide any feedback on which option ("Allow" or "Deny") was selected in the request dialog shown by the above code. Google doesn't return a "result" when launched via
startActivityForResult()
. Nor are there any native listeners. It looks like your only option is to poll the Java [PowerManager.isIgnoringBatteryOptimizations()](https://developer.android.com/reference/android/os/PowerManager#isIgnoringBatteryOptimizations(java.lang.String)) method via hyperloop. Doing the above appears to be your only option. And it appears to be frowned upon by Google too and they'll likely question it if they ever review your app to be "featured" on Google Play. Google's list of what they think is acceptable for an app to do this can be found below. https://developer.android.com/training/monitoring-device-state/doze-standby#whitelisting-casesHello [~josh.mocek], Did you find our last reply helpful for your progress? Let us know the updates from your end.
Ti.Android.currentActivity.startActivity is always throws a red screen for me
Hello [~josh.mocek], Did you try the code guide provided by Joshua? Can you share the log of the error? also the error screenshot will be helpful. Thanks.