10

We are developing an app With MQTT running in a foreground service. The problem is on Xiaomi Redmi Note 7, the service gets killed after we kill the app but on other brands It works fine. I did not test the app on Oppo and Vivo but as I searched they have problem too. In onCreate method of the service, I called startForeground(NOTIFICATION_ID, notification) and my service declaration in manifest is like this

<service
    android:name=".service.mqtt.MqttService"
    android:enabled="true"
    android:exported="false"
    android:foregroundServiceType="location" />

I've also changed foregroundServiceType to connectedDevice|dataSync|mediaPlayback and added android:stopWithTask="false" and returned START_STICKY in onStartCommand method of service but still not working.

frogatto
  • 28,539
  • 11
  • 83
  • 129
Behrad Ranjbar
  • 261
  • 2
  • 13
  • How do you kill the app? swiping it off the screen from recent apps? – frogatto Dec 22 '19 at 09:17
  • @frogatto yes!! – Behrad Ranjbar Dec 23 '19 at 05:53
  • 1
    As a tip, the same happens on "Vivo" either. – Shafizadeh Dec 23 '19 at 06:08
  • According to Android documents, an app having a foreground Service should not get killed when user swipes it off the screen from recent apps, or Android should start it soon later. If the behavior differ on a particular device, I think this is a vendor specific issue and should be reported to their issue tracker. Nevertheless, to work around this problem you can set up a periodic AlarmManager task to check if the Service is running and start it if it's not. – frogatto Dec 23 '19 at 07:15
  • @frogatto I tried that with WorkManager. but worker does not wake up when the app is killed! – Behrad Ranjbar Dec 23 '19 at 09:17
  • 1
    @Shafizadeh That is true! – Behrad Ranjbar Dec 23 '19 at 09:18
  • @BehradRanjbar So, it looks like that the Xiaomi device *force stops* the app. In force stop mode, according to the docs, you would need an *explicit* intent to start your app. i.e. your app won't start by implicit intents. – frogatto Dec 23 '19 at 10:35
  • @frogatto actually I fired this intent Intent xiaomi = new Intent().setComponent(new ComponentName("com.miui.securitycenter", "com.miui.permcenter.autostart.AutoStartManagementActivity")); and got the user to the settings page and whitelisted my app. – Behrad Ranjbar Dec 23 '19 at 12:12

1 Answers1

10

Finally I've found the answer here

private static final Intent[] POWERMANAGER_INTENTS = {
    new Intent().setComponent(new ComponentName("com.miui.securitycenter", "com.miui.permcenter.autostart.AutoStartManagementActivity")),
    new Intent().setComponent(new ComponentName("com.letv.android.letvsafe", "com.letv.android.letvsafe.AutobootManageActivity")),
    new Intent().setComponent(new ComponentName("com.huawei.systemmanager", "com.huawei.systemmanager.startupmgr.ui.StartupNormalAppListActivity")),
    new Intent().setComponent(new ComponentName("com.huawei.systemmanager", "com.huawei.systemmanager.optimize.process.ProtectActivity")),
    new Intent().setComponent(new ComponentName("com.huawei.systemmanager", "com.huawei.systemmanager.appcontrol.activity.StartupAppControlActivity")),
    new Intent().setComponent(new ComponentName("com.coloros.safecenter", "com.coloros.safecenter.permission.startup.StartupAppListActivity")),
    new Intent().setComponent(new ComponentName("com.coloros.safecenter", "com.coloros.safecenter.startupapp.StartupAppListActivity")),
    new Intent().setComponent(new ComponentName("com.oppo.safe", "com.oppo.safe.permission.startup.StartupAppListActivity")),
    new Intent().setComponent(new ComponentName("com.iqoo.secure", "com.iqoo.secure.ui.phoneoptimize.AddWhiteListActivity")),
    new Intent().setComponent(new ComponentName("com.iqoo.secure", "com.iqoo.secure.ui.phoneoptimize.BgStartUpManager")),
    new Intent().setComponent(new ComponentName("com.vivo.permissionmanager", "com.vivo.permissionmanager.activity.BgStartUpManagerActivity")),
    new Intent().setComponent(new ComponentName("com.samsung.android.lool", "com.samsung.android.sm.ui.battery.BatteryActivity")),
    new Intent().setComponent(new ComponentName("com.htc.pitroad", "com.htc.pitroad.landingpage.activity.LandingPageActivity")),
    new Intent().setComponent(new ComponentName("com.asus.mobilemanager", "com.asus.mobilemanager.MainActivity"))
};



for (Intent intent : POWERMANAGER_INTENTS)
    if (getPackageManager().resolveActivity(intent, PackageManager.MATCH_DEFAULT_ONLY) != null) {
            // show dialog to ask user action
        break;
}

edit: Also there is a problem checking if the user enabled auto-start or not. As I searched, currently there are no solutions available. So I designed a solution myself. I created a worker which will save system's time in preferences every 25 min. Every time the app is opened, I check the preferences, and if it is more than 30 min passed from the saved time, It means that worker could not do the work, so possibly user did not enable auto-start the last time and must prompt again.

class BackgroundCheckWorker(val appContext: Context, val workerParams: WorkerParameters) :
Worker(appContext, workerParams), KoinComponent {


override fun doWork(): Result {
    val pref = appContext.getSharedPreferences(PermissionHandler.AUTO_START_PREF, Context.MODE_PRIVATE)
    val editor = pref.edit()
    editor.putString(AUTO_START_PREF_KEY, Calendar.getInstance().time.toString())
    editor.apply()
    return Result.success()
}
}

And in splash I call this function:

fun requestUnrestrictedBackgroundService(context: Activity): Boolean {
            val pref = context.getSharedPreferences(AUTO_START_PREF, Context.MODE_PRIVATE)
            var updated = false
            val lastUpdate = pref.getString(AUTO_START_PREF_KEY, "")
            updated = if (lastUpdate == null || lastUpdate == "") {
                val editor = pref.edit()
                editor.putString(AUTO_START_PREF_KEY, Calendar.getInstance().time.toString())
                editor.apply()
                false
            } else lastUpdate != "" &&
                    DateUtil.minAgo(lastUpdate) <= 30
            if (!updated) {
                for (intent in POWERMANAGER_INTENTS)
                    if (context.packageManager.resolveActivity(
                            intent,
                            PackageManager.MATCH_DEFAULT_ONLY
                        ) != null
                    ) {
                            val dialog = AlertDialog.Builder(context)
                            dialog.setMessage("On this device you must allow us to run services in background")
                                .setPositiveButton("Yes") { paramDialogInterface, paramInt ->
                                    val editor = pref.edit()
                                    editor.putString(AUTO_START_PREF_KEY, Calendar.getInstance().time.toString())
                                    editor.apply()
                                    context.startActivityForResult(intent, 1234)
                                }
                                .setNegativeButton("Cancel") { paramDialogInterface, paramInt -> context.finish() }
                            dialog.show()
                            return false
                    }
            }
            return true
        }
Behrad Ranjbar
  • 261
  • 2
  • 13