Multiple Activities. Many apps have multiple activities

Similar documents
CS 193A. Multiple Activities and Intents

Mobile Development Lecture 8: Intents and Animation

Programming with Android: Intents. Luca Bedogni. Dipartimento di Scienze dell Informazione Università di Bologna

Introduction to Android Development

EMBEDDED SYSTEMS PROGRAMMING Android Services

Tabel mysql. Kode di PHP. Config.php. Service.php

Programming with Android: Activities and Intents. Dipartimento di Informatica Scienza e Ingegneria Università di Bologna

Intents. 18 December 2018 Lecture Dec 2018 SE 435: Development in the Android Environment 1

Android permissions Defining and using permissions Component permissions and related APIs

Software Practice 3 Today s lecture Today s Task Porting Android App. in real device

INTRODUCTION TO ANDROID

EMBEDDED SYSTEMS PROGRAMMING Application Tip: Switching UIs

MOBILE APPLICATION DEVELOPMENT LECTURE 10 SERVICES IMRAN IHSAN ASSISTANT PROFESSOR

Islamic University of Gaza. Faculty of Engineering. Computer Engineering Department. Mobile Computing ECOM Eng. Wafaa Audah.

Understand applications and their components. activity service broadcast receiver content provider intent AndroidManifest.xml

Android. Mobile operating system developed by Google A complete stack. Based on the Linux kernel Open source under the Apache 2 license

Mobile Programming Practice Explicit intent Implicit intent Intent filter Lab#4 PA #1

EMBEDDED SYSTEMS PROGRAMMING Application Basics

LifeStreet Media Android Publisher SDK Integration Guide

Mobile Programming Lecture 5. Composite Views, Activities, Intents and Filters

Getting Started With Android Feature Flags

BCS3283-Mobile Application Development

Application Fundamentals

Services. service: A background task used by an app.

Android Overview. Most of the material in this section comes from

UNDERSTANDING ACTIVITIES

Diving into Android. By Jeroen Tietema. Jeroen Tietema,

Understanding Application

Permissions. Lecture 18

Introduction to Android

Produced by. Mobile Application Development. Higher Diploma in Science in Computer Science. Eamonn de Leastar

Android Exam AND-401 Android Application Development Version: 7.0 [ Total Questions: 129 ]

Software Practice 3 Before we start Today s lecture Today s Task Team organization

Getting Started. Dr. Miguel A. Labrador Department of Computer Science & Engineering

CS 528 Mobile and Ubiquitous Computing Lecture 3b: Android Activity Lifecycle and Intents Emmanuel Agu

App Development for Android. Prabhaker Matet

Why using intents. Activity. Screen1 Screen 2

<uses-permission android:name="android.permission.internet"/>

Android Application Development. By : Shibaji Debnath

CS378 -Mobile Computing. Intents

CMSC436: Fall 2013 Week 3 Lab

Terms: MediaPlayer, VideoView, MediaController,

COSC 3P97 Mobile Computing

CSE 660 Lab 7. Submitted by: Arumugam Thendramil Pavai. 1)Simple Remote Calculator. Server is created using ServerSocket class of java. Server.

LECTURE NOTES OF APPLICATION ACTIVITIES

Android. Operating System and Architecture. Android. Screens. Main features

Workshop. 1. Create a simple Intent (Page 1 2) Launch a Camera for Photo Taking

Android Programming Lecture 7 9/23/2011

INTENTS android.content.intent

Android - JSON Parser Tutorial

CS378 - Mobile Computing. Anatomy of an Android App and the App Lifecycle

Android framework overview Activity lifecycle and states

Starting Another Activity Preferences

Chapter 5 Defining the Manifest

Produced by. Mobile Application Development. Higher Diploma in Science in Computer Science. Eamonn de Leastar

CS 4518 Mobile and Ubiquitous Computing Lecture 5: Rotating Device, Saving Data, Intents and Fragments Emmanuel Agu

Android - Widgets Tutorial

Android Services. Victor Matos Cleveland State University. Services

Android Services & Local IPC: The Command Processor Pattern (Part 1)

Real-Time Embedded Systems

Activities. Repo:

