Russ Wheeler
Russ Wheeler

Reputation: 2630

Reading email at regular intervals

What I am trying to do is write an app that logs into my email account and checks both Inbox and Sent items, at a regular period set by the user.

I have a few queries so I hope this isn't ignored due to it's length.

  1. I think I need a BroadcastReceiver to start the AlarmManager after the phone has finished booting.
  2. I need to do an AsyncTask to read the emails off the main UI thread. However, I read Services also run on separate threads?
  3. From the PendingIntent in the AlarmManager (why is this PendingIntent and not normal Intent?) Should I choose getBroadcast() or getService()?
  4. My understanding of the whole process is:
    • phone finishes booting, fires broadcast
    • BroadcastReceiver gets event and sets AlarmManager
    • AlarmManager fires Intent at specific time
    • BroadcastReceiver gets event
    • BroadcastReceiver starts Service
    • Service starts AsyncTask

Is that right? It seems very long-winded. If I chose getService() instead of getBroadcast() as the PendingIntent could I skip calling the 2nd broadcast in the steps above, and go straight to the Service?

Re-reading this I guess I haven't asked specific questions, but I'm more interested to know if my understanding is correct or if I'm totally lost (I feel like I am!)

UPDATE: I think I'll use an intentservice they sound good as I can call them directly from the alarmmanager. However, reading around it appears that a service may not stay awake long enough so I may have to use a broadcast receiver anyway in order to use a wake lock, something I am now investigating further.

Commonsware has written a useful wrapper called WakefulIntentService which I think I'll use to help do my own wake lock stuff

Upvotes: 0

Views: 183

Answers (2)

user1521536
user1521536

Reputation:

You can use AlarmManager to start a Service (getService()). And a note that Service runs on UI thread. So you cannot make network connections in a Service (to avoid of NetworkOnMainThreadException). You need something like Thread in your Service, don't use AsyncTask, as the document says:

AsyncTasks should ideally be used for short operations (a few seconds at the most.)

Another choice is IntentService, it runs on a separate worker thread, you can do network jobs in there. Note that: 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.

Upvotes: 2

Emil Adz
Emil Adz

Reputation: 41129

I dont have all the answers for you but I will tell you what I know:

3.PendingIntent is a waiting Intent and the difference is that this Intent intended to be sent in the future and not immediately as a regular intent would.

service starts AsyncTask

Service doesn't need to start AsyncTask, you do one of those either you run a service in the background that will constantly run on a different thread. or you could use an AsyncTask that will create a thread, perform it's job and kill the thread at the end.

Upvotes: 0

Related Questions