appforum2014 helping the developer community build next-generation, multi-platform apps. schaumburg,...

44
APPFORUM2014 Helping the developer community build next-generation, multi-platform apps. SCHAUMBURG, ILLINOIS | SEPTEMBER 8-10

Upload: ashlynn-fletcher

Post on 22-Dec-2015

216 views

Category:

Documents


2 download

TRANSCRIPT

Page 1: APPFORUM2014 Helping the developer community build next-generation, multi-platform apps. SCHAUMBURG, ILLINOIS | SEPTEMBER 8-10

APPFORUM2014Helping the developer community build next-generation, multi-platform apps.

SCHAUMBURG, ILLINOIS | SEPTEMBER 8-10

Page 2: APPFORUM2014 Helping the developer community build next-generation, multi-platform apps. SCHAUMBURG, ILLINOIS | SEPTEMBER 8-10

APPFORUM2014

JOHN SEIMERGLOBAL SOLUTION CENTERS

ANDROID APPLICATION FUNDAMENTALS

Page 3: APPFORUM2014 Helping the developer community build next-generation, multi-platform apps. SCHAUMBURG, ILLINOIS | SEPTEMBER 8-10

Agenda

Android application structure – crash course on application components– Activity – Services – Intents – BroadcastReceiver – ContentProvider

Processes and Threading

Data Storage

Page 4: APPFORUM2014 Helping the developer community build next-generation, multi-platform apps. SCHAUMBURG, ILLINOIS | SEPTEMBER 8-10

Dalvik Virtual Machine• Dalvik Executable (.dex) format• Optimized for minimal memory

footprint• Compilation

Android Architecture

Page 5: APPFORUM2014 Helping the developer community build next-generation, multi-platform apps. SCHAUMBURG, ILLINOIS | SEPTEMBER 8-10

What is an APK FileA package containing

• .DEX files• Resources• Assets• Certificates• Manifest file

A .ZIP formatted package based on .JAR file format

Manifest File

• Declares the components and settings of an

application

• Identifies permissions (ie. network access)

• Defines the package of the app

• Defines the version

• Declares hardware and software features

Page 6: APPFORUM2014 Helping the developer community build next-generation, multi-platform apps. SCHAUMBURG, ILLINOIS | SEPTEMBER 8-10

• Activity• Service• BroadcastReceiver• ContentProvider

Activating Components• Intents

Android Application Components

Page 7: APPFORUM2014 Helping the developer community build next-generation, multi-platform apps. SCHAUMBURG, ILLINOIS | SEPTEMBER 8-10

Activity Overview

• A screen with a UI

• A typical application may have many activities

• Typically expensive and so are managed by the system

• May be shutdown by the system

Page 8: APPFORUM2014 Helping the developer community build next-generation, multi-platform apps. SCHAUMBURG, ILLINOIS | SEPTEMBER 8-10

• Activities are managed by the

system’s Activity Manager.

• Applications show new screens

by pushing a new activity onto

the screen. Navigating back (via

back button) causes the top

screen to be popped off the

stack.

• You define what happens in each

callback.

Activity Lifecycle

Page 9: APPFORUM2014 Helping the developer community build next-generation, multi-platform apps. SCHAUMBURG, ILLINOIS | SEPTEMBER 8-10

Activity Lifecycle Explored

Starting for the first time• onCreate • onStart• onResume

Restarting next time• onRestart• onStart• onResume

Rotating the screen• onSaveInstanceState• onPause• onStop• onDestroy• onCreate• onStart• onRestoreInstanceState• onResume

Page 10: APPFORUM2014 Helping the developer community build next-generation, multi-platform apps. SCHAUMBURG, ILLINOIS | SEPTEMBER 8-10

Activity Implementation

Page 11: APPFORUM2014 Helping the developer community build next-generation, multi-platform apps. SCHAUMBURG, ILLINOIS | SEPTEMBER 8-10

Registering the Activity

Register the activity in the manifest file: <application>... <activity

android:name=".ActivityDemo“ android:label="@string/app_name"> <intent-filter>

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

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

</activity> ... </application>The intent filter specifies that this activity is to be the main entry point into the application as well as that it should be shown in the app launcher on home screen.

Page 12: APPFORUM2014 Helping the developer community build next-generation, multi-platform apps. SCHAUMBURG, ILLINOIS | SEPTEMBER 8-10

Building an Android UI

