Android Programming Lecture 5

Slides:



Advertisements
Similar presentations
Android Application Development A Tutorial Driven Course.
Advertisements

Programming with Android: Activities and Intents
Application Fundamentals Android Development. Announcements Posting in D2L Tutorials.
Programming with Android: Activities and Intents Luca Bedogni Marco Di Felice Dipartimento di Scienze dell’Informazione Università di Bologna.
Programming with Android: Activities and Intents Luca Bedogni Marco Di Felice Dipartimento di Informatica – Scienza e Ingegneria Università di Bologna.
Android 02: Activities David Meredith
Intents.
Manifest File, Intents, and Multiple Activities. Manifest File.
Android 101 Application Fundamentals January 29, 2010.
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()
ANDROID PROGRAMMING MODULE 1 – GETTING STARTED
INTERNATIONAL SUMMER ACADEMIC COURSE UNIVESITY OF NIS ISAC – Android programming.
Emerging Platform#4: Android Bina Ramamurthy.  Android is an Operating system.  Android is an emerging platform for mobile devices.  Initially developed.
Android Middleware Bo Pang
Chien-Chung Shen Manifest and Activity Chien-Chung Shen
 Understanding an activity  Starting an activity  Passing information between activities  Understanding intents  Understanding the activity lifecycle.
