How does a service communicate with an activity? - android

I want to create an application where i need to implement the concept of service. The service concept should be worked out from my activity.
Can anyone help out?
Thanks,
Niki

If your service shall only be manipulated and accessed in your own application, implement simply a local service (See 5. in the Service class documentation).
You can then directly call the service functions from the activity.
If your service is meant to be available publicly, it is a bit more complicated. You'll need an AIDL interface. You can download the Music application source code that includes a service to play audio files in the background and can be controlled by any application. The control is done using Broadcast messages (see BroadcastReceiver).

Related

android -best way to communicate between activity and background service

I'm trying to build a small MusicPlayer for learning purposes, and I'm stuck at a point on how to make communication between a background process and the activity. I've haven't seen anyone else's code but in my(almost amateur design) the activity and the service must be updating each other every second since the activity's control should be reflected in the services controls(like if the service had a GUI in android's notification bar) and the music playing should update the Activity's seekbar etc.
What would be the best way to implement the service, so that it can run independent but in sync with the activity?
I followed these instructions:
https://developer.android.com/guide/components/bound-services
A bound service is an implementation of the Service class that allows other applications to bind to it and interact with it. To provide binding for a service, you must implement the onBind() callback method. This method returns an IBinder object that defines the programming interface that clients can use to interact with the service.
Basically you implement your service and your activity as you have done and then when the activity resumes it can bind the service so that they can communicate.
In that same page you can find some code of how is it done in java and kotlin.

Android service inside or outside the main app

I have a complex app that works, and it has a messaging section. I want to keep it updated with the latest messages that are located on a remote server (mysql)
I need a service to check for new messages on the server. I already did that, but I need to know if it is Ok to implement the service inside the same MAIN app, or is it better to make the service a separate app.
The service uses AlarmManager to fire every minute.
If the better way is to make the service independent then how can I make two different apps use the same SQLite database?
If both things are together (service inside main app) then the SQLite is not an issue since it is located in the same folder.
Please give me a solution and an explication for it.
Thanks
If your service does specific work only for your app, then it should be with in your app.
Since the service runs on the background , it won't affect the activity. You can bind to the service using a service connection and communicate with the service from activity.
Check this link http://developer.android.com/reference/android/app/Service.html on how to bind the service.

Background Service in Android - Need Help

I need some help or suggestions regarding Background services.
Well I want to achieve this. I have an application with Some Views that application also has a Background Service that always keeps on running.
In my views there is a Button whenever I press that button, that button passes some files to the Background Service and my Background service upload that file onto some server.
I am done with the uploading process. Now I want to know that how can I make a Background Service that always keeps on running and on my tapping of the button it sends a file to the Service.
I am new in Background service implementation.
Please guide Friends with some tutorials, suggestions or guidelines.
Thanks a bunch
You've probably already read some of the Android Service documentation, but I suggest studying it further and looking at the Local Service Sample if you have not done so already:
http://developer.android.com/reference/android/app/Service.html
It sounds like you have already got your Service up and running, and I think the actual problem you are trying to solve now is how to communicate data from your Activity to your Service. When your Activity is bound with a Service that's part of the same application, that service is in the same process and runs on the same main UI thread, so once you get the IBinder object from the Service after binding with it, you can simply directly call the functions in that Service from your Activity. Similarly, you can pass your Service a handler object from your Activity so that the service can send messages or post Runnables to your Activity. Communication with a local Service is therefore quite simple.
So if you take a look at the Local Service Sample in the link above, you will see a section in the code where we get a reference to the Service once binding has completed:
mBoundService = ((LocalService.LocalBinder)service).getService();
After that point, it's possible to directly call methods on that Service that's in the same application. For example, you could have a method called sendFile in your Service. In your Activity, you might do something like:
mBoundService.sendFile( myStuffObject );
There are quite a number of questions on Stack Overflow regarding communicating between an Activity and a Service, and I think you'd find it beneficial to search and read these.
A standard Android service will do just fine in this case.
It will continue running in the background untill its work is finished or until you ask it to stop.
There is a topic on the android dev site explaining services in detail.
you should go for android Service that is used for Background operation . Inside the service your have use TimerTask which will be checking the Queue for every x sec and when any items present in the Queue it will pull the item and upload it to the server.
here is the link for Android Service..
http://developer.android.com/reference/android/app/Service.html
Link fro Queue.http://developer.android.com/reference/java/util/Queue.html

Android - Shared networking between activities

I am developing an application which utilizes a custom network protocol over TCP. Several tasks within the application use different activities however each activity requires the networking enabled (since each activity has to send/receive some custom packets over the network).
So my idea is basically to
At application start - create a service to handle networking ( I read somewhere that I should do the networking in another thread in this service to prevent ANR)
When a new activity is run, it binds to that service. During that time it sends/receives data from the service. When the activity ends, it unbinds from the service.
I would like to know if this makes sense.
Also, I understand that I can send data to the service (to send over the network) using the aidl interface, but how would I receive data from the service? Polling through the aidl seems to be the only way I can think of - which means I would need another thread in each of my activities to prevent ANR. Surely there is some better way.
I appreciate your help and am open to suggestions.
Actually I've been reading more and am beginning to look at callback methods in the sample code provided here. After reading it more thoroughly I have a better understanding of the services and AIDL.

How to establish a two-way communication between Activity and Service in different process?

I'm working on establishing a two-way communication between an Activity and a Service which runs in a different process.
Querying the process from the Activity is no big deal. But I want the process to notify the Activity on events. The idea behind it is this: the service runs independently from the actual app. It queries a webserver periodically. If a new task is found on the webserver the process should notify the activity.
I found this thread over at AndDev.org but it doesn't seem to work for me. I've been messing around with BroadcastReceiver. I've implemented an interface which should notify the Activity but the problem is that the listener is always null since the Broadcast from the process is done via Intent, hence the class that extends BroadcastReceiver will be newly instantiated.
How can I establish a 2-way communication? This has to be possible.
Thanks for any help,
steff
Either use BroadcastReceiver or have the Activity register a callback or listener object that the Service calls on key events. The links above are to book example projects demonstrating each of those techniques.
I think you should have the BroadcastReceiver start your activity again with the result in the Intent.
Or you could use AIDL about AIDL. The samples also have an (multiple?) example how to use AIDL and services. But AIDL might be to much of a hassle for your purpose.
You have to use BroadcastReceiver to receive intents, and when you want to communicate simply make an Intent with appropriate values.
This way you should be able to make a 2-way communication between any component.

Categories

Resources