Mobile Computing Lecture#08 IntentFilters & BroadcastReceivers.

Slides:



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

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.
BroadcastReceiver.  Base class for components that receive and react to events  Events are represented as Intent objects  Intents received as parameter.
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.
@2011 Mihail L. Sichitiu1 Android Introduction Hello World.
Chien-Chung Shen Manifest and Activity Chien-Chung Shen
Introduction to Android Programming Content Basic environmental structure Building a simple app Debugging.
© Keren Kalif Intro to Android Development Written by Keren Kalif, Edited by Liron Blecher Contains slides from Google I/O presentation.
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.
Cosc 5/4730 Introduction: Threads, Android Activities, and MVC.
Mobile Programming Lecture 17 Creating Homescreen Widgets.
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.
Mobile Programming Lecture 6
CS378 - Mobile Computing Intents.
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.
Chapter 2 The Android User Interface. Objectives  In this chapter, you learn to:  Develop a user interface using the TextView, ImageView, and Button.
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
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 Lecture 5 Composite Views, Activities, Intents and Filters.
Android Using Menus Notes are based on: The Busy Coder's Guide to Android Development by Mark L. Murphy Copyright © CommonsWare, LLC. ISBN:
Mobile Programming Midterm Review
Introducing Intents Intents Bind application components and navigate between them Transform device into collection of interconnected systems Creating a.
Activities and Intents Richard S. Stansbury 2015.
Android and s Ken Nguyen Clayton state University 2012.
Lecture 2: Android Concepts
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.
The Ingredients of Android Applications. A simple application in a process In a classical programming environment, the OS would load the program code.
CS371m - Mobile Computing Intents 1. Allow us to use applications and components that are already part of Android System – start activities – start services.
Chapter 2: Simplify! The Android User Interface
Lab7 – Appendix.
Android Programming - Features
Intents and Broadcast Receivers
Mobile Software Development for Android - I397
Lecture 3 Zablon Ochomo Android Layouts Lecture 3 Zablon Ochomo
Android Introduction Hello World
Android Application Development 1 6 May 2018
Android N Amanquah.
CS499 – Mobile Application Development
Broadcast receivers.
Android – Event Handling
Android Introduction Hello World.
Android Notifications
Messaging Unit-4.
ITEC535 – Mobile Programming
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 אליהו חלסצ'י.
Android Programming Lecture 9
Android Notifications (Part 2) Plus Alarms and BroadcastReceivers
BMI Android Application will take weight and height from the users to calculate Body Mass Index (BMI) with the information, whether user is underweight,
Activities and Intents
Android Notifications
Android Developer Fundamentals V2 Lesson 5
Mobile Programming Dr. Mohsin Ali Memon.
Mobile Programming Broadcast Receivers.
Presentation transcript:

Mobile Computing Lecture#08 IntentFilters & BroadcastReceivers

Lecture Contents  Intent Filters for Plug-ins/Extensibility  Annonymous Actions to Applications  Intents to Broadcast Events  Listening for Broadcasts  Broadcast Receivers  BroadcastReceivers in Code  BroadcastReceivers in XML  Native Android Broadcast Actions 2

Broadcasting  System level message sending mechanism  Structured message sending across applications  Intents can be used to send messages across applications via sendBroadcast() method  Broadcast intents extend the event driven approach (all applications on a system may behave like event handlers)  Applications registered to handle an event can react to that event without causing any change in event generating application 3

Broadcasting an Event Two step process:::: 1. Build the intent for Broadcast 2. Broadcast the built intent 4

Android Code public static final String new_life_appeared = “com.test.lives.NEW_LIFE”; Intent intent = new Intent(new_life_appeared); intent.putExtra(“type”, “human_life”); intent.putExtra(“where”, “unknown_location”); …….. sendBroadcast(intent); 5

Listening for Broadcasts  BroadcastReceiver is a instance of a class that has registered itself as receiver for a particular broadcast event  For a class to work as a BroadcastRegister, it must register itself as BroadcastReceiver  Two methods to register:: 1. Register in manifest 2. Register in code 6

Listening for Broadcasts  While registering as a BroadcastReceiver a class must specify the intent-filter to describe which event this class is listening for 7