There are 2 approaches to building Android User Interfaces:

Declaratively• Declare UI in XML• Eclipse provides nice drag-n-drop tools• Inflate the XML view in Java

Programmatically• Write Java code for the UI• Instantiate all widgets programmatically• Set properties for each

Best approach is to combine both:1. Declare the look and feel using XML2. Inflate XML into Java.3. Program the actions using Java.

Page 13: APPFORUM2014 Helping the developer community build next-generation, multi-platform apps. SCHAUMBURG, ILLINOIS | SEPTEMBER 8-10

Building an Android UI

• UI is built using a hierarchy of View and ViewGroup objects

• Android provides XML vocabulary for all View and ViewGroup objects so that UI can be defined in XML

Page 14: APPFORUM2014 Helping the developer community build next-generation, multi-platform apps. SCHAUMBURG, ILLINOIS | SEPTEMBER 8-10

Building an Android UI

Page 15: APPFORUM2014 Helping the developer community build next-generation, multi-platform apps. SCHAUMBURG, ILLINOIS | SEPTEMBER 8-10

Building an Android UI

Page 16: APPFORUM2014 Helping the developer community build next-generation, multi-platform apps. SCHAUMBURG, ILLINOIS | SEPTEMBER 8-10

Preference ActivitySpecialized activity that knows how to display, read and write application’s user preferences.

import android.os.Bundle;import android.preference.PreferenceActivity;

