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()).
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'm implementing a payment terminal in a POS (point of sale) application for Android. I have gotten an SDK for communicating with the payment terminal through Bluetooth.
Application startup: Initialize the payment terminal, check connection.
Then i need to keep it alive througout the application lifecycle, to be able to check connection status, and start/cancel transaction.
The main issue here is, what is a good/best way to implement this.
I cannot have it in the main thread as i have experienced ANR when doing that.
I was thinking that i maybe should create a service that runs in another process.
I found this table which gave me some indication of what i can use, but i'm not sure anyway. The closest thing to use here was the IntentService, but i'm afraid that since i cannot run tasks in parallell with that, i won't be able to cancel a transaction while it's ongoing.
Difference between Android Service,Thread,IntentService and AsyncTask
Would a solution be a regular service which starts a thread when started?
A side note is that i need to be able to use the content provider to save transactional data as well.
What do you suggest?
Thanks in advance
Use a normal Service that owns a thread. Send jobs to that thread to be executed in parallel on it. Its a fairly common implementation pattern.
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.
is there any way to listen a Server action using a service?i want to check server message for life time and do actions based on server message,please post a sample code
I think what you looking for is a long time running background service, check out this SO question.
Here i have example of using service, but it's doing some other thing. You can work it out..
http://maephv.blogspot.com/2011/08/android-notification-in-your.html
But remember, that when screen comes off, services freeze. I don't know yet how to fix it, but maybe "background process" is word to google
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
I need a good way to get data from a thread to another activity.
I have one gaol: how to develop an Android app that plots bluetooth data forever in real-time.
I inherited a background thread which updates the screen with new data it receives over a bluetooth connection. When I launch my Plot activity I can see a bluetooth background thread continue to write bluetooth data to Logcat forever and ever. So I know for a fact the bluetooth background thread is still running when I launch my Plot activity.
I have succeeded as follows: since this bluetooth background thread seems to run fovever, I decided to use its update() method to call my static Plot.plotData() method to plot the bluetooth data. And this works. It will will run endlessly with out a problem - receiving bluetooth data and plotting it via periodic calls from the bluetooth background method update() to my static Plot.plotData() method.
The latest feedback I have received "It sounds like you're looking for an in-memory way to share data, and that's simply not the way the Android activity model works." In-memory???But this is rediculous. An Android phone has a limited amount of RAM for running Activities (one at a time), threads, handlers, Services, AnycTasks, etc. And an SD card for persisting data.
One critical person basically said: "In order to share data from a bluetooth background thread to my Plot activity (Plot.plotData()) that I must use the SD card." ??? This just sounds nuts, because I have it working using my static method Plot.plotData().
Frankly I don't see anything wrong with my solution primarily because those who criticize it do not follow up with a definitive alternative.
If you find my solution deficient please speak up and provide a definitive solution." Unless I use a new thread, handler, Service, AsyncTask, etc, everyone assumes my solution is not a good one. Why? Supposing the criticism is valid, PRECIOUSLY What should I do instead of what I am currently doing?
One more time: if you have background task running you should create Service. That's how Android expects applications to behave. Otherwise your thread may be terminated at any time (assuming none of the Activities are active).
From documentation:
A service is a component that runs in the background to perform long-running operations or to perform work for remote processes
This precisely describes your case.
Also carefully read Processes and Threads, specifically Process Lifecycle section.
Basically your application falls into Background Process or Empty Process group most of the time. That's why you need Service started in your application.