开发者

Refreshing an Activity from service when Active

开发者 https://www.devze.com 2023-01-06 15:11 出处:网络
I\'ve an app where I wish to update the Activity (List activity to be precise) only when the activity from Service is i开发者_开发百科n foreground (Active)

I've an app where I wish to update the Activity (List activity to be precise) only when the activity from Service is i开发者_开发百科n 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 ?


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.


The best solution is to have a centralized place. I consider this to be the best solution. Check this out:

click here

0

精彩评论

暂无评论...
验证码 换一张
取 消