Cosc 5/4730 Android Communications Intents, callbacks, and setters.

Slides:



Advertisements
Similar presentations
Google Android Introduction to Mobile Computing. Android is part of the build a better phone process Open Handset Alliance produces Android Comprises.
Advertisements

Android Application Development Tutorial. Topics Lecture 6 Overview Programming Tutorial 3: Sending/Receiving SMS Messages.
Cosc 5/4730 Android Services. What is a service? From android developer web pages: Most confusion about the Service class actually revolves around what.
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,
Cosc 5/4730 Android Content Providers and Intents.
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.
Cosc 4730 Android TabActivity and ListView. TabActivity A TabActivity allows for multiple “tabs”. – Each Tab is it’s own activity and the “root” activity.
Android Development (Basics)
 Understanding an activity  Starting an activity  Passing information between activities  Understanding intents  Understanding the activity lifecycle.
Chapter 5: Investigate! Lists, Arrays, and Web Browsers.
Mobile Computing Lecture#08 IntentFilters & BroadcastReceivers.
Android Boot Camp for Developers Using Java, Comprehensive: A Guide to Creating Your First Android Apps Chapter 5: Investigate! Android Lists, Arrays,
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.
Cosc 5/4730 Broadcast Receiver. Broadcast receiver A broadcast receiver (short receiver) – is an Android component which allows you to register for system.
DUE Hello World on the Android Platform.
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.
Copyright© Jeffrey Jongko, Ateneo de Manila University Of Activities, Intents and Applications.
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.
Intent Erick Pranata © Sekolah Tinggi Teknik Surabaya 1.
© 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. Android Boot Camp.
Linking Activities using Intents How to navigate between Android Activities 1Linking Activities using Intents.
Services A Service is an application component that can perform long-running operations in the background and does not provide a user interface. An application.
Cosc 4730 Android Fragments. Fragments You can think of a fragment as a modular section of an activity, which has its own lifecycle, receives its own.
Activity Android Club Agenda Hello Android application Application components Activity StartActivity.
Mobile Programming Midterm Review
CIS Intro to JAVA Lecture Notes Set July-05 GUI Programming – Home and reload buttons for the webbrowser, Applets.
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.
Intents 1 CS440. Intents  Message passing mechanism  Most common uses:  starting an Activity (open an , contact, etc.)  starting an Activity.
Android Intents Nasrullah. Using the application context You use the application context to access settings and resources shared across multiple activity.
Services Background operating component without a visual interface Running in the background indefinitely Differently from Activity, Service in Android.
Lecture 2: Android Concepts
Cosc 5/4735 Voice Actions Voice Interactions (API 23+)
Cosc 4735 Activities, fragments, callbacks/listeners/interfaces.
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) 오지영.
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.
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.
Intents and Broadcast Receivers
Lecture 2: Android Concepts
several communicating screens
Broadcast receivers.
Linking Activities using Intents
Activities, Fragments, and Events
Android Application Development android.cs.uchicago.edu
Developing Android Services
Android Notifications (Part 2) Plus Alarms and BroadcastReceivers
CIS 470 Mobile App Development
Activities and Intents
Activities and Intents
CIS 470 Mobile App Development
Objects First with Java
Service Services.
Activities and Intents
Lecture 2: Android Concepts
Objects First with Java
Activities, Fragments, and Intents
CIS 694/EEC 693 Android Sensor Programming
CA16R405 - Mobile Application Development (Theory)
Presentation transcript:

Cosc 5/4730 Android Communications Intents, callbacks, and setters.

INTENTS Android

Intents An intent is an abstract description of an operation to be performed. It can be used with startActivity to launch an Activity, broadcastIntent to send it to any interested BroadcastReceiver components, and startService(Intent) or bindService(Intent, ServiceConnection, int) to communicate with a background Service. – An activity can also receive a “new” intent, via the OnIntent(Intent) method An Intent provides a facility for performing late runtime binding between the code in different applications. Its most significant use is in the launching of activities, where it can be thought of as the glue between activities. It is basically a passive data structure holding an abstract description of an action to be performed.

Intents The intent – Contains the class to be “called” by the activity – And may contain other information needed by that class (Activity, service, Broadcast receiver, etc)

Making a call example The intent is pretty simple Intent dialIntent = new Intent( "android.intent.action.CALL", Uri.parse("tel: ")); – Use “android.intent.action.DIAL” To just bring up the dialer, but not call. startActivity(dialIntent); Needs – In the manifest.xml file.

