2

I've made a foreground service to constantly scan for BLE devices around me. For some reason it seems to work flawlessly on my stock android device Google pixel and also on Samsung S9+.

But recently I tested the app with a Chinese ROM(Oneplus 6T, Xiaomi Poco F1) the foreground service seems to be killed there after a few minutes. I've used workmanager to restart service but the app is not restarting and I get a bug report instead for the app.

Also on Chinese ROM devices below android 8.0(Xiaomi redmi 3s prime), simple service wont work, I need to use a foreground service there as well. Is there any solution to solving this?

Divye Shah
  • 747
  • 1
  • 11
  • 24

2 Answers2

1

Ask users to whitelist your app. This is the only solution. Even foreground service + wake lock won't work.

There was a discussion last month: Workmanager reliability for periodic tasks on Chinese roms (Xiaomi, Huawei, and so on). There are some useful links in there but eventually you'll have to let users whitelist your app in every ROM's specific battery optimization(or other name) settings.

Dewey Reed
  • 4,353
  • 2
  • 27
  • 41
  • @DeweyReed I am using Xiaomi Poco F1 and OnePlus 6T as of now. In OnePlus 6T I turned my apps preference to don't optimise, yet it's killed. and on Poco F1 the auto start is on, but I still face the issue where it gets killed and has to be restarted by WorkManager or an intent. Can you shed some light on this? – Divye Shah Nov 29 '18 at 12:25
  • @DivyeShah I'm afraid I can't. I don't own any of those phones and the whitelist procedure is quite different. But in that link, you can find this link https://bitbucket.org/copluk/acr/issues/607. There're useful demonstration pictures. – Dewey Reed Nov 29 '18 at 12:45
  • @DeweyReed By whitelisting you mean turning off battery optimisation right? – Divye Shah Nov 29 '18 at 14:28
  • @DeweyReed that helped a lot! Thanks but the Chinese ROMs are a nightmare. I dont know how I can convince users to turn offball these battery optimisations! – Divye Shah Nov 29 '18 at 20:24
  • @DeweyReed I just noticed after installing ACR on my OnePlus, it does not ask for battery optimisation permission, and yet it keeps running, how is that possible? – Divye Shah Dec 04 '18 at 13:47
  • @DivyeShah What is ACR? All I can do is to take a guess. Maybe OnePlus has whitelisted it? Some popular apps have this privilege. – Dewey Reed Dec 04 '18 at 13:54
  • @DeweyReed Try to install truecaller from play store and in settings go to Caller ID and ignore battery optimisations(Idk how these take this permission for the app specifically), Then when you receive a call the foreground service starts instantaneously!! Let me know if you can find something! BTW ACR(automatic call recorder) stopped after a bit without optimisation permissions. – Divye Shah Dec 04 '18 at 14:31
  • @DivyeShah Thanks for explanation. I'll see what I can do. – Dewey Reed Dec 05 '18 at 01:57
  • @DeweyReed I tried intent.setAction(Settings.ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS); but it seems it's different from intent.setAction(Settings.ACTION_IGNORE_BATTERY_OPTIMIZATION_SETTINGS); I tried the first and got that permission in dialogue but the second one redirects user to the battery optimisation page – Divye Shah Dec 05 '18 at 10:04
  • @DivyeShah Normally an app doesn't need to ignore battery optimization. Foreground service and wake lock are enough. Chinese ROMs use their own background manager which is the primary killer. – Dewey Reed Dec 05 '18 at 11:09
  • @DeweyReed So that means the settings shown in the github issues link you shared are essential to make it run on android 8+ devices regardless of battery optimisation? Also did you try truecaller? Been experimenting but could not figure out how it works – Divye Shah Dec 05 '18 at 11:39
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/184753/discussion-between-dewey-reed-and-divye-shah). – Dewey Reed Dec 05 '18 at 12:00
0

A simple approach would be to ask user to put your app in non-optimized apps by opening the battery optimization settings at the starting of your app Use below code to open the setting:

    Intent batterySaverIntent=new Intent(Settings.ACTION_BATTERY_SAVER_SETTINGS);
startActivity(batterySaverIntent);

Or you can try this:

  @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        return START_STICKY;
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        startForegroundService(new Intent(this, ServiceClass.class));
    }
Vikash Bijarniya
  • 404
  • 4
  • 10