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

Mr.Popular picture Mr.Popular · Dec 22, 2016 · Viewed 9k times · Source

MyApp's Receiver is just working fine if it in background which is:

    public class MySmsReceiver extends WakefulBroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        System.out.println("triggered sms");
        if(intent.getAction().equals(Telephony.Sms.Intents.SMS_RECEIVED_ACTION)) {
          Toast.makeText(context, "message Received", Toast.LENGHT_SHORT).show();
    }
}

and my manifest file for receiver is

        <receiver android:name=".MySmsReceiver">
        <intent-filter>
            <action android:name="android.provider.Telephony.SMS_RECEIVED" />

        </intent-filter>
    </receiver>

Document says that if i declare a receiver in manifest file it should always work. but in my case it's not...

But whenever I clear my recent apps by swiping them it just stopped working. and after i went to taskmanager and saw that MyApps forcestop being disabled like below screenshot of my app in taskmanager

and i figured it out some apps like watsapp and fb always kept in memory am saying this bcoz after the swiping of clear recent apps still the task manager has the followig state like below

fb state in task manager

and watsapp state in taskmanager

How can i do this into my app.. what will made my app to behave like other thirdparty apps like watspp and facebook...how can i make my app to be in memory always I am asking this Then only My receiver will always work.. If i am wrong then give me a solution to do this...

I Have been searching this a lot and still i cannot find the solution... Am i asking wrong? or really is there a way to do it? Please someone help me... This making me a week!!! Hope i explained my problem here if i am not ask me I'll give you immediate response.

Answer

David Wasser picture David Wasser · Dec 28, 2016

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).