Let me put a simple test application, One activity and One Service. for the service, it will do the background job like networking, computation. and notify the Acidity to do some UI side rendering or notification to User.
first , Create one Simple Android application with just one Activity,
public class UIActivity extends Activity { |
then Add one Handler in the activity which will be called by the bakcground service when there are any change get triggered,
Handler handler=new Handler() |
then Create one Service inherited from IntentService, here we defined a timer which will do the periodic checking and send back data change to activity,
public class BackService extends IntentService { public BackService() } |
for Activity, need to specify the handler through putextra and start the service,
public void onCreate(Bundle savedInstanceState) { |
remember to register the backservice into the manifest.xml,
when you run the app, you will see the textview will change every 3 seconds,
run adb logcat XXXXX:D *:S, even the activity is brought to background , the Activity still get the change from service,
2 comments:
Is passing a Handler really the best way to handle passing messages from the IntentService to the calling Activity? If the activity is paused/killed the Handler is going to get destroyed as well... Broadcast intents would probably be the best solution.
yes a friend of me faced the same sort of problem, but now this app solved.
Jaycon
Arduino
Post a Comment