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

Similar documents
Introduction to Android Multimedia

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

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

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

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

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

Using Intents to Launch Activities

Mobile Application Development Android

Multiple Activities. Many apps have multiple activities

INTENTS android.content.intent

CMSC436: Fall 2013 Week 3 Lab

CS 193A. Multiple Activities and Intents

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

Android Intents. Notes are based on: Android Developers

Developing Android Applications

Android Programming Lecture 7 9/23/2011

Basic UI elements: Android Buttons (Basics) Marco Ronchetti Università degli Studi di Trento

Mobile Development Lecture 8: Intents and Animation

BCS3283-Mobile Application Development

Understanding Application

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

10.1 Introduction. Higher Level Processing. Word Recogniton Model. Text Output. Voice Signals. Spoken Words. Syntax, Semantics, Pragmatics

Mobile Computing Practice # 2c Android Applications - Interface

Android HelloWorld - Example. Tushar B. Kute,

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

Activities and Fragments

EMBEDDED SYSTEMS PROGRAMMING Application Tip: Switching UIs

Android User Interface Android Smartphone Programming. Outline University of Freiburg

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

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

Android User Interface

CS 370 Android Basics D R. M I C H A E L J. R E A L E F A L L

Learn about Android Content Providers and SQLite

Why using intents. Activity. Screen1 Screen 2