CS378 -Mobile Computing. Anatomy of and Android App and the App Lifecycle

Questions and Answers. Q.1) Which of the following is the most ^aeuroeresource hungry ^aeuroepart of dealing with activities on android?

ANDROID APPS (NOW WITH JELLY BEANS!) Jordan Jozwiak November 11, 2012

Android Application Development

Mobile Programming Practice Background processing AsynTask Service Broadcast receiver Lab #5

Services. Marco Ronchetti Università degli Studi di Trento

Android System Architecture. Android Application Fundamentals. Applications in Android. Apps in the Android OS. Program Model 8/31/2015

In this Class Mark shows you how to put applications into packages and how to run them through the command line.

Software Practice 3 Today s lecture Today s Task

Android Intents. Notes are based on: Android Developers

Using Intents to Launch Activities

8/30/15 MOBILE COMPUTING. CSE 40814/60814 Fall How many of you. have implemented a command-line user interface?

LECTURE 12 BROADCAST RECEIVERS

Mobile Computing. Introduction to Android

Android development. Outline. Android Studio. Setting up Android Studio. 1. Set up Android Studio. Tiberiu Vilcu. 2.

Mobile Application Development Android

Android Workshop: Model View Controller ( MVC):

Mobile Application Development Android

Hybrid Apps Combining HTML5 + JAVASCRIPT + ANDROID

Applications. Marco Ronchetti Università degli Studi di Trento

An Overview of the Android Programming

LAMPIRAN. byte bcdtodec(byte val) { return( (val/16*10) + (val%16) ); } void setup() {

Embedded Systems Programming - PA8001

05. RecyclerView and Styles

Android UI Development

Mobile Application Development Android

A Tour of Android. and some of it s APIs. Bryan Noll

Agenda. Overview of Xamarin and Xamarin.Android Xamarin.Android fundamentals Creating a detail screen

PAPER ON ANDROID ESWAR COLLEGE OF ENGINEERING SUBMITTED BY:

Intents, Intent Filters, and Invoking Activities: Part I: Using Class Name

PROGRAMMING APPLICATIONS DECLARATIVE GUIS

University of Babylon - College of IT SW Dep. - Android Assist. Lect. Wadhah R. Baiee Activities

Introduction To Android

Intents and Intent Filters

Camera and Intent. Elena Fortini

Getting started: Installing IDE and SDK. Marco Ronchetti Università degli Studi di Trento

Software Practice 3 Today s lecture Today s Task

Android Development Tutorial. Yi Huang

CSCU9YH Development with Android

Transcription:

Intents Lecture 7

Multiple Activities Many apps have multiple activities An activity A can launch another activity B in response to an event The activity A can pass data to B The second activity B can send data back to A when it is done

Adding an Activity Create a new.xml file in res/layouts Create a new.java class in src/java Adds information to AndroidManifest.xml about the activity (without this information, the app will not allow the activity)

Android Activities An activity uses the setcontentview( ) method to expose (usually) a single UI from which a number of actions could be performed Activities are independent of each other; however they usually cooperate exchanging data and actions Typically, one of the activities is designed as the first one (main) that should be presented to the user when the application is launched

Intents Moving from one activity to another is accomplished by asking the current activity to execute an intent Activities interact with each other in an asynchronous mode

Intents An intent is an abstract description of an operation to be performed Consist of Action (what to do, example: visit a web page) Data (to perform operation on, example: the url of the web page)

Application Components Activities visual user interface focused on a single thing Basic component of most applications Most applications have several activities that start each other as needed Each is implemented as a subclass of the base Activity class Services no visual interface they run in the background Runs in the background indefinitely Examples Network Downloads Playing Music You can bind to an existing service and control its operation Broadcast Receivers receive and react to broadcast announcements Content Providers allow data exchange between applications 7

Activation of Components 3 of the 4 core application components (activities, services, and broadcast receivers) are started via intents Intents are a messaging system to activate components in the same application And to start one application from another

Uses of Intents

Intents startactivity(intent) : launches an Activity sendbroadcast(intent); sends an intent to any interested BroadcastReceiver components startservice(intent) or bindservice(intent, ) communicate with a background service

AndroidManifest.xml The manifest is part of the application project The manifest contains important data about the application that is required by the Android system before the system will run any of the application s code

Issues Common error: activity in application is not included in manifest Runtime error: when application tries to start activity not declared in manifest

AndroidManifest.xml Contains Java package name of application unique id for application Describes components of application: activities, services, broadcast receivers, content providers and intent messages each component can handle Declares permissions requested by application Minimum required API level Libraries application to link to

<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.maozheng.twoactivitiesintent" > <application android:allowbackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:theme="@style/apptheme" > <activity android:name=".mainactivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.main" /> <category android:name="android.intent.category.launcher" /> </intent-filter> </activity> <!-- name defines the class that handles setting up the Activity label will be the title for the activity theme defines the theme to use --> <activity android:name=".secondscreen" android:label="input Name" android:theme="@style/apptheme"/> </application> </manifest>

Launcher Intent <intent-filter> <action android:name="android.intent.action.main" /> <category android:name="android.intent.category.launche R" /> </intent-filter> Declare this as activity to start when application starts

Intents Intent: an object representing a desired action a bridge between activities; a way for one activity to invoke another The activity can be in the same app or in a different app Can store extra data to pass as parameters to that activity Second activity can return information back to the caller if needed

Intents

Intents To launch another activity (usually in response to an event), create an Intent object and call startactivity with it: Intent intent = new Intent(this, ActivityName.class); startactivity(intent); //existing current activity

Intents If you need to pass any parameters or data to the second activity, call putextra on the intent It stores extra data as key/value pairs Intent intent = new Intent(this, ActivityName.class); Intent.putExtra( name1,value1); Intent.putExtra( name2,value2); startactivity(intent);

Extracting extra data In the second activity that was invoked, you can grab any extra data that was passed to it by the calling act. You can access the Intent that spawned you by calling getintent The Intent has methods like getextra, getintextra, getstringextra, etc. to extract any data that was stored inside the intent

Extracting extra data public class SecondActivity extends AppCompatActivity { protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_second); Intent intent = getintent(); String extra = intent.getextra( name1 ); }