Web browser intent To start up the browser with a page location you specify – Intent dialIntent = new Intent( "android.intent.action.VIEW", Uri.parse(" – startActivity(dialIntent);

Other “standard” intents You can start up the maps with an intent – intent = new Intent(Intent.ACTION_VIEW, Uri.parse("geo: , ?z=19")); Should show Laramie on the map. Will force closes on the simulator. Launch the camera – intent = new Intent("android.media.action.IMAGE_CAPTURE"); – Show contacts – intent = new Intent(Intent.ACTION_VIEW, Uri.parse("content://contacts/people/")); – – Selection a contacts returns the information to the activity Edit a contact – Intent = new Intent(Intent.ACTION_EDIT, Uri.parse("content://contacts/people/1")); Brings up Editor the first entry in the contacts list.

Returning data. Instead of startActivity, use – startActivityForResult(intent, resultCode); resultCode is a number you pick, that you can identify the callback with. Override onActivityResult(int requestCode, int resultCode, Intent data) method.

Passing data to a new activity. Create an intent – Intent i = new Intent(this, ActivityTwo.class); – i.putExtra(“key1”, “Some data”); – i.putExtra(“key2”, “more data”); Where key1, key2 are names both activities know. – startActivityForResult(i, REQUEST_CODE); Assumes we want return data, otherwise use startActivity.

Passing data to a new activity. (2) ActivityTwo – In the onCreate method Bundle extras = getIntent().getExtras(); //Make sure the activity was called correctly. if (extras == null) {return;} String value1 = extras.getString(“key1"); String value2 = extras.getString(“key2"); Like others, many getX methods, like getInt(String key)

Return data. When activityTwo finishes, it can return an Intent with data. In the finish() public void finish() { Intent data = new Intent(); data.putExtra("returnKey1", “some data "); data.putExtra("returnKey2", “more data"); setResult(RESULT_OK, data); super.finish(); } Result: constants are RESULT_OK, RESULT_CANCELED, but you can also use any custom result with an int. – When an activity fails, crashes, the result will be RESULT_CANCELED.

Return data (2) In the Calling protected void onActivityResult(int requestCode, int resultCode, Intent data) { if (resultCode == RESULT_OK && requestCode == REQUEST_CODE) { //remember if RESULT_CANCELED, likely no data in the intent! if (data.hasExtra("returnKey1")) { Toast.makeText(this, data.getExtras().getString("returnKey1"), Toast.LENGTH_SHORT).show(); } } else if (resultCode == RESULT_CANCELED && requestCode == REQUEST_CODE} Toast.makeText(this, “ActivityTwo canceled!”, Toast.LENGTH_SHORT).show(); }

Finally. You can see these intents working in the code provided with the lecture. For more of google applications intents see – intents.html intents.html – ent/Intent.html ent/Intent.html – which also list some 3 rd party intents as well.

More Intents We will use intents all over the place – We also put Intents in PendingIntents, which allows our intents to be launch by others – From the notification for example. Services, Broadcast Receivers also all use intents as we’ll see later on. We can also setup to receive intents in the activities as well with an intent-filter.

CALLBACKS AND SETTERS/ CONSTRUCTORS Android

Communication between fragments Basically we can use callbacks and setters/constructors to communicate between fragments via the activity. The activity passes the data between them or preforms actions the fragments can’t (such as changing fragments).

Setters The constructor takes values when the fragment is instantiated. – This is done with setArguments/getArguments in the factory methods. A setter takes the new values and updates the variables. – The widgets/views are now updates as needed.

callbacks The fragment creates a interface, which is a fragment listener. – The activity implements it. This is likely a button click or other action. – Now the activity receives it and the data. It then preforms the necessary action. Note, When you create a new fragment, studio has a check mark to create skeleton code for the callback in your fragment.

Callbacks fragment Create the interface. public interface OnFragmentInteractionListener { // change parameters to your needs. public void onFragmentInteraction(String Data); } Declare a variable of the interface to use in the fragment. private OnFragmentInteractionListener mListener; Use the onAttach() and OnDeteach() to set the variable public void onAttach(Activity activity) { super.onAttach(activity); try { mListener = (OnFragmentInteractionListener) activity; } catch (ClassCastException e) { } public void onDetach() { super.onDetach(); mListener = null; }

Callbacks fragment (2) Lastly use the variable (carefully). if (mListener != null) { mListener.onFragmentInteraction(variable); } Likely called from some widget/view listener when the user preforms some action.

Callbacks activity The activity then implements the interface. public class MainActivity extends ActionBarActivity implements myFrag.OnFragmentInteractionListener { And somewhere in the public void onFragmentInteraction(String Data) { //implement whatever code is necessary //to carry out the action. } The activity can implement multiple interfaces for different fragments (different names) or the same one for all of them (same name).

LISTVIEWS, FRAGMENTS, AND CALLBACKS.

Frag com with listview. Callback listener, via the onItemClick methods the Activity “sends” the information to the text fragment

Frag com with listview (2) Callback listener, via the onItemClick methods Since the text fragment is not on the screen, it adds it and sets the arguments to the fragment. The fragment reads the position via Get arguments, in the onStart() method. It then displays the correct info. Pressing the back button, return to listview again.

Next time. We will come back and see more of this callbacks in the listview with fragment lecture

Q A &