Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I want to run an long running operation in Android. Say the task would run for about 5-10 mins. For this reason I am planning to use a JobIntentService and Bind it to an Activity.

Right now I am using a AsyncTask, even though I know AsyncTask cannot/should not be used for long running operations hence I am planning to change it now. Many times what happens is while the task is running the user minimizes the app and after sometime the Android OS closes/clears the Activity to free up some memory.

So my AsyncTask is kept running without any purpose and crashes while trying to update a view in that Activity.

So I am planning to use an JobIntentService . But will using an JobIntentService and Binding it to an Activity will reduce the chances of Android OS closing/clearing the Activity? or still will it follow the same process?

Any Help would be really grateful.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
126 views
Welcome To Ask or Share your Answers For Others

1 Answer

If your Activity is in the background then blocking Android from killing your Activity doesn't sound like a good idea (and even necessary) to me. Since your Activity is in the background it's not visible to the user and there's no need to update the UI during this time.

Instead, you could do the following:

If Activity is in the foreground then it can receive updates from your JobIntentService via BroadcastReceiver that you register/unregister with LocalBroadcastManager in onResume/onPause.

IF JobIntentService has completed its work when your Activity was killed or in the background you could persist the result somewhere (for example, in SharedPreferences) and then in onResume of your Activity you could read that result from SharedPreferences.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...