Creating a BroadcastReceiver public class MyBroadcastReceiver extends BroadcastReceiver{ public void onReceive(Context c, Intent intent){ //Some code to handle the event }//End of onReceive } //End of MyBroadcastReceiver 8  onReceive() function is called automatically when event-generated is matched with the one described in intent-filter tag.

onReceive() example public void onReceive(Context context, Intent intent){ Uri data = intent.getData(); String type = intent.getStringExtra(“type”); …… Typically launch some activity/service to perform some action based on the intent received. } 9

BroadcastReceiver in XML ………………….. 10 Receiver registered in xml (manifest) will always be active (even if application is not running/application is in background)

Receiver Example Code public void onReceive(Context context, Intent intent){ Bundle extras = intent.getExtras(); if (extras != null) { String state = extras.getString(TelephonyManager.EXTRA_STATE); if (state.equals(TelephonyManager.EXTRA_STATE_RINGING)) { String phoneNumber = extras.getString(TelephonyManager.EXTRA_INCOMING_NUMBER); Log.i(tag+":Number", phoneNumber); } 11

Receiver Example Manifest 12

Registering Receiver in Code IntentFilter filter = new IntentFilter(string-event); MyBroadcastReceiver receiver = new MyBroadcastReceiver(); registerReceiver(receiver, filter); 13

Unregister a Receiver unregisterReceiver(receiver); 14

Dynamic Receiver A receiver can register as a receiver for any global event for a particular period of time and later can unregister when span of interest is gone. 1. Listening for outgoing calls during office-hours 2. When phone screen is turned on/off during night 3. …………… 15

Native Android Broadcast Events ACTION_BOOT_COMPLETEDFired once when the device has completed its startup sequence. An application requires the RECEIVE_BOOT_COMPLETED permission to receive this broadcast ACTION_CAMERA_BUTTONFired when camera button is clicked ACTION_DATE_CHANGED ACTION_TIME_CHANGED Fired when system’s date/time is changed manually ACTION_MEDIA_EJECT  If the user chooses to eject the external storage media, this event is fired first.  If your application is reading or writing to the external media storage you should listen for this event in order to save and close any open file handles. 16

Native Android Broadcast Events ACTION_MEDIA_MOUNTED ACTION_MEDIA_UNMOUNTED These two events are broadcast whenever new external storage media are successfully added to or removed from the device. ACTION_NEW_OUTGOING_CALLBroadcast when a new outgoing call is about to be placed. Listen for this broadcast to intercept outgoing calls. ACTION_SCREEN_OFF ACTION_SCREEN_ON Broadcast when the screen turns off or on Respectively. ACTION_TIMEZONE_CHANGEDThis action is broadcast whenever the phone’s current time zone changes. The Intent includes a time-zone extra that returns the ID of the new java.util.TimeZone. 17

Pending Intents  The PendingIntent class provides a mechanism for creating Intents that can be fired by another applicationat a later time.  A Pending Intent is commonly used to package an Intent that will be fired in response to a future event, such as a widget View being clicked or a Notification being selected from the notification panel.  PendingIntent class offers static methods to construct Pending Intents used to start an Activity, start a Service, or broadcast an Intent. 18

Pending Intents // Start an Activity Intent startIntent = new Intent(this, OtherActivity.class); PendingIntent.getActivity(this, 0, startIntent, 0); // Broadcast an Intent Intent broadcastIntent = new Intent(NEW_LIFEFORM_DETECTED); PendingIntent.getBroadcast(this, 0, broadcastIntent, 0); 19

Pending Intent It is a token that you give to a foreign application (e.g. Notification Manager, Alarm Manager, Home Screen AppWidget Manager, or other 3rd party applications), which allows a foreign application to use your application's permissions to execute a predefined piece of code. 20

Pending Intent If you give the foreign application an Intent, and that application sends/broadcasts the Intent you gave, they will execute the Intent with their own permissions. But if you instead give the foreign application a Pending Intent you created using your own permission, that application will execute the contained Intent using your application's permission. 21

Pending Intent Example (Main.xml) <LinearLayout xmlns:android=… android:orientation="vertical“ android:layout_width="fill_parent“ android:layout_height="fill_parent"> <EditText android:layout_height="wrap_content" android:layout_width="wrap_content" android:hint="Number of seconds" android:inputType="numberDecimal"/> 22 <Button android:text="Start Counter" android:onClick="startAlert" android:layout_width="wrap_content" android:layout_height="wrap_content” />

Pending Intent Example (TimerReceiver.java) public class TimerReceiver extends BroadcastReceiver { public void onReceive(Context context, Intent intent){ Toast.makeText(context, “Your time is up", Toast.LENGTH_LONG).show(); // Vibrate the mobile phone Vibrator vibrator = (Vibrator) context.getSystemService(Context.VIBRATOR_SERVICE); vibrator.vibrate(2000); } 23

Pending Intent Example (Main.java) public class Main extends Activity { EditText time; /** Called when the activity is first created. public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); } 24

Pending Intent Example (Manifest) 25