New Posts  All Forums:Forum Nav:

Android asynctask help

post #1 of 2
Thread Starter 
It seems asynctask, and threading is required for android apps for 4.0+ I mean the stuff I would like to do. I am still fuzzy on how to use it.

I used it last night, I had it execute small task (well big enough task to where it could not be used on the UI thread) but it did not work.

any books that explain very well on asynctask or threading?
Collecting Dust
(13 items)
 
Server
(9 items)
 
 
CPUMotherboardGraphicsRAM
E8400 3.0ghz OC @ 3.8GHz @ 1.36v DFI BLood Iron P35 none OCZ 2x 2gb DDR2 800 CAS @ 5-6-6-21 
Hard DriveOptical DriveOSMonitor
none none loonix ssh is my monitor 
KeyboardPowerCaseMouse
none Corsair 650 watts old pc case Logitech mx518 
Mouse Pad
none! 
CPUMotherboardGraphicsRAM
AMD Athlon 64 X2 Dual Core Processor 6000+ GX611AA-ABA m8300f onboard 1x 2GB OCZ RAM DDR 
Hard DriveOSMonitorPower
320gb + 1TB HDD xubuntu 14.04 SSH 350 watt 
Case
crappy HP case 
  hide details  
Reply
Collecting Dust
(13 items)
 
Server
(9 items)
 
 
CPUMotherboardGraphicsRAM
E8400 3.0ghz OC @ 3.8GHz @ 1.36v DFI BLood Iron P35 none OCZ 2x 2gb DDR2 800 CAS @ 5-6-6-21 
Hard DriveOptical DriveOSMonitor
none none loonix ssh is my monitor 
KeyboardPowerCaseMouse
none Corsair 650 watts old pc case Logitech mx518 
Mouse Pad
none! 
CPUMotherboardGraphicsRAM
AMD Athlon 64 X2 Dual Core Processor 6000+ GX611AA-ABA m8300f onboard 1x 2GB OCZ RAM DDR 
Hard DriveOSMonitorPower
320gb + 1TB HDD xubuntu 14.04 SSH 350 watt 
Case
crappy HP case 
  hide details  
Reply
post #2 of 2
I don't know of any books. I learned all I needed just from the SDK documentation, and various blogs with tutorials and how-to's, was more than enough to get me started.

As you've already noticed, certain things are not allowed to be performed on the mainactivity thread. Mainly anything that is likely to have a negative performance impact is not allowed. That's google's way of enforcing good app development that keeps a smooth responsive ui.
For me, I had to figure out how to use threading because I was making some web service calls.

Android actually has a number of different ways of threading. Some are very different, and some are just subtlety different.
You can use asyncTask and Threading, but I found them to be a little frustrating.

Two things I used was the alarmmanager. This is not what it sounds like, it does make sounds and vibrate the phone when it runs. You can trigger any action to happen at a predetermined time, and on a re-occurring basis. This has the added benefit that it once it has been scheduled, it will still run even if your app has been closed. So as an example, this is a good way to check for updates somewhere and generate a system notification.

The other tool you have available is Services and IntentService. This is a quick easy way to run an extra thread if it is just going to be running temporary/minor tasks.

IntentService:
Quote:
This "work queue processor" pattern is commonly used to offload tasks from an application's main thread. The IntentService class exists to simplify this pattern and take care of the mechanics. To use it, extend IntentService and implement onHandleIntent(Intent). IntentService will receive the Intents, launch a worker thread, and stop the service as appropriate.

All requests are handled on a single worker thread -- they may take as long as necessary (and will not block the application's main loop), but only one request will be processed at a time.

Here's a good page that talks all about that:
http://www.vogella.com/articles/AndroidServices/article.html
Edited by wedge - 3/16/13 at 9:50am
Death Star
(21 items)
 
Darksaber
(11 items)
 
 
CPUMotherboardGraphicsRAM
Athlon II x2 245 Asus M3A78 Radeon HD6570 1GB Mushkin Silverline 2GB DDR2  
Hard DriveOptical DriveOSMonitor
OCZ Vertex 2 120GB Samsung Blu-Ray Windows 7 Samsung 46" DLP 
PowerCaseOther
Silverstone Strider Essentials 400W Silverstone Milo ML03B Hauppage WinTV 1250 
  hide details  
Reply
Death Star
(21 items)
 
Darksaber
(11 items)
 
 
CPUMotherboardGraphicsRAM
Athlon II x2 245 Asus M3A78 Radeon HD6570 1GB Mushkin Silverline 2GB DDR2  
Hard DriveOptical DriveOSMonitor
OCZ Vertex 2 120GB Samsung Blu-Ray Windows 7 Samsung 46" DLP 
PowerCaseOther
Silverstone Strider Essentials 400W Silverstone Milo ML03B Hauppage WinTV 1250 
  hide details  
Reply
New Posts  All Forums:Forum Nav:
  Return Home
  Back to Forum: Application Programming