CS499 – Mobile Application Development

Slides:



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

Services. Application component No user interface Two main uses Performing background processing Supporting remote method execution.
The Activity Class 1.  One application component type  Provides a visual interface for a single screen  Typically supports one thing a user can do,
BroadcastReceiver.  Base class for components that receive and react to events  Events are represented as Intent objects  Intents received as parameter.
Permissions.  Applications can protect resources & data with permissions  Applications statically declare permissions  Required of components interacting.
Intents and Intent Filters.  Intents are data structures that specify  Operations to be performed  Events that have occurred  Broadcast by one component.
Application Fundamentals. See: developer.android.com/guide/developing/building/index.html.
1 Working with the Android Services Nilanjan Banerjee Mobile Systems Programming University of Arkansas Fayetteville, AR
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.
Chien-Chung Shen Manifest and Activity Chien-Chung Shen
박 종 혁 컴퓨터 보안 및 운영체제 연구실 MobiSys '11 Proceedings of the 9th international conference on Mobile systems, applications,
 Understanding an activity  Starting an activity  Passing information between activities  Understanding intents  Understanding the activity lifecycle.
© 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.
Chapter 2: Simplify! The Android User Interface
Google Cloud Messaging for Android (GCM) is a free service that helps developers send data from servers to their Android.
Broadcast Receiver Android Club Agenda Broadcast Receiver Widget.
Android Activities 1. What are Android Activities? Activities are like windows in an Android application An application can have any number of activities.
P2P communication Using the GTalk Service API. Introduction Peer-to-Peer communication highly used in mobile devices. Very efficient to when certain factors.
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.
Cosc 5/4730 Broadcast Receiver. Broadcast receiver A broadcast receiver (short receiver) – is an Android component which allows you to register for system.
Android ICC Part II Inter-component communication.
COMP 365 Android Development.  Perform operations in the background  Services do not have a user interface (UI)  Can run without appearing on screen.
Mobile Programming Lecture 6
CS378 - Mobile Computing Intents.
로봇 모니터링 2/2 UNIT 21 로봇 SW 콘텐츠 교육원 조용수. 학습 목표 Broadcasting Service 2.
16 Services and Broadcast Receivers CSNB544 Mobile Application Development Thanks to Utexas Austin.
1 Announcements Homework #2 due Feb 7 at 1:30pm Submit the entire Eclipse project in Blackboard Please fill out the when2meets when your Project Manager.
1 Mobile Software Development Framework: Android 2/28/2011 Y. Richard Yang.
Copyright© Jeffrey Jongko, Ateneo de Manila University Of Activities, Intents and Applications.
Chapter 2 The Android User Interface. Objectives  In this chapter, you learn to:  Develop a user interface using the TextView, ImageView, and Button.
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
COMP 365 Android Development.  Every android application has a manifest file called AndroidManifest.xml  Found in the Project folder  Contains critical.
Android Boot Camp for Developers Using Java, Comprehensive: A Guide to Creating Your First Android Apps Chapter 2: Simplify! The Android User Interface.
Mobile Programming Midterm Review
Applications with Multiple Activities. Most applications will have more than one activity. The main activity is started when the application is started.
Introducing Intents Intents Bind application components and navigate between them Transform device into collection of interconnected systems Creating a.
Android and s Ken Nguyen Clayton state University 2012.
User Interface Layout Interaction. EventsEvent Handlers/Listeners Interacting with a user.
Lecture 2: Android Concepts
Android Alert Dialog. Alert Dialog Place Button to open the dialog. public class MainActivity extends ActionBarActivity { private static Button button_sbm;
Speech Service & client(Activity) 오지영.
Mobile Programming Lecture 4 Resources, Selection, Activities, Intents.
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.
Lab7 – Appendix.
Intents and Broadcast Receivers
Mobile Software Development for Android - I397
Lecture 2: Android Concepts
Android Application Development 1 6 May 2018
Broadcast receivers.
CS371m - Mobile Computing Services and Broadcast Receivers
Android – Event Handling
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
Android Programming Lecture 9
Android Notifications (Part 2) Plus Alarms and BroadcastReceivers
Android Developer Fundamentals V2 Lesson 5
Lecture 2: Android Concepts
Activities, Fragments, and Intents
Mobile Programming Broadcast Receivers.
Presentation transcript:

CS499 – Mobile Application Development Fall 2013 BroadcastReceivers

Broadcast Receiver Base class for components that receive and react to events Events are represented as intents Events are broadcast system-wide Interested BroadcastReceivers receive intent and act on the intent BroadcastReceivers have no user interface

How it works Broadcast receivers get registered to receive specific intents Statically via AndroidManifest.xml, or Dynamically via Context.registerReceiver() Some component broadcasts an intent Android identifies appropriate (i.e. registered) receivers & delivers event by calling BroadcastReceiver.onReceive() Event handled in onReceive()

Static Registration Include <receiver> in AndroidManifest.xml <application> <receiver receiver_specs> <intent-filter> event_specs </intent-filter> </receiver> </application> Receiver registered at boot time or when application package is added at runtime

manifest example <application …> <activity android:name=".SimpleBroadcast” …> … </activity> <receiver android:name=".Receiver2"> <intent‐filter android:priority=”5"> <action android:name= "course.examples.BroadcastReceiver.intent.action.TEST2”> </action> </intent‐filter> </receiver> </application> <uses--‐permission android:name="android.permission.VIBRATE"> </uses--‐permission>

Dynamic Registration Create an IntentFilter Create a BroadcastReceiver Register BroadcastReceiver to receive Intents that match the IntentFilter using Context.registerReceiver() As appropriate call Context.unRegisterReceiver() to unregister

Dynamic Registration code public class SingleBroadcast extends Activity { public static final String CUSTOM_INTENT = "course.examples.BroadcastReceiver.intent.action.TEST1"; public void onCreate(Bundle savedInstanceState) { … registerReceiver(new Receiver1(), new IntentFilter(CUSTOM_INTENT)); }

Event Broadcast Several broadcast methods supported Normal vs. Ordered Normal: processing order undefined Ordered: sequential processing in priority order Sticky vs. Non-Sticky Sticky: store intent after initial broadcast Non-sticky: discard intent after broadcast With or without receiver permissions

Normal Broadcasts //public abstract class Context … // send Intent to interested BroadcastReceivers void sendBroadcast (Intent intent) // send Intent to interested BroadcastRecievers // if they have the specified permissions void sendBroadcast(Intent intent, String receiverPermission)

public class SimpleBroadcast extends Activity { public static final string CUSTOM_INTENT = "course.examples.BroadcastReceiver.intent.action.TEST2"; public void onCreate(Bundle savedInstanceState) { … Button button = (Button) findViewById(R.id.button); button.setOnClickListener(new OnClickListener() { public void onClick(View v) { sendBroadcast(new Intent(CUSTOM_INTENT), android.Manifest.permission.VIBRATE); } });

Ordered Broadcasts //public abstract class Context … //send Intent to interested BroadcastReceivers in priority order void sendOrderedBroadcast(Intent intent, String receiverPermission) // send intent to interested BroadcastReceivers in priority order // sender can provide various parameters for greater control void sendOrderedBroadcast(Intent intent, String receiverPermission, BroadcastReceiver resultReceiver, Handler scheduler, int initialCode, String initialData, Bundle initialExtras)

public class CompoundOrderedBroadcast extends Activity { public static final String CUSTOM_INTENT = "course.examples.BroadcastReceiver.intent.action.TEST4"; final Receiver1 br1 = new Receiver1(); final IntentFilter intfil = new IntentFilter(CUSTOM_INTENT); @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); Button button = (Button) findViewById(R.id.button); button.setOnClickListener(new OnClickListener() { public void onClick(View v) { sendOrderedBroadcast(new Intent(CUSTOM_INTENT),android.Manifest.permission.VIBRATE); } });

Sticky Broadcasts Sticky Intents are cached by Android New Intents overwrite older intents they match When BroadcastReceivers are dynamically registered Cached sticky Intents matching the specified IntentFilter are broadcast to the BroadcastReceiver One matching sticky Intent is returned to the caller

Sticky Broadcasts //public abstract class Context … // send sticky Intent to interested BroadcastReceivers void sendStickyBroadcast(Intent intent) //send sticky Intent to interested BroadcastReceivers in priority order // sender can provide various parameters for greater control void sendStickyOrderedBroadcast (Intent intent, BroadcastReceiver resultReceiver, Handler scheduler, int initialCode, String initialData, Bundle initialExtras) Broadcaster must have BROADCAST_STICKY permission to send sticky Intents

Intent Filter Resolution Similar to resolution for Activities & Services Some debugging tips Log BroadcastReceivers that match an intent Intent.setFlag(FLAG_DEBUG_LOG_RESOLUTION) List BroadcastReceivers registered to receive intents Dynamic registration % adb shell dumpsys activityb Static registration % adb shell dumpsys package

Event Delivery Events delivered by calling onReceive() and passing Intent as a parameter onReceive() should be short-lived If event handling lengthy, consider starting a service, rather than performing a complete operation in onRecieve() BroadcastReceivers can’t start async operations (e.g., showing a dialog, binding to a service, starting activity via startActivityForResult)

Handling a normal broadcast public class Receiver1 extends BroadcastReceiver { public void onReceive(Context context, Intent intent) { System.out.println(this + ":GOT THE INTENT"); // emulator doesn't support vibration Vibrator v = (Vibrator) context.getSystemService( Context.VIBRATOR_SERVICE); v.vibrate(500); }