I am creating an chat app which is connected to web socket, using a background service. But I should connect it to the activity to show the new messages. Maybe in the main conversations list, or inside the conversation itself.
So, first, I want to check if my service is running, then getting it bound to the opened activity.
And I want the service to tell the activities that there is a new message.
So, is it good to do it using 'bindService'? Because I feel that there's something not right.
You can send messages from the Service to Activities with the BroadcastReceiver class. I would get your information from the following link:
https://developer.android.com/guide/components/broadcasts
Related
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.
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.
I am implementing a Service which will start when MainActivity starts. This service checks for and maintains a network connection.
I have not bound the Service to the Activity because I want the service to continue running even if the activity isn't available. The Service will receive messages from the server that it is connected to.
I am struggling to choose the best logic to do the following when the service receives a message.
Check if MainActivity is currently open and in front of the user
If it is call some methods in the activity to interact with the UI
If there is no activity update the notification are.
My question is;
How do I correctly check if the activity is running in the UI from my service? I know that I could bind the service but I wouldn't want to unbind it if the activity is closed. Would that be a problem?
I will somehow need to send messages from the service to the activity. Given the above scenario what would be the best way to do this?
Do it differently.
If your Service does not run in a separate process from your Activities, then your Service can provide a synchronized (multithread-safe) list of messages via a subcalssed Application object, where your Activity can look it up. However, this would only be best if the polling occurs on certain other events.
If you want to sort of "push" the message to your Activity, your Activity should register with your service upon finding out that it runs, not the other way round. In this scenario, your Activity should implement an interface through which the Service can notify your Activity of new messages in a Thread-safe way.
Of course you could also go straightforward and simply post notifications which open an Activity, but as I understood it, you want to provide a more smooth integration.
An idea would be to let your Service send status bar notifications when a new update is available.
Implement the notification such that when clicked, to open MainActivity.
Then, when the user will click on the notification, if the MainActivity is already running then it will be brought to front, otherwise it will be started.
So this basically eliminates the need to check if MainActivity is currently open in front of the user (which I see as a bad practice).
I am developing a chat client in which I have a Service which is listening continuously from XMPP server. I have few questions regarding the architecture of this service.
I have read in the documentation of Service class on Android Development Page that onCreate() will be called once when the service run for the first time thats why I have written the connection to the server code in the service onCreate() method. Is it OK?
When I click on a ListView element which is the name of my friend in my client activity a new activity will open which will bind to the service for listening and sending chat messages. I want to know how a service can differentiate that a specific message is for which activity? because If I am having chat with more than 1 friends there will be more than 1 activites and the service has to differentiate that which message is for which activity. I need to know how can I implement this mechanism ? It could be very helpful if there is any tutorial for this or describe what do I need to do to implement this mechanism?
Is it OK?
That is impossible to answer in the abstract. There is nothing obviously wrong with that approach.
I need to know how can I implement this mechanism ?
To be honest, that UI sounds... awkward. That being said, you can have the activity supply a listener or callback object to the service, which the service uses to route messages back to that activity.
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