clear Recent apps wipe the apps memory and my receiver stopped working

Solution 1:

On some Huawei devices (and some LG and Xiaomi devices) you need to add your app to the list of apps that are allowed to run in the background. If you don't, once your app is stopped (by swiping from the recent tasks list, or by Android killing the app for resource reasons), it will NOT be automatically restarted.

On Huawei devices, the setting is called "protected apps". You cannot programmatically add your app to the list of "protected apps". You need to tell the user that he has to do it after you've installed your app. Well-known apps (like Whatsapp, Facebook, Google Mail) are automatically added by the manufacturer.

This behaviour may be different on different devices and it may be different on different versions of Android and it may be different if the device is "branded" for a specific mobile operator, as the mobile operators can also tinker with the settings themselves.

See "Protected Apps" setting on Huawei phones, and how to handle it for some more details.

EDIT: Added this:

Also, Android broke the "swipe from recents" behaviour in Android 4.4 (Kitkat) so that it causes problems for apps that have been swiped. Sticky services don't get retarted and broadcast Intents are not delivered. There is some information here about workarounds to deal with that: In android 4.4, swiping app out of recent tasks permanently kills application with its service . Any idea why?

Also, have you installed your app from the Google Play store? It is possible that the behaviour is different for apps that have been installed from the Play store versus apps that are locally installed (from downloads or via adb or whatever).

Solution 2:

Looks like this scenario happens in few devices (Ex: Xiaomi, Honor devices, Samsung devices with SmartManager feature, etc) where application is force stopped when user removes the app from recent tasks.

This is because those manufacturers have added task manager feature by default which force stops the apps for memory/battery management. But few applications like Whatsapp, Facebook works. This can be because they would have whitelisted the most famous applications.

Going by Android Developer documentation / UX recommendations, a possible workaround for this scenario,

Case Xiaomi MIUI based devices:

Create a UX to communicate to the user to enable "AutoStart" permission to your app. Basically user has to open "Security" app -> click on "Permissions" -> click on "Autostart" -> look for your app and enable it. I know its ridiculous and painful, but there is no other straightforward option.

Do the above after checking if the manufacturer is Xiaomi

String manufacturer = "xiaomi";
        if(manufacturer.equalsIgnoreCase(android.os.Build.MANUFACTURER)) {
            //this will open auto start screen where user can enable permission for your app
            Intent intent = new Intent();
            intent.setComponent(new ComponentName("com.miui.securitycenter", "com.miui.permcenter.autostart.AutoStartManagementActivity"));
            startActivity(intent);
        }

Note: This procedure can be different for Honor devices, Samsung devices as its very specific to the manufacturer.

Also as a normal way to solve this for other devices, is to create a sticky service with overriding onTaskRemoved method. Use the below code:

public class BackgroundService extends Service {

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

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

    @Override
    public void onTaskRemoved(Intent rootIntent) {
        //create a intent that you want to start again..
        Intent intent = new Intent(getApplicationContext(), BackgroundService.class);
        PendingIntent pendingIntent = PendingIntent.getService(this, 1, intent, PendingIntent.FLAG_ONE_SHOT);
        AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
        alarmManager.set(AlarmManager.RTC_WAKEUP, SystemClock.elapsedRealtime() + 5000, pendingIntent);
        super.onTaskRemoved(rootIntent);
    }
}

Here the service will start again after 5 seconds. You can replace it with any intent that you want to start again after being cleared from recent apps.