How to verify service or broadcast been launched from context? Robolectric - android

I starts service and broadcast from context. How can I verify that the correct intent been sent? If activity been start we can verify with shadow.peekNextStartedActivity() but are there any equivalents?

Related

Shutdown Foreground Service from tap notification action

I have an app that uses a ForegroundService and a BroadcastReceiver.
When the app is started, the ForegroundService is started too and the mandatory notification is sent to user to notify that such a service is running. Based on the App's way to work, Service needs to run indefinetely but i would like that the user could eventually shutdown the Service using a TapAction on this notification.
Actually i achieved this result sending an explicit Intent to BroadcastReceiver and then on the onReceive() method send another explicit Intent to the ForegroundService to shut it down with stopService() method.
I was wondering if there is a way to send directly an Intent from TapAction to ForegroundService considering that there is no onReceive() method on Service

Is there is a way to for activity to communicate a service which start the activity?

I have a service which have a callback function for detecting changes in the clipboard where the callback has a pendingIntent which is used by a notification and later notification used to start an activity once the notification is clicked.So here is my question, so is there is a possibility for the started activity to notify back to service once the activity is started (one way communication is just fine)?
You could send a broadcast Intent from the Activity to notify the Service. The Service should register a BroadcastReceiver that will be triggered by the broadcast Intent.

Android - How to listen to broadcast receiver unregistered?

I have an activity A.java which sends a broadcast and needs to wait for this broadcast unregistered then do something else. This broadcast receiver will invoke another activity B.java and only unregister the broadcast receiver when the activity is finished.
So my question is how to make A.java listen to the broadcastReceiver unregistered?
To be more specific, it's A.java in app A and B.java in app B. I can't figure out a way to communicate between apps but broadcast. Does anyone have better idea?
Thanks!!
I suggest you rethink your app flow. When Activity A sends a local broadcast, Activity B can listen and send back another broadcast after the task which it is supposed to do is done. You don't have to listen to Broadcast Receivers being unregistered, as you should be unregistering them in a lifecycle teardown event (such as onStop).
Wrong appraoch. You cannot be triggered on "unregister" of a receiver.
You can communicate between activities iun a single task either by calling startActivityForResult() or startActivity() and passing data in Intent, or by sending data in broadcast Intents. Leave your receivers registered and listening, and send as many broadcast Intents as necessary.

Communication between Service and Activity each hosted in different process using PendingIntent

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?
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 or Service in your activity process, registered in the manifest, but with no <intent-filter>
As part of calling startService(), create a PendingIntent using its getBroadcast() or getService() factory method, with an Intent that identifies your BroadcastReceiver or Service, and put that PendingIntent in an extra for the Intent used with startService()
Your service in the other process, when it has data to deliver to the activity process, calls send() on the PendingIntent, including an Intent with data to fill into the broadcast or service request
Your BroadcastReceiver or Service from step #1 takes the Intent 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 a Notification if all activities were destroyed)

Will Android BroadcastReceivers still receive intents if the process housing the receiver is killed?

I'm curious if a broadcast receiver specified by a pending intent sent to another process will fire even if the app is killed.
Yes. If an app publishes a BroadcastReceiver via the <receiver> tag in its AndroidManifest.xml, and something kills the app's process, an incoming intent will (re)start the process.
On the other hand, if the app registers a receiver in its Activity.onResume() method, it should unregister it in Activity.onPause().
See Receiver Lifecycle and Process Lifecycle.

Categories

Resources