Titanium JIRA Archive
Appcelerator Community (AC)

[AC-6128] Android - REQUEST_IGNORE_BATTERY_OPTIMIZATIONS doesn't work

GitHub Issuen/a
TypeBug
Priorityn/a
StatusResolved
ResolutionNeeds more info
Resolution Date2019-02-23T16:23:28.000+0000
Affected Version/sn/a
Fix Version/sn/a
Componentsn/a
Labelsandroid, battery, optimization, permissions
Reporterjosh.mocek
AssigneeShak Hossain
Created2019-02-06T14:55:26.000+0000
Updated2019-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

FileDateSize
tiapp.xml2019-02-06T14:46:03.000+00003811

Comments

  1. Sharif AbuDarda 2019-02-06

    Hello [~josh.mocek], Can you please share a sample code/project for us to test the issue? Thanks.
  2. josh.mocek 2019-02-06

    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.
  3. Joshua Quick 2019-02-07

    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.
       <uses-permission android:name="android.permission.FOREGROUND_SERVICE"/>
       
  4. josh.mocek 2019-02-08

    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.
  5. Joshua Quick 2019-02-08

    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 an Intent, 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.
       <?xml version="1.0" encoding="UTF-8"?>
       <ti:app xmlns:ti="http://ti.appcelerator.org">
       	<android xmlns:android="http://schemas.android.com/apk/res/android">
       		<manifest>
       			<uses-permission android:name="android.permission.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS"/>
       		</manifest>
       	</android>
       </ti:app>
       
    And you can request the end-user to give your app permission as follows.
       function requestIgnoreBatteryOptimizations() {
       	if (Ti.Platform.Android.API_LEVEL >= 23) {
       		var intent = Ti.Android.createIntent({
       			action: "android.settings.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS",
       			data: "package:" + Ti.App.Android.launchIntent.packageName,
       		});
       		Ti.Android.currentActivity.startActivity(intent);
       	}
       }
       
    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-cases
  6. Rakhi Mitro 2019-02-12

    Hello [~josh.mocek], Did you find our last reply helpful for your progress? Let us know the updates from your end.
  7. josh.mocek 2019-02-12

    Ti.Android.currentActivity.startActivity is always throws a red screen for me
  8. Sharif AbuDarda 2019-02-12

    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.

JSON Source