Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
322 views
in Technique[技术] by (71.8m points)

android - Refreshing an Activity from service when Active

I've an app where I wish to update the Activity (List activity to be precise) only when the activity from Service is in foreground (Active)

I can use ListActivity Intent but the problem is this brings up activity even when its in background.

I also considered using registerDataSetObserver on ListAdapter but that mandates that cursor should be updated. Updating the Cursor from external service may not be feasible.

Do we have a generic solution to this ?

See Question&Answers more detail:os

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

1 Reply

0 votes
by (71.8m points)

Do we have a generic solution to this ?

Probably a dozen of them. Here are a few:

  • Have the activity register a listener object with the service in onResume() and remove it in onPause()
  • Send a private broadcast, picked up by the activity via registerReceiver() when it is in the foreground
  • Have the activity supply a "pending result"-style PendingIntent to the service
  • Use a ResultReceiver
  • Use a ContentProvider, with the activity holding onto a Cursor from the provider, and the service updating the provider

This set of projects demonstrates the first three, in the context of remote services.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...