I am trying to make 2 applications which will interact with each other using AIDL.
Application 1: Will be a service.
Application 2: Will be Activity(with a button) which will show some data which will be fetched by Application 1 service.
Now to start this interaction I know we can make one AIDL file in both applications and when user presses the button in application 2 we can involve the function of service from application one. That is lets say application 2 requests the current time then application 1(Service) will fetch the current time and return it to application 2.
My doubt is that I want to interact the other way round. I want to inform the activity from service when some particular digit occurs in time(or some other event). I am not sure how to proceed with this way of communication ie from Service to Activity.
Some pointers will be really helpful.
You know you can send message from Activity to service, Reference Bound Service
By Following above tutorial You should Consider Sending Handler from Activity to Service using Messenger Class in Intent.
So now Service and Activity can send message to each other which will execute corresponding Handlers
Use Broadcast receiver in Activity and let Service broadcast messages (with same Intent as used by Broadcast receiver in Activity). These messages which are broadcasted from Service will be received by broadcast receiver in the Activity.
Related
I am developing an application in android studio, and I have to save the location of the device, I did a "locationService" class that extends service, so the location is segmented on my server and the service would be in the background.
I have read the android documentation and there are two types of service (service and linked service), I use the linked service because I need to show the location data in an activity.
The problem is when I close the application, the service dies and does not record the change of location.
How can I prevent the linked service from dying. Thank you
I assume you mean a "bound" Service, not a "linked" Service. A bound Service will stop running when the last client disconnects from it. To prevent this, you need to call startService() and make sure that you return START_STICKY from onStartCommand() in your Service.
You don't need to use a "bound" Service in order to pass data from the Service to an Activity. Another way is to have your Service broadcast the data (by adding the data as an "extra" to an Intent and calling sendBroadcast() with the Intent). Your Activity can then set up a BroadcastReceiver to listen for any data braodcast by your Service.
What is difference between BroadcastReceiver and ResultReceiver in android?
Result Receiver:
Generic interface for receiving a callback result from someone.
Broadcast Receiver:
Base class for code that will receive intents sent by sendBroadcast().
EDIT:
Background: All networking operations/long running operations should take place away from the main thread. Two ways to do this :
Async task - For Simple networking like say retreive an image/ do db
processing
Service - For Complex long running background process
If you need to perform work outside your main thread, but only while the user is interacting with your application, then you should probably instead create a new thread and not a service. For example, if you want to play some music, but only while your activity is running, you might create an Async Thread. But if you want the process to continue even after the user exits the app (say a download) then use a service
Lets say you pick 2. Now
You activity sends a web request to your service
Your service executes that using say DefaultHttpClient
It sends back data to your activity.
The third step of receiving data here can be done in two ways
1.) Broadcast receiver: Multiple receivers can receive your data. Used if you want to send data/notifications across applications(say you are also interacting with fb and twitter, multiple receivers for your web broadcast),
whenever you send broadcast its sent system wide.
2.) Result receiver: Your application is the only receiver of the data. It is an Interface you implement and pass it to the intentService through putExtra. IntentService will then fetch this object
and call its receiver.send function to send anything (in bundle) to
calling activity. Result receiver has
preference over broadcast receivers if your all communication is
internal to your application
EDIT: I should also mention this caution
Caution: A service runs in the main thread of its hosting process—the
service does not create its own thread and does not run in a separate
process (unless you specify otherwise). This means that, if your
service is going to do any CPU intensive work or blocking operations
(such as MP3 playback or networking), you should create a new thread
within the service to do that work. By using a separate thread, you
will reduce the risk of Application Not Responding (ANR) errors and
the application's main thread can remain dedicated to user interaction
with your activities.
A BroadcastReceiver is a receiver receiving broadcasts. Those are sent by someone in the intention that there can be many receivers receiving them (like radio broadcasts).
A ResultReceiver on the other hand is intended to receive a callback result from someone. So this could be compared with a walkie talkie, where you call someone and then are going to receive an answer (a result) from the one you called.
These two classes are completely different. It's actually quite the same difference as between Broadcast and Result.
what it Broadcast? In simple words it's some message which is visible to whole system and it can be consumed by every part of the system (which knows the contract), it wasn't originated by smb reuest;
what is Result? It's something we're expecting to receive from another part of the system. Usually there's only one receiver for result and usually that receiver has requested processing to obtain result (feel the difference - for broadcast nobody needs to do any 'request' to let it originated);
That was explanation from logic point of view. From the code perspective if You would compare BroadcastReceiver and ResultReceiver You could observe huge difference. Basically both classes are built on top of IPC but BroadcastReceiver is much more complex because of it's different nature (which I've tried to explain in first part).
Broadcast Receiver
A broadcast receiver is a component that responds to system-wide broadcast announcements. example, a broadcast announcing that the screen has turned off, the battery is low, or a picture was captured. Applications can also initiate broadcasts—for example, to let other applications know that some data has been downloaded to the device and is available for them to use. Although broadcast receivers don't display a user interface, they may create a status bar notification to alert the user when a broadcast event occurs. More, though, a broadcast receiver is just a "gateway" to other components and is intended to do a very minimal amount of work. For instance, it might initiate a service to perform some work based on the event.
Result Receiver
If your service is going to be part of you application then you are making it way more complex than it needs to be. Since you have a simple use case of getting some data from a Restful Web Service, you should look into ResultReceiver and IntentService.
This Service + ResultReceiver pattern works by starting or binding to the service with startService() when you want to do some action. You can specify the operation to perform and pass in your ResultReceiver (the activity) through the extras in the Intent.
I have to different applications. First application has background service. Second application has activity with textview.
Question: how can I detect second app launch using service from first app? And how to change textview's text from service?
Thanks.
You can't detect the launch on an other application, neither you can change the TextView of a different application directly.
But you can achieve this with cooperation. The second application could notify the Service about being started, and the Service could ask the First Activity to change its TextView.
This can be achieved with intents. The Activity in the first application could start the Service as it starts, and the Service can send a broadcast or send an intent to the activity directly to request it to modify its TextView.
If you want to notify the Activity only when it runs, and don't want to wake it up like in the previous example, then I suggest you to read this link about bound services. It gives you a complete example on how to bind to a service, and also describes how to make requests back to the Activity.
Notice that this example does not show how the service can respond to
the client. If you want the service to respond, then you need to also
create a Messenger in the client. Then when the client receives the
onServiceConnected() callback, it sends a Message to the service that
includes the client's Messenger in the replyTo parameter of the send()
method.
use broadcastreceiver in the that update your another application.
if you are using service then it will not update your GUI of another application.
broadcastreceiver is the best practice to do which you want.
Assume I have 2 application: A.apk and B.apk. Applcation B only consists of 1 background service and has no UI. The pplication A.apk want to call the service in application B.apk, transmit some data and receive the result from application B.apk.
I know there is 1 techniques is using AIDL. But because I just want to perform IPC, but do not need to handle multithreading, it is not necessary to use AIDL.
Could you please tell me is there a technique to do that?
in Activity A call startService() with data you want to transmit into Service B.
In Service B handle this call in onStartCommand(). Then after handling send custom broadcast to Activity A. The broadcast receiver must be registered in this Activity.
I have a problem:
I have a UI thread which displays webwiew and another chatActivity which displays chat.
I keep on getting data from server which would be displayed on both Activities.
What is the best way to do this viz doInBackground or Service,
If service, than can i bind 2 activity with 1 service i.e. if user press logout from UI or chatActivity, both activity and service should die otherwise service sud update both activity contents.
I am new to 'service' so any reference or sample code would be helpful.
Sounds like you need to broadcast some information. You than will be able to set broadcast receivers in any activity/service you would like to get notified.
Read more online about Broadcastreceiver and about send broadcast