android tutorial activities and services

Post on 10-May-2015

361 Views

Category:

Software

0 Downloads

Preview:

Click to see full reader

DESCRIPTION

Some Android Basics Android Activity Basics Android’s chief UI component Android Service Basics Android’s chief background service component Android Intent Basics Android’s means to communicate between components Wrap up and Q/A To include list of resources Android apps are written in Java Can also be written in C/C++ Android Java apps run on a virtual machine Called the Dalvik Virtual Machine (DVM) Android OS manages the start/stop of the DVM. Java code is compiled into byte code held in a .dex file Apps are bundled into an .apk file (fancy ZIP file) for deployment to the device By default, all components of an app run in one DVM By default, all components execute in the same thread of that process

TRANSCRIPT

Mobile Twin CitiesAndroid Tutorial on Activities/Services

An Intertech, Inc. Presentation

Copyright © Intertech, Inc. • www.Intertech.com • 800-866-9884 Slide 2

IntertechInstructors Who Consult, Consultants Who Teach

Training• Training through hands-on,

real world business examples.

• Our site, your site, or live online – globally.

• Agile/Scrum, Citrix, VMware, Oracle, IBM, Microsoft, Java/Open Source, and web and mobile technologies.

Consulting Design and develop software

that powers businesses and governments of all sizes.

On-site consulting, outsourcing, and mentoring.

Agile, .NET, Java, SQL Server, mobile development including iPhone and Android platforms and more….

Our Company Over 35 awards for growth,

innovation and workplace best practices.

99.7% satisfaction score from our consulting and training customers.

Yearly “Best Places to Work” winner in Minnesota.

An Intertech, Inc. Presentation

Copyright © Intertech, Inc. • www.Intertech.com • 800-866-9884 Slide 3

An Intertech, Inc. Presentation

Copyright © Intertech, Inc. • www.Intertech.com • 800-866-9884 Slide 4

My Agenda

• Some Android Basics

• Android Activity Basics

• Android’s chief UI component

• Android Service Basics

• Android’s chief background service component

• Android Intent Basics

• Android’s means to communicate between components

• Wrap up and Q/A

• To include list of resources

Your Agenda

But – I can

take this in

just about any

direction you

would like

An Intertech, Inc. Presentation

Copyright © Intertech, Inc. • www.Intertech.com • 800-866-9884 Slide 5

Some Android Basics

• Android apps are written in Java

• Can also be written in C/C++

• Android Java apps run on a virtual machine

• Called the Dalvik Virtual Machine (DVM)

• Android OS manages the start/stop of the DVM.

• Java code is compiled into byte code held in a .dex file

• Apps are bundled into an .apk file (fancy ZIP file) for deployment to the device

• By default, all components of an app run in one DVM

• By default, all components execute in the same thread of that process

Linux Kernel

DVM Process

Application

.APK

An Intertech, Inc. Presentation

Copyright © Intertech, Inc. • www.Intertech.com • 800-866-9884 Slide 6

• Android applications are really a loose affiliation of components.

• Android applications have no single entry point; no beginning or end to speak of

• Components defined in one “application” can be used by another application.

• Android applications are comprised of one or more of the following “building block” components:

• Activities

• Services

• Content Providers

• Broadcast Receivers

• All these must be registered an the AndroidManifiest.xml.

• In addition, there are a number of components used by these main components

• Intents

• Notifications

• AsyncTask

Android Apps

An Intertech, Inc. Presentation

Copyright © Intertech, Inc. • www.Intertech.com • 800-866-9884 Slide 7

Activities

• Activities are visual user interface components

• Think “screen”

• Most applications will have one or more activities

• In most applications, the “first” activity gets opened when the application launches

• Alive per user needs or as Android sees fit (ex: removed for incoming call, to allow another app to run, etc.)

• Activities contain Views (and View Groups)

• Think “widgets”

• ViewGroups organize and layout children View components• ViewGroups can have children View components

• An activity’s Views/ViewGroups can be specified by Java code or XML

An Intertech, Inc. Presentation

Copyright © Intertech, Inc. • www.Intertech.com • 800-866-9884 Slide 8

Building an Activity

• Extend android.app.Activity

• Minimally override onCreate( )

• Method creates and displays the activity

import android.app.Activity;

public class MyActivity extends Activity

