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).
Related
So basically what i am trying to do is i want a service that runs in background and updates the LatLong to the server. This operation needs to happen all the time even if the application is running or not.
Now when the application is launched i want the service to calculate the distance between the latlong and update the UI in addition to the work that it was already doing i.e. updating the latlong to the server. i want the service to do the additional work for multiple activities. lets say i launch the application and i am on Activity A, onclick of a button on Activity A the service starts updating the UI and when we click gain it stops updating. Now i am on Activity B and on click i want the service to do some work in addition to the updation of LatLong and update the UI.
What would be the best approach to achieve this.??
EDIT
The problem i am facing is not getting the service update the UI but making the activity communicate with the service when it has already started.
i can pass on some data when i am starting the service but how to communicate with the service when it has already started. How to tell the service that see you are already running and doing some operations now you have to perform some more operation on top of the previous operation.
I can make some static method in the service and call them when i need to perform the extra operation but i dont wanna do that.. i want to better approach.
Here is the basically services runs on the same main thread process as ui. When you want make it run continuously you have run it in your tread. In this way you avoid service being get stopped as application go in background or killed. (Please take look at Service.START_STICKY flag, this is what you need as i guess).
And more coming your second problem of activity getting updated with service information or data that is being collected for this you need look at " How Bind the service". (In activity check for service connection and Binding to a activity, Unbindibg is also there have look at it also). Activity has all the call backs for it you need to implement binder.
EDIT
Service to update ui has to have send and receive intent mechanism. You can broadcast and intent from the services check your activity is running or not. If running broadcast intent and have receiver in activity to listen it.
You can do it by interface mechanism too
Say I have an Activity A which is running a sockets connection to server, the server will keep sending some message and A should process these message and update UI to display them. The sockets connection is running on another thread.
On A I have a button, I wish to press the button and start another activity B.
The requirement is that when user go to activity B, activity A should keep connection with the server and updating its UI. When User back from B, activity A should show up to date info as user never leave.
I did some research, one of the post suggest that using Tabhost Cheat, that put both A and B into an invisible tab host. However I check tab host, when switching tabs the activity will go to onPause state, which disconnect the socket in that activity.
Is solution to work around this? Thank you!
It might be worth taking a look at using an intentservice to do the work with the web socket and have it broadcast messages. In whatever activity you want to read/process the messages you can register a broadcast receiver to get the messages sent from the intentservice thereby ensuring that whenever for example Activity A has it;s onResume method called you could update the ui with the latest messages from the broadcast receiver or just interrogate the intentservice for the latest messages.
The advantage here is that the IntentService will run in the background without needing to any activity hanging around, you might also want to think about introducing something to cut down on the amount of web traffic by having the intentservice only poll for data when it is needing to be shown thereby reducing battery drain and potential cost to the user with using up data allowances, especially if user is on PAYG
An excellent simple example of how this might work can be found here
http://code.tutsplus.com/tutorials/android-fundamentals-intentservice-basics--mobile-6183
I have the following situation: A service is running in the background of my application and regularly receives UDP packets. It uses an instance of my HandleMessageAgent class which analyses every message and shall start a new activity if necessary.
I would like to perform the following task: No matter which activity is in the front (as long as the service is running) I would like to inform the user about an incoming message under certain circumstances. I also need to update the information regularly as long as it is valid. Afterwards it should be closed automatically.
At first I thought about using a Dialog, but I think I cannot use it when the activity is not visible. Therefore I decided to use an activity, as it can be started from a service all the time.
I want to start the activity within the HandleMessageAgent object (in a method). My problem is, that I do not know how I can define an Intent to start an activity within an object, as the Context is not clear to me.
Is there a more elegant way to perform this task? Or can anybody help me with starting an activity from an object method within a service? Thank you!
There are two situations to consider:
When your service need to notify user your activities are not active, because some other app is active. In this case you should notify user via a system-preferred way: Android Notifications. You should not forcefully show dialogs or activities if user is using some other app. That's what notifications are for.
If one of your activities is active (no matter which) then your service should send a broadcast and interested activities should listen for it and act upon it. That way your service will not depend upon specific activity and will not need to keep track which activities are active at the moment the notification must be shown.
You can make your object Parcelable and add it to the Intent that will start the Activity.
Or you can put it in a subclass of Application because that instance is shared between your activities and your services (as long as they're in the same process)
You might need the "START_NEW_TASK" flag on the Intent
I have a service running in the background. Based on some condition it has to start some activity. Activity has to send back the response.
I did google search and found out we have to use Notification mechanism. But I am not clear how to send the response back from activity to the service running.
Also service is collecting sensor data(acclerometer, gps). So should activity be started in separate thread so that collecting sensor data is not affected.
Please clarify.
Activity would be started in main UI thread, instead, your long-running service should run in and manage its own thread, since according to the document, service is also created in main thread.
Basically the best way to communicate is to use Intent. This allows loose couple of sender/receiver (i.e., activity/service in your case). Intent is a large topic in terms of android, and yet it is one of the most fundamental one, I think you should look for tutorials online about it.
If your activity is opened and return the result after completing the task of Activity then you can use startActivityForResult and then return the result.
Using of notification is simple, just create a interface class, and implement the class in your service. When you need to send data back to service, you can just call the appropriate method with data.
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