Intents and Broadcast Receivers

Slides:



Advertisements
Similar presentations
Application Fundamentals Android Development. Announcements Posting in D2L Tutorials.
Advertisements

Android 02: Activities David Meredith
Intents.
Manifest File, Intents, and Multiple Activities. Manifest File.
The Activity Class 1.  One application component type  Provides a visual interface for a single screen  Typically supports one thing a user can do,
Mobile Programming Pertemuan 6 Presented by Mulyono Poltek NSC Surabaya.
Intent An Intent describes the operation to be performed. Intents are used to start an activity using either of the following methods – Context.startActivity()
@2011 Mihail L. Sichitiu1 Android Introduction Application Fundamentals.
Bluetooth. Bluetooth is an open, wireless protocol for exchanging data between devices over a short distance. –managed by the Bluetooth Special Interest.
Software Architecture of Android Yaodong Bi, Ph.D. Department of Computing Sciences University of Scranton.
Mobile Computing Lecture#08 IntentFilters & BroadcastReceivers.
CSS216 MOBILE PROGRAMMING Android, Chapter 5 Book: “Professional Android™ 2 Application Development” by Reto Meier, 2010 by: Andrey Bogdanchikov (
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.
CS378 - Mobile Computing Intents.
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.
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.
Cosc 5/4730 Android Communications Intents, callbacks, and setters.
Linking Activities using Intents How to navigate between Android Activities 1Linking Activities using Intents.
Activity Android Club Agenda Hello Android application Application components Activity StartActivity.
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.
1 Mobile Software Development Framework: Android Inter- Thread, Process Communications 10/11/2012 Y. Richard Yang.
Working with Multiple Activities. Slide 2 Introduction Working with multiple activities Creating multiple views Introduction to intents Passing data to.
Introducing Intents Intents Bind application components and navigate between them Transform device into collection of interconnected systems Creating a.
Intents 1 CS440. Intents  Message passing mechanism  Most common uses:  starting an Activity (open an , contact, etc.)  starting an Activity.
Lecture 2: Android Concepts
Technische Universität München Services, IPC and RPC Gökhan Yilmaz, Benedikt Brück.
Activities and Intents Chapter 3 1. Objectives Explore an activity’s lifecycle Learn about saving and restoring an activity Understand intents and how.
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.
David Sutton SMS TELEPHONY IN ANDROID. OUTLINE  This week’s exercise, an SMS Pub Quiz  Simulating telephony on an emulator  Broadcast Intents and broadcast.
Working with Multiple Activities. Slide 2 Introduction Working with multiple activities Putting together the AndroidManifest.xml file Creating multiple.
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.
Lecture 2: Android Concepts
Android Application Development 1 6 May 2018
Activity and Fragment.
several communicating screens
Programming with Android:
Broadcast receivers.
Android 5: Interacting with Other Apps
Linking Activities using Intents
Basic Activities and Intents
Reactive Android Development
Mobile Software Development Framework: Android
Android Programming Lecture 9
Developing Android Services
Android Programming Lecture 5
Android Notifications (Part 2) Plus Alarms and BroadcastReceivers
Application Fundamentals
Mobile Software Development Framework: Android
Activities and Intents
Activities and Intents
Android Developer Fundamentals V2 Lesson 5
Objects First with Java
Activities and Intents
Application Fundamentals
Lecture 2: Android Concepts
Objects First with Java
Mobile Programming Dr. Mohsin Ali Memon.
Chapter 5 Your Second Activity.
Activities, Fragments, and Intents
Mobile Programming Broadcast Receivers.
CA16R405 - Mobile Application Development (Theory)
Presentation transcript:

Intents and Broadcast Receivers Dr. David Janzen Except as otherwise noted, the content of this presentation is licensed under the Creative Commons Attribution 2.5 License.

Intents Allows communication between loosely-connected components Allows for late run-time binding of components Explicit Implicit Intent myIntent = new Intent(AdventDevos.this, Devo.class); myIntent.putExtra("ButtonNum", ""+index); startActivity(myIntent); //finish(); //removes this Activity from the stack Intent i = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.biblegateway.com/passage/?search="+ passage +"&version=NIV")); startActivity(i);

Other Native Android Actions ACTION_ANSWER – handle incoming call ACTION_DIAL – bring up dialer with phone # ACTION_PICK – pick item (e.g. from contacts) ACTION_INSERT – add item (e.g. to contacts) ACTION_SENDTO – send message to contact ACTION_WEB_SEARCH – search web

Sub-Activities Activities are independent However, sometimes we want to start an activity that gives us something back (e.g. select a contact and return the result) Use startActivityForResult(Intent i, int id) instead of startActivity(Intent)

Capturing Intent Return Results class ParentActivity extends Activity { private static final int SUB_CODE = 34; … Intent intent = new Intent(…); startActivityForResult(intent, SUB_CODE); @Override public void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); if (requestCode == SUB_CODE) if (resultCode == Activity.RESULT_OK) { Uri returnedUri = data.getData(); String returnedString = data.getStringExtra(SOME_CONSTANT,””); } if (resultCode == Activity.RESULT_CANCELED) { … } };