Waiting for a result (FirstActivity) If calling activity (FirstActivity) wants to wait for a result from called activity (SecondActivity): 1. Call startactivityforresult startactivityforresult requires you to pass a unique ID number to represent the action being performed By convention, you declare a final int constant with a value of your choice The call to startactivityforresult will not wait; it will return immediately

Waiting for a result (FirstActivity) 2. Write an onactivityresult method that will be called when the second activity is done Check for your unique ID as was passed to startactivityforresult If you see your unique ID, you can ask the intent for any extra data Modify the called activity to send a result back Use its setresult and finish methods to end of the called activity

Sending back a result (SecondActivity) In the second activity that was invoked, send data back Need to create an intent to go back Store any extra data in that intent; call setresult and finish public class SecondActivity extends AppCompatActivity { public void myonclick(view view) { Intent intent = new Intent(); Intent.putExtra( name,value); setresult(result_ok, intent); finish();//calls ondestroy, stops/closes the current activity }

Grabbing the result (FirstActivity)

Intents Using Standard Actions One that launches another app, without naming that specific app, to handle a given type of request or action Examples: invoke default browser, load music player to play a song // go to a web page in the default browser Uri webpage = Uri.parse("http://www.yahoo.com"); Intent webintent = new Intent(Intent.ACTION_VIEW, webpage); startactivity(webintent); //AndroidManifest.xml <uses-permission android:name="android.permission.internet" />

Intents Using Standard Actions Launching the music player // launch music player Intent myintent = new Intent( android.intent.action.music_player ); startactivity(myintent);

Intents Using Standard Actions More examples: Call Geo Mapping an Address Sending email Sending pictures Setting systems

Playing sound (did not use intent) Sound files:.wav, MP3 Sound files in project folder app/src/main/res/ raw In Java code, refer to audio file as R.raw.fileName Donot include the extension Use simple file names, only letters and numbers

Playing sound Load and play clips using Android s MediaPlayer class MediaPlayer mp = MediaPlayer.create(this, mp.start(); R.raw.fileName);