I want to send gps cordinates to a web service whenever the gps position on the phone gets updated.
Should i create a class that extends AsyncTask that sends the data to the webservice from the onLocationChanged event? Or do you guys suggest something else?
I started coding apps yesterday, so please tell me if im on the right track. Thanks
Yes, I think this would be the right approach. Extend AsyncTask and in doInBackground() make the POST/PUT to your web server.
Another approach may be to use a SocketConnection, but this will still have to be operated in a background thread, and may add overhead, to thread Socket disconnection etc.
Just be sure to unregister your BroadcastReceiver in onPause and also handle your AsyncTasks to get cancelled when a new one will be executed, or the Activity stopped... etc, depending on your application needs.
If you would also like to send the location while your app is in background you should consider implementing a Service.
I recommend the IntentService mechanism.
This has a lot of uses and is often overlooked by many Android developers.
Related
I have created an accessibility service for my app where it would detect of the current outgoing call is answered or not but now i want create a callback function to my activity when the call is answered.
I have found some code to make callbacks from service in below links -
Callback from Service
RESTFul API
but i cant figure out a way to do that with my accessibility service, as i am not starting the service, android is, so there is no way to pass an object to the service.
If anyone can help please share.
Your service and your activity are separate processes. They cannot communicate directly. You should look into BroadcastReceiver. Note, though, that the user would have had to start your activity and have it running in the background. If the system decides it is low on memory and kills your activity completely, there is nothing you can do. You will not reliably get this broadcast with your activity in the background. You could try starting a background process from your activity for the sake of finding this event and storing the results somewhere for future consumption. Depends of course on your use case.
I have an intentService in my app that I use to play music files in the background.
In this service, I have several methods (play, pause, stop, etc.) that I need to call from my main fragment in response to UI input.
However I cant seem to find any way of sending messages to an intentservice, I can only find ways to sending messages from an intentService to an activity.
If someone knows a way I can achieve this, I would much appreciate it.
I'm quite new to android. I'm building an application that uses the android smack library to communicate with my XMPP server. I am able to send and receive messages perfectly however what I want is to run this ENTIRE sending and receiving messages code to a long term running process in the background and on receiving a certain message I want to launch a screen (something similar to the incoming call screen of android) no matter what the user is doing on his smartphone i.e. when the activity running the messaging service is not open (in a similar way when you get an incoming call from a viber contact and you get faced with the screen even though you obviously dont have the viber application open).
Any ideas on how exactly to do this ?
I have a few ideas in mind after research but dont quite seem to get the correct way and glue all peaces together.
Thanks in advance
you can use intentService to communicate with mainThread
here is a basic examples of intentservice and service
http://mobile.tutsplus.com/tutorials/android/android-fundamentals-intentservice-basics/
service example
http://www.vogella.com/articles/AndroidServices/article.html
here is an official document
http://developer.android.com/reference/android/app/IntentService.html
i made an application which keep the screen awake, and it works fine until it gets killed by the system.
How can i keep my app running in background?
I see a lot of apps that uses notifications to stay running and avoid being killed, but i don't know how to implement it.
Can someone help me achieve this?
You can use the Service API for this, specifically the startForeground(int, Notification) function call. There is an example provided in the API documentation that I have linked to.
Services or AlarmManager is what you are looking for I think, depends on what you want to do..
Use Android Services.You can use the following methods:-
To call a Service which doesnt return any value back use - startService()
To call a Service which return back values use - bindService()
Its good to be cautious when using any Sensors to run in the background as it might eat up you battery fast
I want to make my app run in the background like a process that runs always.
I need it because I want to get locations update for GPS every 2 minutes (longitude, latitude) and to use the information in a method.
For that I need for the app to be running when the phone is asleep or not in the UI of the app in other words I need the app will run always.
I'm sure that there is a way to make it , thanks anyway for any answers :)
This was just the first google search result I found:
http://www.vogella.com/articles/AndroidServices/article.html
The answer here is to use a service, if this tutorial is lacking there are 6.4 billion others.
We have something like this, but it is made up of several parts.
Firstly you will want your code to run (and be registered in the manifest) as a Service
You will probably also want to request android.permission.RECEIVE_BOOT_COMPLETED so that you can write and register a BroadcastReceiver that gets notified by android.intent.action.BOOT_COMPLETED action and its onReceive method kicks off the service.
In our case we also have a front-end activity which also pokes the service to make sure it is running, but it's been a while snce I checked to see if this was still required.
Our service is nearly empty and onCreate immediately calls a custom Handler which then manages the 'ticks' which wakes the Handler and fires a Runnable if there is work to do, but this is where my code diverges from yours. In our case we only attempt to update the GPS location when the service 'ticks' (usually every minute) and there is work to do. It usually only performs a couple of dozen operations per client per day so I can't really advise on how it will impact battery usage.