Reinherd
Reinherd

Reputation: 5506

Why should I use a service in this case?

I'm following this basic tutorial: http://karanbalkar.com/2013/07/tutorial-41-using-alarmmanager-and-broadcastreceiver-in-android/

As you can see, it basically does the following (correct me if i'm wrong, please):

First stop.: MyReceiver is extending BroadcastReceiver. Is it possible to start an Intent to a normal Activity?

Second stop.: Same as before: BroadcastReceiver, can only launch Intents which are Services? Or can it launch normal activities? Why do I even have to call any new intent? Why can't I just do the job in BroadcastReceiver (like downloading some content over internet)?

I'm a bit new about Services, so I'm sorry if I said something extremly weird.

Thank you so much.

Upvotes: 0

Views: 64

Answers (3)

Vipul
Vipul

Reputation: 28093

Why can't I just do the job in BroadcastReceiver (like downloading some content over internet)?

As per Android Documentation Broadcast receivers onReceive method is called on Main Thread so you can not perform downloading task in onReceive.Since it will freeze the UI and may throw exception in 3.0 and above version.

If you want to perform download,then the best bet would be to trigger IntentService inside onReceive ( IntentService by default runs on background thread.)

Upvotes: 1

tyczj
tyczj

Reputation: 73741

BroadcastReceivers can launch anything you want (Activities, Services) with an intent.

The reason you dont want to stuff in a BroadcastReceiver is because they only live so long where as a service runs until you tell it to stop.

If you are doing polling or something you really dont need a BroadcastReceiver and you can just use a service (IntentService specifically) with your alarm manager. An IntentService only runs a long as it has something to do meaning it will stop itself unlike a normal service where you have to stop it when its done.

Upvotes: 1

Alexander
Alexander

Reputation: 48252

BroadcastReceiver is just a means to do anything you want upon receiving a broadcast.

A Service is something that won't easily be killed by the Android OS unlike an Activity. Service has no GUI though.

Upvotes: 1

Related Questions