Cosc 5/4730 Broadcast Receiver. Broadcast receiver A broadcast receiver (short receiver) – is an Android component which allows you to register for system.

Slides:



Advertisements
Similar presentations
Android Application Development Tutorial. Topics Lecture 6 Overview Programming Tutorial 3: Sending/Receiving SMS Messages.
Advertisements

Cosc 5/4730 Android Services. What is a service? From android developer web pages: Most confusion about the Service class actually revolves around what.
All About Android Introduction to Android 1. Creating a New App “These aren’t the droids we’re looking for.” Obi-wan Kenobi 1. Bring up Eclipse. 2. Click.
The Android Activity Lifecycle. Slide 2 Introduction Working with the Android logging system Rotation and multiple layouts Understanding the Android activity.
BroadcastReceiver.  Base class for components that receive and react to events  Events are represented as Intent objects  Intents received as parameter.
Cosc 5/4730 Game Design. A short game design primer. A game or animation is built on an animation loop. – Instance variables of “objects” are updated.
Cosc 5/4730 Android SMS. A note first Depending on the API level, an import changes because of a deprecated API 3 uses – import android.telephony.gsm.SmsManager;
System broadcasts and services. System broadcast events. EventDescription Intent.ACTION_BOOT_COMPLETEDBoot completed. Requires the android.permission.RECE.
Mobile Programming Pertemuan 6 Presented by Mulyono Poltek NSC Surabaya.
SMS. Short Message Service – Primarily text messages between mobile phones – First one sent December 3, 1982 “Merry Christmas” – In 2008 Approximately.
Getting Started with Android Development Rohit Ghatol.
Chien-Chung Shen Manifest and Activity Chien-Chung Shen
Monitoring battery power. overview Battery usage is critical on mobile apps Measuring energy usage is a bit tricky since many processes Android provides.
Developing Push Notifications (C2DM) for Android Vijai Co-Founder Adhish Technologies, Sweet’N’Spicy apps.
© Keren Kalif Intro to Android Development Written by Keren Kalif, Edited by Liron Blecher Contains slides from Google I/O presentation.
Software Architecture of Android Yaodong Bi, Ph.D. Department of Computing Sciences University of Scranton.
Mobile Computing Lecture#08 IntentFilters & BroadcastReceivers.
Google Cloud Messaging for Android (GCM) is a free service that helps developers send data from servers to their Android.
Broadcast intents.
Cosc 5/4730 Introduction: Threads, Android Activities, and MVC.
Integrating with Android Services. Introduction  Android has numerous built-in functionality that can be called from within your applications  SMS/MMS.
Using Intents to Broadcast Events Intents Can be used to broadcast messages anonymously Between components via the sendBroadcast method As a result Broadcast.
Android ICC Part II Inter-component communication.
DUE Hello World on the Android Platform.
CS378 - Mobile Computing Intents.
16 Services and Broadcast Receivers CSNB544 Mobile Application Development Thanks to Utexas Austin.
Overview of Android Application Development
Cosc 5/4730 Android App Widgets. App Widgets App Widgets are miniature application views that can be embedded in other applications (such as the Home.
CS378 - Mobile Computing Intents. Allow us to use applications and components that are part of Android System – start activities – start services – deliver.
Android - Broadcast Receivers
Understand applications and their components activity service broadcast receiver content provider intent AndroidManifest.xml.
Cosc 5/4730 Android Communications Intents, callbacks, and setters.
Cosc 5/4730 Android Maps v2 Blackberry Maps. Android.
Lecture 2: Android Concepts
Cosc 5/4735 Voice Actions Voice Interactions (API 23+)
Speech Service & client(Activity) 오지영.
Intents and Broadcast Receivers Dr. David Janzen Except as otherwise noted, the content of this presentation is licensed under the Creative Commons Attribution.
Mobile Software Development for Android - I397 IT COLLEGE, ANDRES KÄVER, WEB:
David Sutton SMS TELEPHONY IN ANDROID. OUTLINE  This week’s exercise, an SMS Pub Quiz  Simulating telephony on an emulator  Broadcast Intents and broadcast.
Messaging and Networking. Objectives Send SMS messages using the Messaging app Send SMS messages programmatically from within your app Receive and consume.
CS371m - Mobile Computing Intents 1. Allow us to use applications and components that are already part of Android System – start activities – start services.
Cosc 4735 Nougat API 24+ additions.
Google VR (gvr) CardBoard and DayDream With OpenGL
Android Application -Architecture.
Mobile Software Development for Android - I397
Lecture 2: Android Concepts
CS499 – Mobile Application Development
Broadcast receivers.
CS371m - Mobile Computing Services and Broadcast Receivers
Android Runtime – Dalvik VM
Messaging Unit-4.
Activities and Intents
Android Mobile Application Development
The Android Activity Lifecycle
Reactive Android Development
Broadcast Receivers A Android Component where you can register for system or application events, receive and react to broadcast intent. Each broadcast.
Mobile Software Development for Android - I397
Firebase Cloud messaging A primer
Android Programming Lecture 9
Developing Android Services
Android Notifications (Part 2) Plus Alarms and BroadcastReceivers
CIS 470 Mobile App Development
Activities and Intents
CIS 470 Mobile App Development
Android Developer Fundamentals V2 Lesson 5
SE4S701 Mobile Application Development
Lecture 2: Android Concepts
Android Development Tools
Mobile Programming Broadcast Receivers.
CIS 694/EEC 693 Android Sensor Programming
Presentation transcript:

Cosc 5/4730 Broadcast Receiver

Broadcast receiver A broadcast receiver (short receiver) – is an Android component which allows you to register for system or application events. – All registered receivers for an event are notified by the Android runtime once this event happens.

Uses. You app registers which events it wants to receive – Your receiver will then receive an intent when the “event” happens. Boot_completed is popular to start a background service on a reboot. » Note the app has to be launched once before this works, but it can be many reboots ago. Text messages custom messages like in the notifications lecture.

System Broadcasts EventDescription Intent.ACTION_BOOT_COMPLETED Boot completed. Requires the android.permission.RECEIVE_BOOT_COM PLETED permission. Intent.ACTION_POWER_CONNECTEDPower got connected to the device. Intent.ACTION_POWER_DISCONNECTEDPower got disconnected to the device. Intent.ACTION_BATTERY_LOW Triggered on low battery. Typically used to reduce activities in your app which consume power. Intent.ACTION_BATTERY_OKAYBattery status good again. Popular list, but this is not the complete list.

The Basics You can register a receiver in the (statically) AndroidManifest.xml or using (dynamically) the context.registerReceiver() method When an intent is received, then the onReceive() method is called. – A receiver then does it’s work and finishes. In API11+, you can call goAsync() and then the receiver can stay alive until PendingResult.finish() is called.

Declaration. import android.content.Context; import android.content.Intent; public class MyReceiver extends BroadcastReceiver public void onReceive(Context context, Intent intent) { //now do something with the intent. }

Registering a receiver Statically in the AndroidManifest.xml … This is the intent you want the receiver to receive

Registering a receiver (2) Normally in onResume() and onPause() If you forget to unregister, then you will get a “leaked broadcast receiver error” on exit. You can register for “local” events or system wide. – Local means only your application is going to send intents to it and very likely this custom intents.

Dynamic Local broadcast Example: Where mReceiver is a variable of type public void onResume() { super.onResume(); // Register mReceiver to receive messages. LocalBroadcastManager.getInstance(this).registerReceiver(mReceiver, new IntentFilter("CUSTOM_EVENT")); protected void onPause() { //or onDestory() // Unregister since the activity is not visible LocalBroadcastManager.getInstance(this).unregisterReceiver(mReceiver); super.onPause(); } A note, I could not get this to work in the examples, so I used system wide register.

Send a broadcast Easily to do. Intent i = new Intent("SOME_ACTION"); sendBroadcast(i); Remember, you can add more data/information to the intent as we have done in many places. NOTE: you can not send system broadcast, like boot_completed.

Dynamic system Wide broadcast Where mReceiver is a variable of type public void onResume() { super.onResume(); // Register mReceiver to receive messages. getBaseContext().registerReceiver(mReceiver, new IntentFilter(Intent.ACTION_BATTERY_LOW)); protected void onPause() { //or onDestory() getBaseContext()unregisterReceiver(mReceiver); super.onPause(); }

Now what? So now your application can respond to an event. – Like launch an activity – Start a service – Or just doing something quickly in the receiver. Remember an intent can contain data in the bundle – so an intent you create, send via a pendingIntent through say alarm or notification service. – Others system events maybe have information as well.

System Broadcasts. A common one is for a application to start a service when the devices finishes it boot. – IE Run on startup and continue to run. – These services are common for polling for notifications and sort of thing. Example: – SnapChat (and many apps), It running in the background as a service polling every so often their services for new “messages”. Setups a notification when there are new messages. Should be noted, this a drain on the battery.

Battery and Charging. You app may want to know when the battery state changes. – You can be notified when the battery state and charging state changes. Using a receiver and intents listed before. Note, for immediate info, see the following: – g-device-state/battery-monitoring.html g-device-state/battery-monitoring.html

Screen on/off There is a broadcast to any running applications for the Screen On or Off – Intent.ACTION_SCREEN_OFF and Intent.ACTION_SCREEN_ON – Your activity or service must be running Or the system will ignore your application. – This is an odd one because when the screen turns “off”, your activity likely just received an onPause() call too.

Demo code BroadCastDemo1 – Simple implementation of a receiver with a static and dynamic registered intent-filter BroadCastDemo2 – Setup to receive intents about battery status and power status. – Also uses android:launchMode="singleTop" so that only one activity is launched, when it is the foreground app. BroadcastNoti – A reimplementation of the notification demo, but using only receivers for the broadcast. BroadcastBoot – Receives a broadcast on boot, that starts a service. Note, you need to start the main activity once, and to see it really work, “reboot” the emulator.

References adcastReceiver/article.html adcastReceiver/article.html oid/content/BroadcastReceiver.html oid/content/BroadcastReceiver.html id_broadcast_receivers.htm id_broadcast_receivers.htm

Q A &