else if(rb2.ischecked()) {

ANDROID PROGRAMS DAY 3

M.C.A. Semester V Subject: - Mobile Computing (650003) Week : 2

Writing Efficient Drive Apps for Android. Claudio Cherubino / Alain Vongsouvanh Google Drive Developer Relations

The Intent Class Starting Activities with Intents. Explicit Activation Implicit Activation via Intent resolution

Overview. Lecture: Implicit Calling via Share Implicit Receiving via Share Launch Telephone Launch Settings Homework

Basic GUI elements - exercises

Understanding Intents and Intent Filters

package import import import import import import import public class extends public void super new this class extends public super public void new

Android: Intents, Menus, Reflection, and ListViews

Thread. A Thread is a concurrent unit of execution. The thread has its own call stack for methods being invoked, their arguments and local variables.

Embedded Systems Programming - PA8001

Camera and Intent. Elena Fortini

Intents. Your first app assignment

Starting Another Activity Preferences

Create Parent Activity and pass its information to Child Activity using Intents.

Lab 1: Getting Started With Android Programming

Applications. Marco Ronchetti Università degli Studi di Trento

LifeStreet Media Android Publisher SDK Integration Guide

what we will cover - setting up multiple activities - intents - lab 2 What you need to know for Lab 2

Android Data Storage

Introduction To Android

UNDERSTANDING ACTIVITIES

THE CATHOLIC UNIVERSITY OF EASTERN AFRICA A. M. E. C. E. A

EMBEDDED SYSTEMS PROGRAMMING Application Tip: Saving State

Terms: MediaPlayer, VideoView, MediaController,

1. Location Services. 1.1 GPS Location. 1. Create the Android application with the following attributes. Application Name: MyLocation

Programmation Mobile Android Master CCI

DROIDS ON FIRE. Adrián

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

SMAATSDK. NFC MODULE ON ANDROID REQUIREMENTS AND DOCUMENTATION RELEASE v1.0

Android Services. Victor Matos Cleveland State University. Services

Computer Science E-76 Building Mobile Applications

Outline. Admin. Example: TipCalc. Android: Event Handler Blocking, Android Inter-Thread, Process Communications. Recap: Android UI App Basic Concepts

INTRODUCTION TO ANDROID

App Development for Android. Prabhaker Matet

Action Bar. (c) 2010 Haim Michael. All Rights Reserv ed.

App Development for Smart Devices. Lec #18: Advanced Topics

Android Specifics. Jonathan Diehl (Informatik 10) Hendrik Thüs (Informatik 9)

Android. Broadcasts Services Notifications

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

Android Help. Section 8. Eric Xiao

Android AND-401. Android Application Development. Download Full Version :

Programming with Android: Introduction. Layouts. Dipartimento di Informatica: Scienza e Ingegneria Università di Bologna

Programming Android UI. J. Serrat Software Design December 2017

CSE 660 Lab 3 Khoi Pham Thanh Ho April 19 th, 2015

COMP4521 EMBEDDED SYSTEMS SOFTWARE

MAD ASSIGNMENT NO 3. Submitted by: Rehan Asghar BSSE AUGUST 25, SUBMITTED TO: SIR WAQAS ASGHAR Superior CS&IT Dept.

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

Hybrid Apps Combining HTML5 + JAVASCRIPT + ANDROID

Lecture 7: Data Persistence : shared preferences. Lecturer : Ali Kadhim Al-Bermani Mobile Fundamentals and Programming

Meniu. Create a project:

Database Development In Android Applications

Manifest.xml. Activity.java

Practical 1.ListView example

Intents and Intent Filters

Creating a Custom ListView

CS 234/334 Lab 1: Android Jump Start

Android Coding. Dr. J.P.E. Hodgson. August 23, Dr. J.P.E. Hodgson () Android Coding August 23, / 27

Android Apps Development for Mobile and Tablet Device (Level I) Lesson 2

Security model. Marco Ronchetti Università degli Studi di Trento

Faculty of Engineering Computer Engineering Department Islamic University of Gaza Network Lab # 7 Permissions

Diving into Android. By Jeroen Tietema. Jeroen Tietema,

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

Mobile Application Development Lab [] Simple Android Application for Native Calculator. To develop a Simple Android Application for Native Calculator.

Getting started: Hello Android. Marco Ronchetti Università degli Studi di Trento

Transcription:

Islamic University of Gaza Faculty of Engineering Computer Engineering Department Mobile Computing ECOM 5341 By Eng. Wafaa Audah July 2013 1

Launch activitits, implicit intents, data passing & start activity for result Intents An Android application could include any number of activities. Activities are independent of each other; however they usually cooperate exchanging data and actions. Moving from one activity to another is accomplished by asking the current activity to execute an intent. Intents are used to start new activities, either explicitly or implicitly. Intents are used as a message-passing mechanism within and between applications. Intents are used for broadcasting messages across the system. Intents Types There are two primary forms of intents you will use. - Explicit Intents: provides the exact class to be run. Often these will not include any other information, simply being a way for an application to launch various internal activities it has as the user interacts with the application. - Implicit Intents: they must include enough information for the system to determine which of the available components is best to run for that intent. Android provides some implicit actions that can be caught and handled by OS. The main arguments of implicit Intent are: - Action: The built-in action to be performed, such as ACTION_VIEW, ACTION_MAIN, etc. - Data: The primary data to operate on, such as a phone number to be called (expressed as tel:, http://, smsto:) 2

Launch activity: Intent myactivity = new Intent (action, data); startactivity (myactivity); Examples of native action/data pairs are: ACTION_DIAL tel:1234567 Display the phone dialer with the given number filled in. ACTION_CALL tel:1234567 Brings up a phone dialer and immediately initiates a call using the number supplied in the Intent URI. ACTION_VIEW - ACTION_VIEW http://www.google.com Show Google page in a browser view. - ACTION_VIEW content://contacts/ people/ Display a list of people, which the user can browse through. Selecting a particular person to view would result in a new intent. ACTION_EDIT content://contacts/people/2 Edit information about the contact person whose identifier is "2". ACTION_PICK Launches a sub-activity that lets you pick an item from the Content Provider specified by the Intent URI. When closed it should return a URI to the item that was picked. ACTION_SENDTO smsto: 1234567 Launches an Activity to send a message to the contact specified by the Intent URI. Implicit Intents Examples Intent i = new Intent(Intent.ACTION_DIAL,Uri.parse("tel:+123456789")); startactivity(i); Intent i = new Intent(Intent.ACTION_VIEW,Uri.parse("http://www.google.com")); startactivity(i); 3

Lab Work 1 Create an interface as the figure below shows. When the user enter phone number and click Call button, It calls the number in the EditText. (This will need permission; permissions are clarified at the next page) 4

Permission Permission is a restriction limiting access to a part of the code or to data on the device. The limitation is imposed to protect critical data and code that could be misused to distort or damage the user experience. For example, here are some of the permissions defined by Android: android.permission.call_emergency_numbers android.permission.call_phone android.permission.read_owner_data android.permission.set_wallpaper android.permission.device_power android.permission.internet How to make it 5

<uses-permission> Requests a permission that the application must be granted in order for it to operate correctly. Permissions are granted by the user when the application is installed, not while it's running. <permission> Declares a security permission that can be used to limit access to specific components or features of this or other applications. 6

Intents used to: Launch an Activity (explicitly) Intent i = new Intent (CurrentActivity.this, DestinationActivity.class); startactivity(i); (All activities must be added to the AndroidManifest.xml File) - Data-passing mechanism between activities (explicitly) To share primitive data between Activities/Services in an application, use Intent.putExtra (key,value), getextra(key) is used to extract data from the intent. First Activity Intent i = new Intent(First.this,Second.class); i.putextra("msg", "hi"); startactivity(i); Second Activity Intent i = this.getintent(); String x = i.getstringextra("msg"); putexrta can take String, Integer, Double, Boolean, IntArray and other types. Extra.. Using Bundle - The Android Bundle container is a simple mechanism used to pass data between co-operating activities. It is a type-safe MAP collection of <key, value> pairs. - There is a set of putxxx and getxxx methods to store and retrieve (single and array) values of primitive data types from/to the bundles. 7

- The code using an additional bundle is slightly heavier(it won't make any difference in any practical application) and slightly easier to manage, being more general(if you decide that - before sending information inside an intent - you want to serialize the data to database - it will be a bit cleaner to have a bundle that you can serialize, add to intent and then feed to a Bundle all in one object) Example First Activity Second Activity Intent i= new Intent(First.this, Second.class); Bundle extras = new Bundle(); extras.putstring("username","name"); extras.putint("password","pass"); int[] code={1,2,3}; extras.putintarray("code",code); extras.putboolean("check",true); intent.putextras(extras); startactivity(intent); Bundle extras = getintent().getextras(); String name = extras.getstring("username "); int password = extras.getint("password"); int [] code = extras.getintarray("code"); boolean check = extras.getboolean("check"); Note: In order to get the array at a correct shape : String array2str=" " for (int m=0; m<code.length; m++) { array2str = array2str + "," + Integer.toString( code[m] ); } 8

Lab Work 2 Create an interface as the figure below shows. - User fills the two fields and checks the checkbox, then click Save in the first activity: - The second activity must get the entered data in first activity and its interface appears by this way: Your Name is (from first activity) Your ID is ((from first activity)) Accept rules checkbox status is (from first activity) (Using textview/s) Hint: At first: i.putextra("id", 22446688); At second: int y i.putextra("accept", true); = i.getintextra("id",-1); << -1 : default value if null is returned >> boolean z = i.getbooleanextra("accept",false); << false : Default value >> 9

Starting an Activity for a result - In order to get results back from the called activity we use the method startactivityforresult ( Intent, requestcodeid ) - Where requestcodeid is an arbitrary value you choose to identify the call. (similar to a nickname ). - The result sent by the sub-activity could be picked up through the listener-like asynchronous method onactivityresult ( requestcodeid, resultcode, Intent ) - Before an invoked activity exits, it can call setresult(resultcode) to return a termination signal back to its parent. FirstActivity final public static int SECOND_ACTIVITY_ID= 2; Intent i= new Intent (FirstActivity.this, SecondActivity.class) startactivityforresult(i, SECOND_ACTIVITY_ID); @Override protected void onactivityresult(int requestcode, int resultcode, Intent data) { // TODO Auto-generated method stub super.onactivityresult(requestcode, resultcode, data); switch (requestcode) { case SECOND_ACTIVITY_ID: if(resultcode==activity.result_ok){ String name = data.getstringextra("username"); break; }else if (resultcode==activity.result_canceled){ //Do Something } // if there are more one activity that we want to startactivityforresult // for it, many cases will be added here 10 with different activity numbers give above }

SecondActivity Intent i = new Intent(); data.putextra("username","myname"); setresult(result_ok, i); finish(); Lab Work 3 Go back to lab work 2, add button and textview/s as the figure below shows. When user click More the following interface will appear, user fill the fields, clicks Save, entered data will be returned back to the main interface at the textview 11

Homework Create an activity << FirstOne >>interface that contains 2buttons <<Review>> & << Let s Go >> and textview/s for << Result >> Create another three activities (classes) at the same application - 1 st : MainTabActivity - 2 nd : MessageTab : its interface contains: 1. edittext to enter phone number that you will send msg for it, 2. button << view >> - 3 rd : WebTab : its interface contains: 1. edittext to enter the website link that you will want to view, 2. button < finish >> 3. button << view >> - Two activities (MessageTab and WebTab) will be Tabbed. (Every tab represents activity, MainTabActivity will be the main container activity that contain the tabhost) Scenario will be as the following: FirstOne interface appear. When user clicks Let s Go: 1. MainTabActivity will be started for result, it will show MessageTab, WebTab in tabs, user will work with both of activities.(send msg, view web :on view button click) 2. MainTabActivity will return the entered data from them *** (on finish button click), and it will return these result to FirstOne. 3. Result will appear at FirstOne on textviews. When user clicks Review, interface at labwork2 will appear, entered data will be returned at the textview at FirstOne: *** Msg sent successfully to: (from returned result 1) The following website opened successfully: (from returned result 1) Your Name is: (from returned result 2) Your ID is: (from returned result 2) Accept rules checkbox status is: 12 (from returned result 2)

Tabs Sometimes, we want to wrap multiple views in a single window and navigate through them with a Tab Container. This can be done in Android using TabHost control. There are two ways to use a TabHost application in Android: 1. Using the TabHost to navigate through multiple views within the same activity. 2. Using the TabHost to navigate through Actual multiple Activities using intents (wanted!) Anatomy of Tabbed Application An activity with a TabHost may look like this: The Activity consists of: 1. A TabHost: The root element of the layout 2. The TabHost wraps a TabWidget which represents the tab bar. 3. The TabHost wraps a FrameLayout which wraps the contents of each tab. 13

- What if we have multiple Activities in our application and we want to navigate between them using tabs; In this case, we will have one activity as the root activity of the application. - This activity will have the TabHost and will navigate to other activities using Intents. Note: The root activity must inherit from TabActivity. The root activity will have layout file like this: <?xml version="1.0" encoding="utf-8"?> <TabHost android:layout_width="fill_parent" android:layout_height="fill_parent" android:id="@android:id/tabhost" xmlns:android="http://schemas.android.com/apk/res/android" > <TabWidget android:layout_width="fill_parent" android:layout_height="wrap_content" android:id="@android:id/tabs" /> <FrameLayout android:layout_width="fill_parent" android:layout_height="fill_parent" android:id="@android:id/tabcontent" > </FrameLayout> </TabHost> 14

In order to view two tabs, the foloowing code is used import android.os.bundle; import android.app.activity; import android.app.tabactivity; import android.content.intent; import android.widget.tabhost; public class TaActivity extends TabActivity { @Override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_tab); TabHost tabhost=gettabhost(); TabHost.TabSpec spec; Intent i1=new Intent(TaActivity.this,one.class); spec=tabhost.newtabspec("tab1").setindicator("tab1").setcontent(i1); tabhost.addtab(spec); Intent i2=new Intent(TaActivity.this,two.class); spec=tabhost.newtabspec("tab2").setindicator("tab2").setcontent(i2); tabhost.addtab(spec); tabhost.setcurrenttab(0); }} 1. We create tabs using TabSpecs class. 2. We set the title of each tab using TabSpecs.setIndicator() method. 3. We set the content of each tab using TabSpecs.setContent() method. 15

Important note. At your homework: 1. MainTabActivity interface will be typically like the xml file above. 2. Code will be written within MainTabActivity. 3. Tabs will have interfaces as specified before. 4. Change application theme into Theme.Black.NoTitleBar. 5. You can choose an icon photo for every tab (optional). In order to see an example of how to set the contents of tabs by specifying multiple layout resources to be displayed within the same activity, visit the following link (optional) http://www.codeproject.com/articles/107693/tabbed-applications-in-android SEE YOU AT THE NEXT LAB 16