{

@Override

public void onCreate(Bundle savedInstanceState)

{

super.onCreate(savedInstanceState);

setContentView(R.layout.main);

}

}

An Intertech, Inc. Presentation

Copyright © Intertech, Inc. • www.Intertech.com • 800-866-9884 Slide 9

Setting the Content of an Activity

• setContentView( ) provides “guts” to the activity.

• Typically called from onCreate( ).

• Sets the View root - an activity can have one root View.

• ViewGroups are Views (allows a collection of widgets to be displayed)

• The content can be set with an instance of View.

• Using the Android/Java API to create root View.

• The content can be set via XML resource file (layout file)

• The XML specifies the View component(s).

An Intertech, Inc. Presentation

Copyright © Intertech, Inc. • www.Intertech.com • 800-866-9884 Slide 10

Android View/ViewGroups (a.k.a. “Widgets”)

• “Widget” toolbox

• Views

• TextView

• EditText

• ListView

• Spinner

• Button

• Checkbox

• RadioButton

• ViewGroups = layout

• TableLayout

• RelativeLayout

• Etc.

An Intertech, Inc. Presentation

Copyright © Intertech, Inc. • www.Intertech.com • 800-866-9884 Slide 11

Loose Android – AWT/Swing Translation

Android UI Component Swing Component

Activity (J)Frame

View (J)Component

TextView (J)Label

EditText (J)TextField

Button (J)Button

Checkbox (J)Checkbox

RadioButton JRadioButton

ViewGroup

LinearLayout

TableLayout

LayoutManager

BoxLayout or FlowLayout

GridLayout

An Intertech, Inc. Presentation

Copyright © Intertech, Inc. • www.Intertech.com • 800-866-9884 Slide 12

Setting Content Programmatically

• Create a View or ViewGroup instance.

• Organize the widgets

• Configure the widgets

• Set the content of the activity with the root view

public void onCreate(Bundle savedInstanceState)