public class PrefsActivity extends PreferenceActivity { @Override protected void onCreate(Bundle savedInstanceState {

super.onCreate(savedInstanceState);

addPreferencesFromResource(R.xml.prefs); }

}

R.xml.prefs refers to Preference screen resource.

Page 17: APPFORUM2014 Helping the developer community build next-generation, multi-platform apps. SCHAUMBURG, ILLINOIS | SEPTEMBER 8-10

Preference Activityres/xml/prefs.xml

<?xml version="1.0" encoding="utf-8"?><PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"> <CheckBoxPreference

android:title="Background data" android:key="background_data" android:summary="Use background data?”>

</CheckBoxPreference> <ListPreference

android:key="text_size" android:entries="@array/text_size_entries“

android:entryValues="@array/text_size_values" android:title="Text Size”>

</ListPreference> </PreferenceScreen>

Page 18: APPFORUM2014 Helping the developer community build next-generation, multi-platform apps. SCHAUMBURG, ILLINOIS | SEPTEMBER 8-10

Preference Activity

Page 19: APPFORUM2014 Helping the developer community build next-generation, multi-platform apps. SCHAUMBURG, ILLINOIS | SEPTEMBER 8-10

Preference Activity

Page 20: APPFORUM2014 Helping the developer community build next-generation, multi-platform apps. SCHAUMBURG, ILLINOIS | SEPTEMBER 8-10

Intent Overview

• Intents are like events or messages.• Can be used to start activities, start/stop services, or send broadcasts. • Composed of an action that needs to be performed and the data that it

needs to perform the action on.• Can be implicit or explicit.

Page 21: APPFORUM2014 Helping the developer community build next-generation, multi-platform apps. SCHAUMBURG, ILLINOIS | SEPTEMBER 8-10

Using Intents Sample

An activity can send an intent to start another activity:

Intent I = new Intent (this, SecondActivity.class);startActivity(i);

An intent can contain data to be used by the receiving component:

String url = http://www.google.com;Intent i = new Intent (Intent.ACTION_VIEW);i.setData(Uri.parse(url));startActivity(i);

Page 22: APPFORUM2014 Helping the developer community build next-generation, multi-platform apps. SCHAUMBURG, ILLINOIS | SEPTEMBER 8-10

• Intent filter is a way for to assign certain action to an activity, service, receiver or similar.

• Action is one of system defined actions, or something developer can come up with.

• Intent filter typically goes into Android Manifest file, within <activity>, <service>, or <receiver> elements.

Android Manifest file... <intent-filter>

<action android:name="some.action.goes.here" /> </intent-filter> ...

Intent Filters

Page 23: APPFORUM2014 Helping the developer community build next-generation, multi-platform apps. SCHAUMBURG, ILLINOIS | SEPTEMBER 8-10

Explicit Intent Sample

ActivityDemo.java

... startActivity(new Intent(this, AnotherActivity.class)); ... startService(new Intent(this, ServiceDemo.class)); ...

this is the context from which this intent is being sent, in this case an Activity or Service

Page 24: APPFORUM2014 Helping the developer community build next-generation, multi-platform apps. SCHAUMBURG, ILLINOIS | SEPTEMBER 8-10

Implicit Intent Sample

ActivityDemo.java

... startService(new Intent(“MSI.intent.action.IntentServiceDemo")); ... sendBroadcast(new Intent(“MSI.intent.action.ReceiverDemo")); ...

Requires an intent filter filtering for this particular intent.

Page 25: APPFORUM2014 Helping the developer community build next-generation, multi-platform apps. SCHAUMBURG, ILLINOIS | SEPTEMBER 8-10

Service

• Service Overview • Service Lifecycle • Service Lifecycle Explored • Service Sample• Service Callbacks • Registering Service

Page 26: APPFORUM2014 Helping the developer community build next-generation, multi-platform apps. SCHAUMBURG, ILLINOIS | SEPTEMBER 8-10

• Runs in the background• Can be started and

stopped• No UI

Service Overview

Page 27: APPFORUM2014 Helping the developer community build next-generation, multi-platform apps. SCHAUMBURG, ILLINOIS | SEPTEMBER 8-10

Starting a service first time onCreate onStartCommand

Restarting the same service onStartCommand

Stopping the service onDestroy

Starting the intent service onCreate onHandleIntent onDestroy

Service Lifecycle

Page 28: APPFORUM2014 Helping the developer community build next-generation, multi-platform apps. SCHAUMBURG, ILLINOIS | SEPTEMBER 8-10

Registering a service that will be called explicitly by its class name

<application> ... <service android:name=".ServiceDemo"></service>...

</application>

Registering a service that will be called via action...<service android:name=".IntentServiceDemo">

<intent-filter> <action android:name=“MSI.intent.action.IntentServiceDemo" />

</intent-filter></service> …

Registering a Service

Page 29: APPFORUM2014 Helping the developer community build next-generation, multi-platform apps. SCHAUMBURG, ILLINOIS | SEPTEMBER 8-10

Example Service

Page 30: APPFORUM2014 Helping the developer community build next-generation, multi-platform apps. SCHAUMBURG, ILLINOIS | SEPTEMBER 8-10

• An Intent-based publish-

subscribe mechanism

• Respond to broadcast

messages from other

applications or from the

system.

• Great for listening for system

events such as SMS

messages

Broadcast Receiver Overview

Page 31: APPFORUM2014 Helping the developer community build next-generation, multi-platform apps. SCHAUMBURG, ILLINOIS | SEPTEMBER 8-10

Registering in Android Manifest file

<application> <receiver android:name=".ReceiverDemo">

<intent-filter> <action

android:name=“android.intent.action.BOOT_COMPLETED"/> </intent-filter>

</receiver></application>

Registering a Broadcast Receiver

Page 32: APPFORUM2014 Helping the developer community build next-generation, multi-platform apps. SCHAUMBURG, ILLINOIS | SEPTEMBER 8-10

• Alerts the user in the status bar• Contains the following elements:

1. Content title

2. Large icon

3. Content text

4. Content info

5. Small icon

6. Time the notification

was issued

Notification Overview

Page 33: APPFORUM2014 Helping the developer community build next-generation, multi-platform apps. SCHAUMBURG, ILLINOIS | SEPTEMBER 8-10

Data Storage

• Android provides a number of options for storing data

• SharedPreferencesStore persistent key-value pairs that can be private or publicIdeal for store preferences and other small data

• FilesLocal Storage – created in app sandbox and privateExternal Storage – Open to be read by all applications

• SQLiteAndroid provides full SQLite supportAndroid provides SQLiteOpenHelper to manage opening and closing of actual databaseIdeal for storing structured data

Page 34: APPFORUM2014 Helping the developer community build next-generation, multi-platform apps. SCHAUMBURG, ILLINOIS | SEPTEMBER 8-10

Content Provider

• Content Provider Overview• Content Provider Lifecycle • Content Provider Lifecycle Explored • Content Provider Sample• Content Provider Callbacks • Registering Content Provider

Page 35: APPFORUM2014 Helping the developer community build next-generation, multi-platform apps. SCHAUMBURG, ILLINOIS | SEPTEMBER 8-10

Content Providers share content with applications across application boundaries.

Examples of built-in Content Providers are:

• Contacts • MediaStore • Settings and more

Content Provider Overview

Page 36: APPFORUM2014 Helping the developer community build next-generation, multi-platform apps. SCHAUMBURG, ILLINOIS | SEPTEMBER 8-10

• Content provider is initiated first time it is used via a call to onCreate(). • There is no callback for cleaning up after the provider. • When modifying the data (insert/update/delete), open/close database

atomically. • When reading the data, leave database open or else the data will get garbage

collected.

Content Provider Lifecycle

Page 37: APPFORUM2014 Helping the developer community build next-generation, multi-platform apps. SCHAUMBURG, ILLINOIS | SEPTEMBER 8-10

Registering in the manifest file:

<application>... <provider android:name=".ProviderDemo“ android:authorities="com.MSI.android.lifecycle.providerdemo" />...</application>

The authority of this provider must match the URI authority that this provider is responding to.

Registering a Content Provider

Page 38: APPFORUM2014 Helping the developer community build next-generation, multi-platform apps. SCHAUMBURG, ILLINOIS | SEPTEMBER 8-10

Typical Use of Content Providers

Page 39: APPFORUM2014 Helping the developer community build next-generation, multi-platform apps. SCHAUMBURG, ILLINOIS | SEPTEMBER 8-10

Processes and Threading

• All components of the same application package run in the same process (all

activities, services, etc).

• Android system automatically creates a thread called “main”, which is usually

referred to as the “UI thread”.

• Any interaction with views (ui components) must be done from the UI thread.

• Android provides convenience classes for using background threads for long

running tasks.

Page 40: APPFORUM2014 Helping the developer community build next-generation, multi-platform apps. SCHAUMBURG, ILLINOIS | SEPTEMBER 8-10

Threading - AsyncTask

Page 41: APPFORUM2014 Helping the developer community build next-generation, multi-platform apps. SCHAUMBURG, ILLINOIS | SEPTEMBER 8-10

• Android applications are developed in Java. Decide on the API level that your

app will be supporting.• Each Android application runs in its own Application Security Sandbox• Android system can kill your activity anytime if it decides its not required. Hence

save your screen data constantly.• Your application settings can be stored/retrieved using Preference Activity.• You could register intent filters to start your activity or service from any other

app.• Services are the code that does not have UI.• Use IntentService to perform any background activity.• Broadcast Receiver listens to the system event to occur.• Use a Content Provider if shared data access is required.

Summary/Recap

Page 42: APPFORUM2014 Helping the developer community build next-generation, multi-platform apps. SCHAUMBURG, ILLINOIS | SEPTEMBER 8-10

DEVELOPMENT OPTIONS

PAGE 42

Android Java Development using EMDK• Compatible with standard Android SDK• Supports Profile management and Traditional APIs

Android Java Development using MSI Extensions • Compatible with standard Android SDK• MSI Extensions for Data Capture and more• Uses Android API (Intents) Mechanism

• Recommended when you have Android and Java knowledge

RhoMobile• Cross Platform Application development framework• Develop using Web skills (HTML5, JS, Ruby)• Same application runs on Android, Windows Mobile, iOS

Targeted for multiple device and OS development Web development skills recommended

Recommended when no application programming desired and basic text data processing required

Recommended when programmatically accessing MSI value adds is required for app development

Recommended for standard Android development

NEW

Page 43: APPFORUM2014 Helping the developer community build next-generation, multi-platform apps. SCHAUMBURG, ILLINOIS | SEPTEMBER 8-10

EMDK OFFERING

PAGE 43

EMDK

Scanning API

MSR API

PROFILES

Scanning

MSR

Fusion

Clock

Power

Battery

Profile API

Sample Apps / Code

Settings

Scanning

MSR

Eclipse IDE

Android SDK + AD

T //Get scanManager instanceScanManager scnDeviceMngr = (ScanManager)EMDKManager.getInstance(getApplicationContext(), SCANNER);//Enumerate scan devicesArrayList<Device> scnDevices = scnDeviceMngr.getDevices();//Get specified scan deviceDevice scnDevice = scnDevices.get(index);

//Enable scan devicescnDevice.enable();

//Set configConfigurationSettings config = scnDevice.getConfiguration();config.triggerMode = TriggerMode.SOFT_ONCE;config.Decoders.Code39 = ENABLE;…

Page 44: APPFORUM2014 Helping the developer community build next-generation, multi-platform apps. SCHAUMBURG, ILLINOIS | SEPTEMBER 8-10

THANK YOU