I want to create a Android background service, which creates a notification, if a server has restarted, but I need some ideas how to implement it.
I thought about a http connection, where the background service waits until a message comes in, but I think the connection can not be keep up while a restart. After this there came up a new idea, where the background service pushes a notification, when the connection breaks.
Would this be possible (if yes, what would be the easiest way) or is there a better way to solve this?
Create an asynchronous task that tries to connect to the server in an endless loop. Use an time limit to cancel the process after a given time or return an ok value if the server responds an 200.
Related
i am currently trying to get code running as a background service.
what this code does is:
send request to server with current location of the user
receive response
parse response
save into model (singleton)
and this is set to happen in a 30 sec interval again and again.
now if my app stays in the background for too long, it will get disposed by the device and that code will not be executed anymore. what would be the right kind of background service for this usecase?
one of my main concernes is that i save my data in a singleton. but if my app is disposed this singleton will probably not exist anymore.
intent service doesnt make sense imho because it runs a one time tasks and has to be restarted from an app that might already be disposed at that point.
using the alarm manager would mean that i will have to save everything out of the app (sqllite for example) and then retrieve that data when the activity is started again which sounds rather complicated.
can someone please help me out here?
thanks in advance!
You sir needs the service of GCM
https://developers.google.com/cloud-messaging/
Thats exactly what you need for your desire ;)
But it's not less complicated as sticking to background services.
Also you can do a Hack: having two services watching your service to keep on running and itself...I swear when the User doesn't stop your app manually in the menu the System won't be able to stop them itself. Foolproof.
I've tried searching around and I can't find a good answer that makes sense.
Basically when the activity first starts it should connect to a server and continually listen for a message. When it gets it, it'll parse it and if it's a particular message, close the connection and then do some other stuff. I should also be able to close it if the user pushes a button.
I tried searching around and I honestly can't tell if I should use servlets or services or what (edit: or how to use them). I do know that it probably needs to be on it's own thread.
So what should I use?
Edit: I'm not sure how to use the servers/services/etc. to make it happen
An IntentService is what you're gonna want. It's the easiest to implement and it's perfect for network operations. It automatically handles running the service in its own thread. Once you establish a connection with the server, it should stay open until you respond with some message from the server.
Hey I need to implement a infinite loop for receiving commands through socket and updating status in my android app and want that thread to be running even if application is closed. So I need to know that what I'm thinking is optimal solution or not... I am planning to make a service which create a thread which will look for commands and this class is also Observable so the activity can get updates of status from it. Kindly suggest your way or if you think this is right solution. Thanks.
Running things in background is usually a bad idea(battery life), especially if using data services(data plan costs). What you need is indeed a service, but start your updating status thread only when you receive a user present broadcast and stop it when the device goes to sleep(I think it's enough just not to request a wake lock and not use startForeground()).
I need to make a batch of HTTP requests and feed the responses to a ListView one by one. I am using an async task and running a for loop of requests in a doBackgroundProcess method. Is that a correct approach? If not, please guide me to the best practise.
It's not entirely clear what you're trying to do. If you're doing the following:
Collect a set of HTTP requests
Send them.
Get back the results.
Post to an adapter backing a list view
Wait for the user to initiate the next set of HTTP requests
then I suggest you look into an IntentService. If your Activity goes into the background for any reason, AsyncTask will stop, but IntentService continues until it's done all its work.
I'd even suggest you stick your HTTP results in a content provider. It's best to persist data that takes a long time to retrieve. Your users will like you for it! You can also stop when you lose connectivity, and then re-start where you left off, if you have the data already. And if connectivity isn't available at the start, you can show users the most recent results.
Remember that the network isn't always available.
I have a school assignment about building a UDP based chat client on android. Now, my first idea was to make an activity that starts a service and the service handle all the networking and the activity all the input an output. But a have found a problem.
If i try to start the service so that the startCommand function starts and calls the socket.receive to start receiving network message, I can not bind the service so that i can pass input from the activity to the service (input = messages that the user want to send). It just hangs and a ANR exception is thrown. If i only bind it (so that receive is never called), it works like a Charm.
Maybe I'm not approaching this problem right. Or should it work to bind the service wen it's on a blocking receive call?
I did something like this once. I took the necessary data needed to track down the other device and fed it into an asyncTask where I kicked off a Runnable that basically kept a connection going on in the background.(This way you don't have to worry about binding) I found that instead of making your own socket connections its easier just to use Apache Mina or Netty. (No need to reinvent the wheel). If you just pass the data transmissions from the session running in the background to a List or a Queue and have an asyncTask check it and update the UI accordingly you should be all set for chatting. Maybe this isn't the best way, but it worked for me. Hope some of this helps.