Capturing Intent Return Results class SubActivity extends Activity { … if (/* everything went fine */) { Uri data = Uri.parse(“content://someuri/”); Intent result = new Intent(null,data); result.putStringExtra(SOME_CONSTANT, “This is some data”); setResult(RESULT_OK, result); finish(); } if (/* everything did not go fine or the user did not complete the action */) { setResult(RESULT_CANCELED, null); };

Using a Native App Action  public class MyActivity extends Activity { //from http://developer.android.com/reference/android/app/Activity.html      ...      static final int PICK_CONTACT_REQUEST = 0;      protected boolean onKeyDown(int keyCode, KeyEvent event) {          if (keyCode == KeyEvent.KEYCODE_DPAD_CENTER) {              // When the user center presses, let them pick a contact.              startActivityForResult(                  new Intent(Intent.ACTION_PICK, new Uri("content://contacts")),                   PICK_CONTACT_REQUEST);             return true;          }          return false;      }      protected void onActivityResult(int requestCode, int resultCode, Intent data) {          if (requestCode == PICK_CONTACT_REQUEST) {              if (resultCode == RESULT_OK) {                  // A contact was picked.  Here we will just display it to the user.                  startActivity(new Intent(Intent.ACTION_VIEW, data));              }          }      } }

Broadcasts and Broadcast Receivers So far we have used Intents to start Activities Intents can also be used to send messages anonymously between components Messages are sent with sendBroadcast() Messages are received by extending the BroadcastReceiver class

Sending a Broadcast Typically like a package name to keep it unique //… public static final String MAP_ADDED = “com.simexusa.cm.MAP_ADDED”; Intent intent = new Intent(MAP_ADDED); intent.putStringExtra(“mapname”, “Cal Poly”); sendBroadcast(intent); Typically like a package name to keep it unique

Receiving a Broadcast public class MapBroadcastReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { Uri data = intent.getData(); String name = data.getStringExtra(“mapname”); //do something context.startActivity(…); } }; Must complete in <5 seconds Broadcast Receivers are started automatically – you don’t have to try to keep an Activity running

Registering a BroadcastReceiver Statically in ApplicationManifest.xml or dynamically in code (e.g. if only needed while visible) <receiver android:name=“.MapBroadcastReceiver”> <intent-filter> <action android:name=“com.simexusa.cm.MAP_ADDED”> </intent-filter> </receiver> IntentFilter filter = new IntentFilter(MAP_ADDED); MapBroadcastReceiver mbr = new MapBroadcastReceiver(); registerReceiver(mbr, filter); … unregisterReceiver(mbr); in onRestart() ? in onPause() ?

Native Broadcasts ACTION_CAMERA_BUTTON ACTION_TIMEZONE_CHANGED ACTION_BOOT_COMPLETED requires RECEIVE_BOOT_COMPLETED permission

Intent Filters Intent filters register application components with Android Intent filter tags: action – unique identifier of action being serviced category – circumstances when action should be serviced (e.g. ALTERNATIVE, DEFAULT, LAUNCHER) data – type of data that intent can handle Ex. URI = content://com.example.project:200/folder/subfolder/etc Components that can handle implicit intents (one’s that are not explicitly called by name), must declare category DEFAULT or LAUNCHER

        <activity android:name="NotesList" android:label="@string/title_notes_list">             <intent-filter>                 <action android:name="android.intent.action.MAIN" />                 <category android:name="android.intent.category.LAUNCHER" />             </intent-filter>             <intent-filter>                 <action android:name="android.intent.action.VIEW" />                 <action android:name="android.intent.action.EDIT" />                 <action android:name="android.intent.action.PICK" />                 <category android:name="android.intent.category.DEFAULT" />                 <data android:mimeType="vnd.android.cursor.dir/vnd.google.note" />             </intent-filter>             <intent-filter>                 <action android:name="android.intent.action.GET_CONTENT" />                 <category android:name="android.intent.category.DEFAULT" />                 <data android:mimeType="vnd.android.cursor.item/vnd.google.note" />             </intent-filter>         </activity>                 <activity android:name="NoteEditor"                   android:theme="@android:style/Theme.Light"                   android:label="@string/title_note" >             <intent-filter android:label="@string/resolve_edit">                 <action android:name="android.intent.action.VIEW" />                 <action android:name="android.intent.action.EDIT" />                 <action android:name="com.android.notepad.action.EDIT_NOTE" />                 <category android:name="android.intent.category.DEFAULT" />                 <data android:mimeType="vnd.android.cursor.item/vnd.google.note" />             </intent-filter>             <intent-filter>                 <action android:name="android.intent.action.INSERT" />                 <category android:name="android.intent.category.DEFAULT" />                 <data android:mimeType="vnd.android.cursor.dir/vnd.google.note" />             </intent-filter>         </activity> Broadcast Receivers are started automatically – you don’t have to try to keep an Activity running