0

I am developing an app which will be performing some background function within a JobIntentService.

The user initiates action from Screen A. A background Intent Service is started. After the Intent service finishes its work it sends a callback to the Screen A and then Screen Sartes Screen B which utilizes the results of Intent Service.

My issue is that suppose the user starts the Action and then minimizes the app and then the Android OS clears the Screen A due to running on low Memory and then the Service sends back the callback.

My question is Now since Screen A no longer exists will it receive the callback? Is it possible to figure out that Screen A has to be cleared by Android OS so that my service can directly start Screen B?

Also, how can such a scenario be handled?

Any suggestion would be grateful.

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
Rahulrr2602
  • 701
  • 1
  • 13
  • 34

2 Answers2

2

The better approach is to create a standalone BroadcastReceiver. This insures that your app can respond to the broadcast, whether or not the Service is running. In this case you can check in service activity is still alive or not. then take decision on that.

Use the below method with your package name. It will return true if any of your activities is in foreground.

public boolean isForeground(String myPackage) {
    ActivityManager manager = (ActivityManager) getSystemService(ACTIVITY_SERVICE);
    List<ActivityManager.RunningTaskInfo> runningTaskInfo = manager.getRunningTasks(1); 
    ComponentName componentInfo = runningTaskInfo.get(0).topActivity;
    return componentInfo.getPackageName().equals(myPackage);
}
Sultan Mahmud
  • 1,245
  • 8
  • 18
  • 1
    You beat me to it. I was going to suggest a BroadcastReceiver. – Taslim Oseni Apr 22 '19 at 17:28
  • A very good answer. Thanks! I have just a few questions. Suppose Android OS clears my Screen and after my service is over what should I do? How do I take the user to the New Screen? Can my service start a screen? or do I have to wait for the user to get back to my app and when he is back I should check if the new screen has been shown to him or not and if not then I should show him? And one more thing how should I save the results of the service? – Rahulrr2602 Apr 22 '19 at 19:18
  • 1
    yes you can start activity from service. For small amount of data save it in sharedpreference. – Sultan Mahmud Apr 22 '19 at 19:35
  • Thanks for the answer. I have accepted it as the right answer. – Rahulrr2602 Apr 24 '19 at 23:02
1

Also, how can such a scenario be handled?

You can solve this by the importance of the background task.

If you think that your background task is important to notify the user then You can crate a Notification after your background work is done. So your user will get a update of your task.You can set result data to Intent and start your Screen B after notification click.

If its not important to notify the user the you can save background task result and when user comes back to your app your can send the user to Screen B.

Edit:

I think when your Activity is in background and is killed by System then starting an Activity from background service is not good UX.Because that time user may be doing some important task(game, online streaming etc) on other app. Which will give user a bad user experience.

Abu Yousuf
  • 5,729
  • 3
  • 31
  • 50
  • Thanks for the answer. But how do I check if the Android OS has killed my activity? – Rahulrr2602 Apr 22 '19 at 19:26
  • 1
    Its tough to detect your `Activity` was killed by OS. Check this answer https://stackoverflow.com/a/5226993/1292557 – Abu Yousuf Apr 23 '19 at 05:45
  • 1
    You can try this https://stackoverflow.com/a/34304730/1292557 for detecting Activity is destroyed from Service – Abu Yousuf Apr 23 '19 at 06:09
  • Thanks for the idea of notification and caching the task. Sorry! since the other answer seems to provide a solution by using a Broadcast, I am accepting that :). – Rahulrr2602 Apr 24 '19 at 23:01