I want to implement the thread in my application
first requirement is:
Each activity access the local database that time it take some time to load.So I am planning to give progress-dialog.I want to do it in thread.Currently I did using the AsynsTask because of I dodnt know how long will take for record. Other than AsynsTask How we can implement using Thread?
Multitasking Facility :
I want to run two activity.One is in background.I.E If there are any upload available(Database Syn Android to SQL Server) while running activity ,Upload should start in background.How we can implement this?
Please guide me on this
Thanks in advance
You want to synchronize your database in background so I think you have to use service in which you have to implement thread and you have to write your code in thread.
You can then schedule your service startup time and also you can repeat your service when ever you want to keep duration for start service.
And for upload you have to options
1) Using AysncTask
2) Using Service with thread
And also know that Service is running in main UI so if you want to use service for synchronize database you have to implement thread
you can execute many AsyncTasks in the background, but only one Activity can be active at a single time.
AsyncTask handles the threading for you.
In your case, AysncTask has one benefit, that when multi-user (other word, multi connection) connect to database, AsyncTask will do in serial. (But you should notice, this might change through android version, for example: at Donut, they will do together, but in Honeycomb to now, serially)
Related
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 the following task:
I have to do a Wi-Fi scan and then send some RSSs to the a server to obtain a location from the server. After that I should display the location on a map.
I have one activity in my application. What is the best way to implement it? I haven't implemented a thread nor AsyncTask in android before. So I don't know a lot about it. I have read about threads and AsyncTask on android developer website. But I still can't understand everything.
I would appreciate it if anyone can tell me when its the best to use threads, and how AsynTask is different from threads and when to use AsyncTask instead of threads.
EDIT: the task that I have to implement should run only when the user click on actioBar item
.
If you can't understand the read documentation, I'd suggest starting with an AsyncTask, as it already implements a Thread when doInBackground() is fired and it already implements some mechanisms that you would probably have to implement by hand when using Threads (concurrency, Thread start/stop processes, sending information to UI thread, ...).
However, you don't specify what exactly want to do. If you plan to do a long-running process, instead of Threads or AsyncTasks, it's recommended using Service that would start a Thread as implementation, as contrary to popular beliefs, AsyncTasks are meant for short-lasting tasks.
In any case, if you're starting with this, I'd recommend using AsyncTask first, and when understood what it's doing and how, decide by yourself what's the best way to implement what you're planning to do: if running a Service with a Thread inside, a Thread itself or an AsyncTask.
it is logical question so I don't have code.
My question is:
How may I run background task that make Network work (waiting for messages from the server) and in the same time updates the UI when Message arrive!!
the Paradox "Android prevent access to UI from another nonUI thread", and "prevent network accesss from UI thread!!!"
Important: I want to run my method all the time that the application run, and scan network buffers and when I get message I want to update the UI and messages List...
That sounds like a perfect description for a Service. You can use IntentService or build your own Service (be careful, the last one doesn't start in a new thread by default).
There are also many examples out there how to update the UI from a Service (i.e. using Notifications or Broadcast).
Android provides built in class to perform network operation. For this purpose you can use AsyncTask class.
AsyncTask enables proper and easy use of the UI thread. This class
allows to perform background operations and publish results on the UI
thread without having to manipulate threads and/or handlers.
You can get details from here Android AsyncTask
But if you want to check message continuously then Service is the best solution as #AlexS explained it perfectly.
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 want to do the replication between sql server & android sqlite.I have done downloading(sql server to Android) using WCF service.Uploading part also that way I can do it.
My Problem is: After user change/enter table contents (e.g enter new invoice), it should start the upload(sqlite to sql sever) backed without effecting my current work/activity.
How we can implement this?
If it is mutithreading how it is possible.Need to run two activity same time.One will run foreground other one run in background....
Please help me or give me idea reading this?
Thanks in advance?
The recommended design practice for this is to use a content provider which handles concurrent thread access to the database.
To handle the background service actions, just implement an IntentService and simply call startService(intent);
Activity will be used for foreground tasks. Using that User can interact to your application. In Android we have a Mechanism to do long running task in background using Services. But it doesn't mean you should write your code to upload the data on server in Service only. Give service a task to just invoke a background thread and tell that , Upload data to server. Using AsyncTask you can achieve this.
So the flow should be. Activity --> Service --> AsyncTast --> To Your Server.