I am new to android. I what to know the difference between Intent and BroadcastReceiver. I am more confused with BroadcastReceiver than Intent.
Please help me out. Simple code will be helpful.
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Ok, I will explain it with an example.
Let’s suppose I want to create an app to check subway status from it’s webpage. I also want a system notification if the subway is not working ok.
I will have:
Activityto show results.Serviceto check if the subway is working and show a notification if it’s not working.Broadcast ReceivercalledAlarm Receiverto call the service every 15 minutes.Let me show you some code:
Explanation:
As you can see you can set an alarm. and when the alarm is received we use an
intentto start theservice. Basically theintentis a msg which can have actions, an serialized stuff.Explanation:
The
servicestarts and:AsyncTaskwhich takes care of updating an notifying theActivityIt doesn’t make sense to paste the
AsyncTaskbut when it finished it calls:This creates a new
Intentwith a certainNEW_SUBWAYS_STATUSaction, put inside the intent the subways andsendBroadcast. If someone is interested in getting that info, it will have a receiver.I hope I made myself clear.
PS: Some days ago someone explained broadcast and intents in a very cool way.
Someone wants to share his beer, so he sends a broadcast
with an intent having action:”FREE_BEER” and with an extra: “A glass of beer”.