Im developing for an android aplication and I need to run a thread in the background that is going to be check the current gps and show an activity depending of the current location data but the application have diferents activities, and I need this thread running all the time no matter if I change between an activity and other.
Can I do that ?
I know that I can add threads to an activity but I dont know how have a main thread in the background
You need to use a Service which is not tied to any particular Activity. You can read all about Services here
In your case I ssuggest you take a look at IntentService which automatically generates a background thread for you
You want to use a service here:
http://developer.android.com/reference/android/app/Service.html
You can run things in a background thread if you want in the service. And then you can bind to it within your activites to communicate between the service and activities.
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 run my asynctask class of activity, then finish the activity, and asynctask still works. Android after some time killing my asynctask. I tried to add to the function run "Process.setThreadPriority (Process.THREAD_PRIORITY_FOREGROUND);" or "Process.setThreadPriority (Process.THREAD_PRIORITY_BACKGROUND);" but asynctask is still killed.
Yes I know they are Services, but I want the task was completed after the work done, and do not work all the time in the background as Services.
You should use IntentService. This is different from a regular Service. IntentService will result in the service closes it self when it is done. IntentService is not running on the UI thread so you don't need to "handle" it as you need to do with a service..
You start it, it does some work and when it finishes it closes itself, exactly what you need.
There is no way for you to insure that the Asynctask will finish, since it's not build for that purpose.
Also, a service is not "running" as in taking cpu when it has nothing to do even if it's open.
I'm trying to implement multiple tasks in my app that are required to continue running even when the app is paused (when the app loses focus but the user has not completely closed the app).
The first task consists of establishing a connection to a server and receiving updates.
The second task consists of communicating back and forth with a paired Bluetooth device.
Both tasks will not only be ran when the app is paused, they will be required to be run when the app is in focus. When the app is in focus they will be required to update the GUI based on the information they receive. Also both tasks task doesn't really have a set finishing condition except for when the user decides to stop the tasks themselves or when the app is completely closed thus they must be able to be cancelled in mid process. These two tasks will very likely be running for a long time.
In summary:
Two background tasks that can be run when app is not in focus.
Both tasks are required to be able to update the GUI when the app is in focus.
Both tasks must be able to be terminated when the user decides to stop the task inside the app or when the app is completely closed.
I believe that I need to using either a Service, IntentService or AsyncTask.
From my reading I believe that I should be using an AsyncTask but I want to be sure before starting.
Service: Can't manipulate GUI.
IntentService: Can't run tasks in parallel, they will be queued on the same thread.
AsyncTask: Seems fine!
Is this correct or should I be using a combination of things somehow?
You misunderstand the point of Service and IntentService. They are not a concurrency mechanism. They will not run automatically in the background. They run on the same thread as your app. If you need concurrency, you need either AsyncTask or Thread. Please note that if you use AsyncTask to get true concurrency between multiple tasks you still need to use executeOnExecutor().
What Services do is persist even when your Activity is gone, or when it isn't in the foreground anymore (either another activity in your app is, or another app is). So if you need your secondary thread to be running after the Activity is finished, you should have it owned in a Service. If you don't, owning it in the Activity is fine.
An IntentService is basically a Service that does more or less fixed length jobs 1 at a time. There's uses for them, but if they need to do network IO or anything like that they still have to spin off AsyncTasks, because they still are run on the main thread.
You should use a service, the purpose of a service is to have a much lesser restricted lifecycle that can run while the app isn't in the foreground. You can use a BroadcastReceiver or callback/AIDL to send messages back to the calling activity that will manipulate the ui.
I'm struggling to decide which class to use, Activity or Service.
My application is complicated so I will use another example. A timer.
I would like to create an application that start to count the seconds when I open the application and keep counting even when I click the back button.
When I back to the application it will have a gui that show me the time.
So the gui I guess would be the activity and the counting method would be on the service.. but how do I manage that system?how they will be communicating? how can I do that?
You can use BroadcastReceivers and send broadcasts that your activity will be listening to, or you can use Messenger to send messages from the service to the activity
they can communicate with intents, or via shared storage, or content providers, ...
Basically I'm trying to make a little app for watching offline content. So there's a moment where the user selects to download the contents (and the app should download about 300 small files and images).
I'd like to show the user how does the process go if he enters the proper activity. Showing a list of all the files, telling what has been already downloaded, in progress or waiting for download.
My problem is that I really don't know what approach to take for achieve this. Since the download should last until finished I imagine the solution is an Service, but whats best? an IntentService, a Bound Service or an Standard Service calling a startService() for each download? And how can I keep my objects updated for displaying them later? should I use a database or objects in memory?
Thanks
I would suggest using AsyncTask class, it allows you to easily move time consuming code(like downloading files) to a different thread. This will keep your app responsive, while giving you the ability to update your UI in the process.
It's hard to be more specific without having more details about how exactly you want your app to behave. Are the downloads only going to happen when the app is running or in the background as well?
You could use Asynctask or implement a ExecutorService with custom policies and send to it the download threads.
You need to keep a reference to the AsyncTask or a Future object respectively inside of a collection if you want to give the oportunity to the user to stop downloads.
Of course, you need to call startService each time you want to download a new file.
Service onCreate only is called if service is not running and onStartCommand run each time you call startService. In onStartCommand you run a new thread for download a new file.
You can bind service with an activity and each time that your downloadsActivity is created you show the state of downloads implementing a custom Adapter. Service only finishes when you call activity.stopService or service.stopSelf