30

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.

AndroidHacker
  • 3,588
  • 22
  • 44
Mr.Popular
  • 825
  • 8
  • 15
  • 2
    On which device you tested? Some devices force stops the app when cleared from recent apps list except some popular apps. – Monish Kamble Dec 22 '16 at 07:22
  • Hauwei Honor android version 4.4 – Mr.Popular Dec 22 '16 at 07:24
  • watsapp still a third party app and i installed just as another apps but still that can do the trick.. How? how can i do that?? – Mr.Popular Dec 22 '16 at 07:25
  • 1
    Try it on emulator and some others devices. – Monish Kamble Dec 22 '16 at 07:26
  • android 5.0 lenova working fine – Mr.Popular Dec 22 '16 at 07:27
  • but my device clear the app from memory.. i just want to develop an app that should work for all devices – Mr.Popular Dec 22 '16 at 07:27
  • 1
    Sometime back I came across a thread on Stackoverflow with same issue. I am searching for it so that I'll point you there. – Monish Kamble Dec 22 '16 at 07:29
  • see this http://androidtrainningcenter.blogspot.ca/2013/05/how-to-make-android-service-unstoppable.html – masoud vali Dec 22 '16 at 08:03
  • Thanks for your response @masoudvali.. restarting service on lowmemory situations thats not probably a good idea..And whenever i swiping clear recent apps services are stopped working without calling onDestroy and it wont get restarted itself even if i returned START_STICKY – Mr.Popular Dec 22 '16 at 09:09
  • Same issue with Xiaomi http://stackoverflow.com/questions/30748107/xiaomi-does-not-receive-notification-when-application-is-not-running , http://stackoverflow.com/questions/31355084/how-to-keep-my-app-service-keep-running-even-if-app-is-closed-from-task-manger-i – Monish Kamble Dec 22 '16 at 09:48
  • i am sure its not depends on os... wat about the watsapp fb..?? – Mr.Popular Dec 22 '16 at 09:53
  • i can see many more apps having that bhavior not only wats app and fb.. MX PLAYER , CLASS OF CLANS more and more – Mr.Popular Dec 22 '16 at 09:55
  • 1
    You might want to check solutions given in this [SO post](http://stackoverflow.com/questions/36324794/android-system-killed-my-service-when-i-clear-all-recent-app) and see if it will help. This [particular answer](http://stackoverflow.com/a/36325065) gives out some methods that your app might also need to make your receiver less likely to stop working such as returning `START_STICKY` from `onStartCommand()`. – Teyam Dec 23 '16 at 09:55
  • Those are all about services.. Does it related to my Question??? – Mr.Popular Dec 23 '16 at 10:48
  • @Teyam if i use a Foreground Service with notification the above said bhavior available but i dont want to give a notification.. – Mr.Popular Dec 23 '16 at 11:17
  • 1
    @Mr.Popular You might want to check http://stackoverflow.com/questions/21550204/how-to-automatically-restart-a-service-even-if-user-force-close-it and use START_STICKY to keep it going, but if the device actually force closes the app the same as pressing the button in your screen, it's not starting it again until user interacts with it. FB and whatsapp probably get special treatment as others have mentioned. – Steve M Dec 28 '16 at 06:59
  • @SteveM i am asking about that special treatment they are just like other third party android applications so i can do watever they had done it in their apps?? cant i??? – Mr.Popular Dec 28 '16 at 07:02
  • 1
    @Mr.Popular Not necessarily. If what the other poster says is true, some manufacturers will cause a real force stop when swiped away for most packages, but have different behavior for the FB and whatsapp packages. Manufacturers definitely change what the swipe behavior does. On my devices swiping does not force stop anything, and the broadcast receiver continues working. – Steve M Dec 28 '16 at 07:08
  • 1
    @Mr.Popular And if it's really force stopped there's nothing at all you can do about it to restart it. It removes it from AlarmManager, etc. – Steve M Dec 28 '16 at 07:13
  • but how watsapp working if you are taking about manufacturers fault??? – Mr.Popular Dec 28 '16 at 07:45
  • 1
    @SteveM mxplayer and more games??? how can they.. does all of them having some special treatments – Mr.Popular Dec 28 '16 at 07:47

3 Answers3

22

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

Community
  • 1
  • 1
David Wasser
  • 85,616
  • 15
  • 182
  • 239
  • I have installed my app from system only locally... Is there any documentation available about play store apps and local apps ??? – Mr.Popular Dec 29 '16 at 04:59
  • Unfortunately none of this is documented anywhere :-( Manufacturers do what they want and the rest of us have to figure it out. – David Wasser Dec 29 '16 at 10:10
  • 1
    other devices just working fine with startsticky and honor devices only let the protected apps to run in background always.. so after putting my apps as protected apps then its just fine.. – Mr.Popular Dec 29 '16 at 13:18
  • @DavidWasser Sir I am facing the same problem in my lenovo k5 vibe ,Please help me to resolve this . – Sidhartha Jul 12 '17 at 08:07
21

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.

Govind
  • 2,142
  • 22
  • 38
  • Thanks man i am working on it if it solved my problem i ll accept this answer.. Thanks you helped a lot... – Mr.Popular Dec 29 '16 at 04:57
  • did you found the solution? @Mr.Popular – Moorthy Sep 16 '17 at 18:24
  • 1
    in `xiaomi` device, when I kill app from recent task -> `onTaskRemoved`,`onDestroy` not fired :( – Linh Nov 06 '17 at 04:23
  • 2
    @PhanVanLinh onTaskRemoved won't work in devices where exclusive task manager is being used by manufacturer. (Ex: Xiaomi, Honor, etc). Check the first part of the answer where I've specified something very specific for Xiaomi devices, which might be of a help to you. – Govind Nov 09 '17 at 05:15
  • @Govind thank you for nice explain, im sorry I miss the first part in your answer – Linh Nov 09 '17 at 05:48
0

If the "Force Stop" button is disabled it means swiping the app away in Overview has caused your app to be force stopped on this device. You will not be receiving any broadcasts until the user interacts with your app again.

Steve M
  • 8,136
  • 8
  • 40
  • 84
  • but watsapp doesnt getting forcestopped... hows that – Mr.Popular Dec 28 '16 at 07:45
  • 1
    if you say its having some special permissions then how can i make my app to get that??? – Mr.Popular Dec 28 '16 at 07:48
  • Get added to whatever list of special apps for that manufacturer I guess. But really, try it out on Samsung's Remote Test Lab to see what they do and decide if you can tolerate this behavior on some devices and put in warning message for the user if necessary. If the app is not specifically force stopped, there are ways of keeping it going as mentioned in some of the links already posted. – Steve M Dec 28 '16 at 08:00
  • there are ways of keeping it going as mentioned in some of the links already posted>> if you hav any posts which can give the bhavior like i said then tell me – Mr.Popular Dec 28 '16 at 12:07
  • Do you any documents which assures your >>Get added to whatever list of special apps for that manufacturer I guess – Mr.Popular Dec 28 '16 at 12:08