CS5103 Software Engineering Lecture 08 Android Development II.
© Keren Kalif Intro to Android Development Written by Keren Kalif, Edited by Liron Blecher Contains slides from Google I/O presentation.
Favorite Twitter® Searches App Android How to Program © by Pearson Education, Inc. All Rights Reserved.
Intent Android Club Agenda Intent class Explicit activation Implicit activation.
This work is licensed under the Creative Commons Attribution 4.0 International License. To view a copy of this license, visit
CS378 - Mobile Computing Intents.
Overview of Android Application Development
CS378 - Mobile Computing Intents. Allow us to use applications and components that are part of Android System – start activities – start services – deliver.
COMP 365 Android Development.  Every android application has a manifest file called AndroidManifest.xml  Found in the Project folder  Contains critical.
Intent Erick Pranata © Sekolah Tinggi Teknik Surabaya 1.
Lec 03 Intents Explicit Intents Implicit Intents.
Linking Activities using Intents How to navigate between Android Activities 1Linking Activities using Intents.
Applications with Multiple Activities. Most applications will have more than one activity. The main activity is started when the application is started.
Working with Multiple Activities. Slide 2 Introduction Working with multiple activities Creating multiple views Introduction to intents Passing data to.
Lec 04 Intents and Bundles Fragments. Activated by Intents Activities Services Broadcast Receivers (aka Receivers) (
Introducing Intents Intents Bind application components and navigate between them Transform device into collection of interconnected systems Creating a.
Lecture 2: Android Concepts
Activities and Intents Chapter 3 1. Objectives Explore an activity’s lifecycle Learn about saving and restoring an activity Understand intents and how.
Intents and Broadcast Receivers Dr. David Janzen Except as otherwise noted, the content of this presentation is licensed under the Creative Commons Attribution.
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.
Android Mobile Application Development
Android Application -Architecture.
Reactive Android Development
Mobile Applications (Android Programming)
Lecture 2: Android Concepts
Android 01: Fundamentals
Mobile Application Development BSCS-7 Lecture # 2
Programming with Android:
Android 5: Interacting with Other Apps
Linking Activities using Intents
3 Introduction to Classes and Objects.
Android Runtime – Dalvik VM
Lecture 3 agenda A tour of Android studio features
MAD.
Activities and Intents
Mobile Application Development Chapter 4 [Android Navigation and Interface Design] IT448-Fall 2017 IT448- Fall2017.
Mobile Application Development BSCS-7 Lecture # 3
Mobile Device Development
Anatomy of an Android Application
Android Programming Lecture 9
CS5103 Software Engineering
Application Development A Tutorial Driven Course
Activities and Intents
Android Topics What are Intents? Implicit Intents vs. Explicit Intents
HNDIT2417 Mobile Application Development
Activities and Intents
Activities and Intents
Emerging Platform#3 Android & Programming an App
Activities and Intents
It is used to Start an Activity Start a Service Deliver a Broadcast
Lecture 2: Android Concepts
Chapter 5 Your Second Activity.
Presentation transcript:

Android Programming Lecture 5 Testing notes Intent

What is Intent From Google: An Intent object is a bundle of information. It contains information of interest to the component that receives the intent (such as the action to be taken and the data to act on) plus information of interest to the Android system (such as the category of component that should handle the intent and instructions on how to launch a target activity)

Start an Activity with Intent Explicit Intent Activity 1 Activity 2 Intent Intent anIntent = new Intent(Activity1.this, Activity2.class); startActivity(anIntent); Activity 1 Activity 2 Intent Intent AnotherIntent = new Intent(Activity2.this, Activity1.class); startActivity(AnotherIntent);

Activity Stack

The activities are arranged in a stack, called task, in the order in which each activity is opened. When the current activity starts another, the new activity is pushed on the top of the stack and takes focus. The previous activity remains in the stack, but is stopped. Refer to: http://developer.android.com/guide/components/tasks-and-back-stack.html

When the user presses the Back button, the current activity is popped from the top of the stack and the previous activity resumes Refer to: http://developer.android.com/guide/components/tasks-and-back-stack.html

task A single activity is instantiated multiple times. User selects to view the second scene Activity 2 User get back to the main menu Main Menu User selects to view the first scene Activity 1 Separate instance Main Menu User launches the app A single activity is instantiated multiple times.

Launch Modes of Activity The launchMode attribute specifies an instruction on how the activity should be launched into a task There are four different launch modes you can assign to the launchMode attribute standard: (the default mode) singleTop: Reuse the activity on the top singleTask:The system creates a new task and instantiates the activity at the root of the new task. singleInstance Refer to: http://developer.android.com/guide/components/tasks-and-back-stack.html http://inthecheesefactory.com/blog/understand-android-activity-launchmode/en

Launch Modes of Activity <activity android:name=".NewsDetailActivity" android:launchMode="singleTop"> <intent-filter> <action android:name="android.intent.action.VIEW"></action> <category android:name="android.intent.category.DEFAULT"></category> <category android:name="android.intent.category.BROWSABLE"></category> <data android:scheme="oauth" android:host="twitt"></data> </intent-filter> </activity> http://www.intridea.com/blog/2011/6/16/android-understanding-activity-launchmode

Pass Data using Intent

Passing Data between Activities Intent Activity 1 Activity 2 // Create an activity using the constructor Intent intent = new Intent(Activity1.this, Activity2.class); // Create a bundle Bundle bundleData = new Bundle(); // Create put string data in the bundle (key-value pair) bundleData.putString("key_name", "name"); bundleData.putString("key_age", "age"); // Create put the bundle data in the Extra of Intent intent.putExtras(bundleData); // Invoke the activity using the created intent startActivity(intent); The Bundle class provides a container for storing data

Bundle The Bundle class provides a container for storing data using a key- value pair (string values to various Parcelable types) mechanism void putFloat(String key, float value) // Inserts a float value into the mapping of this Bundle, replacing any existing value for the given key. void putFloatArray(String key, float[] value) // Inserts a float array value into the mapping of this Bundle, replacing any existing value for the given key. void putInt(String key, int value) // Inserts an int value into the mapping of this Bundle, replacing any existing value for the given key. void putIntArray(String key, int[] value) // Inserts an int array value into the mapping of this Bundle, replacing any existing value for the given key. void putString(String key, String value) // Inserts a String value into the mapping of this Bundle, replacing any existing value for the given key. void putStringArray(String key, String[] value) // Inserts a String array value into the mapping of this Bundle, replacing any existing value for the given key. Class reference: http://developer.android.com/reference/android/os/Bundle.html

Passing Data between Activities Intent Activity 1 Activity 2 // Create an activity using the constructor Intent intent = new Intent(Activity1.this, Activity2.class); // Create a bundle Bundle bundleData = new Bundle(); // Create put string data in the bundle (key-value pair) bundleData.putString("key_name", "name"); bundleData.putString("key_age", "age"); // Create put the bundle data in the Extra of Intent intent.putExtras(bundleData); // Instead of using bundle, we can also put data, e.g., strings, directly in the Extra of Intent intent.putExtra("key_id", "id"); intent.putExtra("key_address", "address"); // Invoke the activity using the created intent startActivity(intent);

Passing Data between Activities Activity 1 Intent intent = new Intent(Activity1.this, Activity2.class); Bundle bundle = new Bundle(); bundle.putString("key_name", "name"); bundle.putString("key_age", "age"); intent.putExtras(bundle); intent.putExtra("key_id", "id"); intent.putExtra("key_address", "address"); startActivity(intent); Intent putXXX() to load data Activity 2 // In Activity2.java // Retrieve the intent Intent intent = getIntent(); // Retrieve the string data in the intent String name = intent.getStringExtra("key_name"); String age = intent.getStringExtra("key_age"); String id = intent.getStringExtra("key_id"); String address = intent.getStringExtra("key_address"); getXXX() to retrieve data

Extra An Intent carries additional information through Extra Extras are Key-value pairs You can add extra data with various putExtra() methods, each accepting two parameters: the key name and the value. putExtra(String name, int value), putExtra(String name, float value), putExtra(String name, byte value) You can also create a Bundle object with all the extra data, then insert the Bundle in the Intent with putExtras() Bundle data = new Bundle(); bundle.putString("key_name", "name"); bundle.putString("key_age", "age"); intent.putExtras(data);

Implicit Intent

Implicit Intent Android supports two types of intents: explicit or implicit Explicit intents must know the Java class name of the component that they want to start Implicit intents must know the action type they want to be done for them

Explicit Intent Example Assuming that you need to get to a Telstra technician to help you set up your home Internet. Android OS Can you direct me to John in technician group?

Implicit Intent Example Implicit intent identifies the activities by describing the required services on the specified data Can you direct me to someone, who can help me setup the home Internet, and I am using cable connection, living in Burwood, VIC, …

Activity A creates an Intent with an Action description and passes it to startActivity(). The Android System searches all apps for an intent filter that matches the intent. When a match is found, the system starts the matching activity (Activity B) by invoking its onCreate() method and passing it the Intent.

Implicit Intent Implicit Intents have not specified a component as explicit intents; Instead, they must include enough information for the system to determine which of the available components is best to run for that intent. Action (services), Category and Data Implicit intents are very useful to re-use code and to launch external applications

Intent Filter An intent filter describes the Actions (or services) that an activity can perform, and type of Data that an activity can process Each intent filter is defined by an <intent-filter> element in the app’s manifest file, nested in the corresponding app component (such as an <activity> element). Include the following tags within the <intent- filter> tag <action>: Specify the ACTION String value accepted by this component <data>: Specify the type of data accepted by this component consisting of MIME type and the URI made up of scheme, host, port and path. <category>: Specify the category of Intents accepted by this component. To receive implicit Intents, specify CATEGORY_DEFAULT in the IntentFilter. http://developer.android.com/guide/components/intents-filters.html http://www.tutorialspoint.com/android/android_intents_filters.htm

<intent-filter> Manifest tag A single IntentFilter may include more than one <action>,<category> and <data>. In this case the target component must be able to handle any or all combination of Intents. To allow several specific combinations of <action>, <category> and <data>, define multiple IntentFilters. Each IntentFilter will be tested against an incoming Intent to satisfy the <action>, <category> and <data> tests. The Intent will be delivered to the component only if it can pass all the tests for at least one IntentFilter. In order to receive implicit intents, you must include the CATEGORY_DEFAULT category in the intent filter <activity android:name="TestActivity"> <intent-filter> <action android:name="android.intent.action.SEND"/> <category android:name="android.intent.category.DEFAULT"/> <data android:mimeType="text/plain"/> </intent-filter> </activity>

Intent intent = new Intent(Intent. ACTION_VIEW, Uri. parse("http://www startActivity(intent); Action to perform Data that the action to perform on The Android System will search the intent filter of all the apps to find an activity with ACTION_VIEW defined, and can open the data in the form of “http”

Common Intent Action Name Description ACTION_CALL Initiate a phone call ACTION_EDIT Display data to edit ACTION_MAIN Start as a main entry point, does not expect to receive data. ACTION_PICK Pick an item from the data, returning what was selected. ACTION_VIEW Display the data to the user ACTION_SEARCH Perform a search http://developer.android.com/guide/components/intents-common.html http://developer.android.com/reference/android/content/Intent.html

Common Intent // Intent to call a webpage Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.yahoo.com")); startActivity(intent); // Intent to send an email Intent intent = new Intent(Intent.ACTION_SENDTO, Uri.parse("mailto:tom.luan@deakin.edu.au")); startActivity(intent); // Intent to call a number Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:0392446445")); startActivity(intent); // Intent to dial a number Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse("tel:0392446445")); startActivity(intent); // Intent to send a text message Intent intent = new Intent(); intent.setAction(Intent.ACTION_SEND); intent.putExtra(Intent.EXTRA_TEXT, "This is my text to send."); intent.setType("text/plain"); startActivity(intent);

User Permissions In order to access external components in android, permissions needs to be set in AndroidManifest.xml file. <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.commonintent" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="19" /> <uses-permission android:name="android.permission.CALL_PHONE"/> <uses-permission android:name="android.permission.CAMERA"/> <uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/> <uses-permission android:name="android.permission.BLUETOOTH"/> <uses-permission android:name="android.permission.INTERNET"/> </manifest> http://developer.android.com/guide/topics/manifest/uses-permission-element.html

Manifest.xml

What is Android Manifest File Every application must have an Android Manifest.xml file (with precisely that name) in its root directory. The manifest presents essential information about the application to the Android system. information the system must have before it can run any of the application's code.

Tags in Manifest xml File <action> <activity> <activity-alias> <application> <category> <data> <grant-uri-permission> <instrumentation> <intent-filter> <manifest> <meta-data> <permission> <permission-group> <permission-tree> <provider> <receiver> <service> <uses-configuration> <uses-library> <uses-permission> <uses-sdk> http://developer.android.com/guide/topics/manifest/manifest-intro.html

AndroidManifest.xml Applications should declare everything needed on the the AndroidManifest.xml file … One AndroidManifest.xml for application .. What's contained in it? Permissions Hardware and Software resources used by the Application Activities Intent-filters

Summary What is an activity stack? What is launch mode of an activity? What is a Bundle? How to pass data from one activity to another one? What is the different between explicit intent and implicit intent? What is an intent filter?