I am starting a foreground service from a fragment which gets destroyed after call to startService()
, which is a reason I can't use ResultReceiver
or Messanger
. So the option remains PendingIntent
. How can I communicate between foreground service(hosted in different process) from any activity/fragment using PendingIntent?
相关问题
- How can I create this custom Bottom Navigation on
- Bottom Navigation View gets Shrink Down
- How to make that the snackbar action button be sho
- Listening to outgoing sms not working android
- How to create Circular view on android wear?
相关文章
- android开发 怎么把图片放入drawable的文件夹下
- android上如何获取/storage/emulated/下的文件列表
- androidStudio有个箭头不认识
- SQLite不能创建表
- Windows - Android SDK manager not listing any plat
- Animate Recycler View grid when number of columns
- Why is the app closing suddenly without showing an
- Android OverlayItem.setMarker(): Change the marker
You have two separate issues:
How do you get data from the service process to the UI process?
How do you get the data from whatever you did for #1 to whatever portion of the UI needs that data?
There are any number of solutions for #1:
PendingIntent
,ResultReceiver
,Messenger
, AIDL-defined callback for a bound service connection, etc. #2 then mostly is a matter of using an event bus or something similar to alert all relevant Java objects about the new data.So, for example, here is an off-the-cuff recipe for using a
PendingIntent
for this:Implement a
BroadcastReceiver
orService
in your activity process, registered in the manifest, but with no<intent-filter>
As part of calling
startService()
, create aPendingIntent
using itsgetBroadcast()
orgetService()
factory method, with anIntent
that identifies yourBroadcastReceiver
orService
, and put thatPendingIntent
in an extra for theIntent
used withstartService()
Your service in the other process, when it has data to deliver to the activity process, calls
send()
on thePendingIntent
, including anIntent
with data to fill into the broadcast or service requestYour
BroadcastReceiver
orService
from step #1 takes theIntent
delivered to it and uses an event bus to let the rest of your activity process know about whatever happened, also handling the case where nothing in the activity process is registered for the event (e.g., raise aNotification
if all activities were destroyed)