{

super.onCreate(savedInstanceState);

TableLayout layout = new TableLayout(this);

layout.setStretchAllColumns(true);

TableRow rowHeader = new TableRow(this);

layout.addView(rowHeader);

ImageView image = new ImageView(this);

Image.setImageResource(R.drawable.itech);

TextView title = new TextView(this);

title.setText("Reversy");

title.setTextSize(30f);

rowHeader.addView(image);

rowHeader.addView(title);

TableRow row1 = new TableRow(this);

TextView nameLabel = new TextView(this);

nameLabel.setText(“String:");

EditText nameEdit = new EditText(this);

row1.addView(nameLabel);

row1.addView(nameEdit);

layout.addView(row1);

setContentView(layout);

}

An Intertech, Inc. Presentation

Copyright © Intertech, Inc. • www.Intertech.com • 800-866-9884 Slide 13

Setting Content via Resource File

• Establish a layout resource file defining the View/ViewGroupsto display

• XML file located in /res/layout

• Organize widgets via XML elements

• Configure widgets via XML

• Set the content of the activity with referenced layout file

<TableLayout xmlns:android=

http://schemas.android.com/apk/res/android

android:layout_width="fill_parent”

android:layout_height="fill_parent"

android:stretchColumns="1">

<TableRow>

<ImageView android:id="@+id/itechicon"

android:src="@drawable/itech"

android:layout_width="wrap_content"

android:layout_height="wrap_content">

</ImageView>

<TextView android:text="Reversy" android:textSize="30sp"

android:layout_gravity="center_horizontal" />

</TableRow>

<TableRow>

<TextView android:text="String:" android:textSize="20sp"/>

<EditText android:id="@+id/datastring" />

</TableRow>

<Button android:layout_height="wrap_content"

android:text="Reverse it”

android:layout_width="wrap_content"

android:id="@+id/reverse“

android:layout_alignParentRight="true"

android:onClick="onReverse" />

</TableLayout>

public void onCreate(Bundle savedInstanceState)

{

super.onCreate(savedInstanceState);

setContentView(R.layout.main);

}

An Intertech, Inc. Presentation

Copyright © Intertech, Inc. • www.Intertech.com • 800-866-9884 Slide 14

Register the Activity in the Manifest

• Android components must be registered.

• One activity can be designated as the activity to be launched when the app starts.

• Called the “main launcher” activity

• Designated by intent filter (more on intents in a bit)

<?xml version="1.0" encoding="utf-8"?>

<manifest xmlns:android="http://schemas.android.com/apk/res/android"

package="com.intertech.welcomemat.activities" android:versionCode="1” android:versionName="1.0">

<application android:label="@string/app_name" android:icon="@drawable/itech">

<activity android:label="@string/app_name" android:name=“MyActivity">

<intent-filter>

<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />

</intent-filter>

</activity>

</application>

<uses-sdk android:minSdkVersion="9" />

</manifest>

An Intertech, Inc. Presentation

Copyright © Intertech, Inc. • www.Intertech.com • 800-866-9884 Slide 15

Active Activity

• Only one activity is “active” in Android at a time.

• Android maintains a “stack” of executing activities.

• When all or part of an activity becomes obscured, it moves lower in the stack.

• To conserve CPU, memory, etc. Android may need to pause or stop activities (and their DVM)

• Typically done with activities lower in the stack.

• The activity lifecycle methods allow your code to react

Activity A

Activity B

Activity C

An Intertech, Inc. Presentation

Copyright © Intertech, Inc. • www.Intertech.com • 800-866-9884 Slide 16

Activity Lifecycle

• Android invokes various lifecycle methods as activity is displayed/hidden/destroyed

• These callback methods allow your app to…

• Manage resources

• Store/restore app state

• Store/restore screen data

• Add complexity to activity development.

An Intertech, Inc. Presentation

Copyright © Intertech, Inc. • www.Intertech.com • 800-866-9884 Slide 17

Screen Orientation Change

• A screen orientation change causes activities to be

• Destroyed

• Recreated

• i.e. - go through the entire activity lifecycle

• Requires work to capture/maintain state between activity instances

An Intertech, Inc. Presentation

Copyright © Intertech, Inc. • www.Intertech.com • 800-866-9884 Slide 18

Services

• Services are background processes

• Think “cron job” or “Windows scheduled task”

• No direct visual representation or user interaction

• Examples: play your music or check on updates to a feed

• Often used to schedule tasks

• Alive for as long as they have to be

• Can be killed, but Android will remember to restart them if desired

• Android gives them a higher priority than inactive Activities

• Typically launched from the UI (activity)

• But can be launched from the start of the application

Services are considered

one of the real

strengths/selling point

of Android

An Intertech, Inc. Presentation

Copyright © Intertech, Inc. • www.Intertech.com • 800-866-9884 Slide 19

Creating a Service

• Extend android.app.Service

• Minimally override onBind( )

• onBind( ) – provides a persistent connection to a service.

• For tight coupling to something like an activity

• In most cases, also implement the onCreate and onStartCommand( )

• onCreate( ) - called when the service is created

• onStartCommand - called when the service is started.

• Where the work of the service gets kicked off.

public class MyService extends Service {

@Override

public void onCreate() {

super.onCreate();

// … initialize here

}

@Override

public IBinder onBind(Intent arg0) {

return null;

}

@Override

public int onStartCommand(Intent intent, int flags, int

startId) {

// … do work of the service here

]

}

An Intertech, Inc. Presentation

Copyright © Intertech, Inc. • www.Intertech.com • 800-866-9884 Slide 20

Register the Service in the Manifest

<?xml version="1.0" encoding="utf-8"?>

<manifest xmlns:android="http://schemas.android.com/apk/res/android"

package="com.intertech.reversy.activities" android:versionCode="1"

android:versionName="1.0">

<application android:label="@string/app_name" android:icon="@drawable/itech">

<service android:label=“MyService" android:name="com.intertech.MyService“/>

</application>

<uses-sdk android:minSdkVersion="9" />

</manifest>

An Intertech, Inc. Presentation

Copyright © Intertech, Inc. • www.Intertech.com • 800-866-9884 Slide 21

Comic half time

An Intertech, Inc. Presentation

Copyright © Intertech, Inc. • www.Intertech.com • 800-866-9884 Slide 22

Intents

• Intents are system messages/event carriers/announcements

• Used to communicate between components (in any app)

• Examples

• Way for one activity to call to another activity to display itself

• Way to trigger a service

• Way to request the device make a phone call or play music

• Android controls the delivery

• Intents = action + context or data

• Not unlike how HTTP works (GET http://...)

• Example intent action: VIEW or EDIT

• Example intent context/data: Activity name or URI of a contact

• Two types of intents: explicit and implicit

An Intertech, Inc. Presentation

Copyright © Intertech, Inc. • www.Intertech.com • 800-866-9884 Slide 23

Intents == Messages

• Intents don’t do anything on their own.

• They can cause other components to trigger.

• They alone are just carriers of information.

Addressee (who gets the

message)

Used by the system to get

the message to the right

addressee

Contents: The data to be

used by the addressee

An Intertech, Inc. Presentation

Copyright © Intertech, Inc. • www.Intertech.com • 800-866-9884 Slide 24

To: Duane

From: Ben

Duane, can you get me a list of

customers that have purchased in

the last 90 days?

Explicit Intent

Input data

Expected output data

An Intertech, Inc. Presentation

Copyright © Intertech, Inc. • www.Intertech.com • 800-866-9884 Slide 25

To: All staff

From: Ben

I need a list of customers

who have purchased in

the last 90 days

Implicit Intent

Input data

Expected output data

An Intertech, Inc. Presentation

Copyright © Intertech, Inc. • www.Intertech.com • 800-866-9884 Slide 26

Types of Intents

• Explicit

• Specify a class by name to load and do something

• “Send a message to X!”

• Optionally include data in intent that X will need to do something

• Implicit

• Specify an action and data

• “I would like to do X with Y, can anyone help?”

• “Anyone” is some other component

• The component might be in the same or different app

• Optionally include data in intent that “Anyone” will need in order to help

An Intertech, Inc. Presentation

Copyright © Intertech, Inc. • www.Intertech.com • 800-866-9884 Slide 27

Start by Intent - Explicit

• Start a service via intent

• Start an activity by intent

Intent serviceIntent = new Intent(this, MyService.class);

serviceIntent.putExtra("data",”my data string”);

startService(serviceIntent);

Intent activityIntent = new Intent(this, MyActivity.class);

activityIntent.putExtra("data",”my data string”);

startActivity(activityIntent);

An Intertech, Inc. Presentation

Copyright © Intertech, Inc. • www.Intertech.com • 800-866-9884 Slide 28

Start by Intent - Implicit

• Make a phone call

• To play a music file

String callNumber = "111-333-222-4";

String uri = "tel:" + callNumber.trim() ;

Intent intent = new Intent(Intent.ACTION_CALL);

intent.setData(Uri.parse(uri));

startActivity(intent);

Uri data = Uri.parse("file:///mnt/sdcard/abc_xyz.mp3");

Intent intent = new Intent(Intent.ACTION_VIEW);

intent.setDataAndType(data, "audio/*");

startActivity(intent);

An Intertech, Inc. Presentation

Copyright © Intertech, Inc. • www.Intertech.com • 800-866-9884 Slide 29

Android Intents (Built-in)

• Android comes with several Intent actions

• Examples

• ACTION_DIAL

• ACTION_EDIT

• ACTION_VIEW

• ACTION_BATTERY_LOW

• Android comes with several built-in data URI

• Examples

• tel:555-555-1212

• content://contacts/people/10 – for a PMI contact

• http://www.intertech.com

An Intertech, Inc. Presentation

Copyright © Intertech, Inc. • www.Intertech.com • 800-866-9884 Slide 30

Intent Filters

• Your application components can react to implicit Intents

• Components simply need to register in the manifest file for the intent by…

• Action

• Data/type

• Category

• All of the above

• Think you have the worlds best music playing service?

• Register your service to play music

• User will pick which music player is used on the device when there are many.

<service android:label="ReverseService" android:name="com.intertech.MyService">

<intent-filter>

<action android:name= "android.content.Intent.ACTION_VIEW" />

<category android:name= “android.app.category.CATEGORY_APP_MUSIC” />

<data android:mimeType=“audio/*" />

</intent-filter>

</service>

An Intertech, Inc. Presentation

Copyright © Intertech, Inc. • www.Intertech.com • 800-866-9884 Slide 31

And now a Demo

Count

upper/lower case

characters

serviceData entry

activity

Results activity

An Intertech, Inc. Presentation

Copyright © Intertech, Inc. • www.Intertech.com • 800-866-9884 Slide 32

Associated Courses and Resources

• Complete Android Training

• Complete IPhone and IPad IOS 7 TRAINING

• Complete HTML5 Mobile Training

Intertech Android Article

• Jim’s email - jwhite@intertech.com

Questions

And some answers??

Thanks for having me!

• Intertech Blog site

• www.intertech.com\blog

top related