Android Programming Lecture 9

Slides:



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

Google Android Introduction to Mobile Computing. Android is part of the build a better phone process Open Handset Alliance produces Android Comprises.
Application Fundamentals Android Development. Announcements Posting in D2L Tutorials.
Cosc 5/4730 Android Services. What is a service? From android developer web pages: Most confusion about the Service class actually revolves around what.
Android 02: Activities David Meredith
All About Android Introduction to Android 1. Creating a New App “These aren’t the droids we’re looking for.” Obi-wan Kenobi 1. Bring up Eclipse. 2. Click.
Android 101 Application Fundamentals January 29, 2010.
Mobile Application Development
@2011 Mihail L. Sichitiu1 Android Introduction Application Fundamentals.
Emerging Platform#4: Android Bina Ramamurthy.  Android is an Operating system.  Android is an emerging platform for mobile devices.  Initially developed.
Chien-Chung Shen Manifest and Activity Chien-Chung Shen
Mobile Application Development with ANDROID Tejas Lagvankar UMBC 29 April 2009.
About me Yichuan Wang Android Basics Credit goes to Google and UMBC.
© Keren Kalif Intro to Android Development Written by Keren Kalif, Edited by Liron Blecher Contains slides from Google I/O presentation.
Software Architecture of Android Yaodong Bi, Ph.D. Department of Computing Sciences University of Scranton.
@2011 Mihail L. Sichitiu1 Android Introduction Platform Overview.
Mobile Computing Lecture#08 IntentFilters & BroadcastReceivers.
Android Info mostly based on Pro Android 3.  User Applications  Java Libraries – most of Java standard edition ◦ Activities/Services ◦ UI/Graphics/View.
01. Introduction to Android Prof. Oum Saokosal Master of Engineering in Information Systems, South Korea
Cosc 5/4730 Introduction: Threads, Android Activities, and MVC.
Rajab Davudov. Agenda Eclipse, ADT and Android SDK APK file Fundamentals – Activity – Service – Content Provider – Broadcast Receiver – Intent Hello World.
Cosc 5/4730 Broadcast Receiver. Broadcast receiver A broadcast receiver (short receiver) – is an Android component which allows you to register for system.
Android ICC Part II Inter-component communication.
This work is licensed under the Creative Commons Attribution 4.0 International License. To view a copy of this license, visit
Mobile Application Development using Android Lecture 2.
DUE Hello World on the Android Platform.
CS378 - Mobile Computing Intents.
16 Services and Broadcast Receivers CSNB544 Mobile Application Development Thanks to Utexas Austin.
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.
ANDROID L. Grewe Components  Java Standard Development Kit (JDK) (download) (latest version)  AndroidStudio.
Working in the Background Radan Ganchev Astea Solutions.
Applications with Multiple Activities. Most applications will have more than one activity. The main activity is started when the application is started.
Services Background operating component without a visual interface Running in the background indefinitely Differently from Activity, Service in Android.
Lecture 2: Android Concepts
Services. What is a Service? A Service is not a separate process. A Service is not a thread. A Service itself is actually very simple, providing two main.
CS371m - Mobile Computing Intents 1. Allow us to use applications and components that are already part of Android System – start activities – start services.
Introduction to Android Programming
Android Application -Architecture.
Lecture 2: Android Concepts
Android 01: Fundamentals
Android Application Development 1 6 May 2018
Broadcast receivers.
CS371m - Mobile Computing Services and Broadcast Receivers
Lecture 2 Zablon Ochomo Android Programming Lecture 2 Zablon Ochomo
Lecture 7: Service Topics: Services, Playing Media.
Instructor: Mazhar Hussain
MAD.
Android Mobile Application Development
Reactive Android Development
Broadcast Receivers A Android Component where you can register for system or application events, receive and react to broadcast intent. Each broadcast.
Android Application Development android.cs.uchicago.edu
Developing Android Services
Microsoft Windows 2000 Professional
CS5103 Software Engineering
Application Fundamentals
Application Development A Tutorial Driven Course
Android Topics Android Activity Lifecycle and Experiment Toast
Android Topics UI Thread and Limited processing resources
Activities and Intents
Emerging Platform#3 Android & Programming an App
Lecture 7: Service Topics: Services, Broadcast Receiver, Playing Media.
Application Fundamentals
Lecture 2: Android Concepts
Android Overview.
Mobile Programming Dr. Mohsin Ali Memon.
Android Development Tools
Activities, Fragments, and Intents
CIS 470 Mobile App Development
Mobile Programming Broadcast Receivers.
Presentation transcript:

Android Programming Lecture 9 Testing notes Service and Broadcast Receiver

Sample App: Music Player

Sample App (1/7) Click on the icon and get the startup Launcher activity is started by the sending of a special intent – the Launcher intent Application Manifest declares which Java class accepts the launcher intent

Sample App (2/7) Left side is a menu for different views The activity has a few fragments (sub- activity) for navigation Youtube tutorial Click on My Library

Sample App (3/7) Click Albums to get into the Albums sub-activity (fragment) Tab UI can be easily achieved using Tab Host ViewGroup Click an album name

Sample App (4/7) Shows the album in a different activity Click on a track for immediate playback Click adds it to the task bar Launches a service to play Click on the back button to return to the previous activity Android maintains a stack of activities for “back” Refer to: http://developer.android.com/guide/components/tasks-and-back-stack.html

Sample App (5/7) Playback continues, even if the user navigates away Even if the user locks the phone, as long as the user does not shut down the app

Sample App (6/7) Service can be controlled by a widget when locked Also from the notification bar By clicking the notification bar, we can get back to the music player

Sample App (7/7) Service interrupted when a call come A Broadcast Event that all the (registered) apps would receive! Resumes after the call hangs up

App Components Activity Intent Service ContentProvider Application= Set of Android Components Intent Activity UI Component typically corresponding to one screen. BroadcastReceiver Responds to notifications or status changes. Can wake up your process. Service Faceless task that runs in the background. ContentProvider Enable applications to share data.

App Components Activities Application= Set of Android Components Provides User Interface Usually represents a Single Screen Can contain one/more Views Extends the Activity Base class

Connected with Intents App Components Application= Set of Android Components Activities Provides User Interface Usually represents a Single Screen Can contain one/more Views Extends the Activity Base class Services No User Interface Runs in Background to handle long running operations Connected with Intents Faceless task that runs in the background for long-running operations

Intent/Broadcast Receiver App Components Application= Set of Android Components Activities Provides User Interface Usually represents a Single Screen Can contain one/more Views Extends the Activity Base class Connected with Intents Responds to notifications /status changes/events Incoming phone call Battery low Alarm … Intent/Broadcast Receiver Receives and Reacts to broadcast Intents No UI but can start an Activity

Connected with Intents App Components Application= Set of Android Components Activities Provides User Interface Usually represents a Single Screen Can contain one/more Views Extends the Activity Base class Connected with Intents Content Provider Makes application data available to other apps Data stored in SQLite database Enable applications to share data Retrieve icon, songs from other music app Retrieve contact books to recommend music to friends

Intent/Broadcast Receiver App Components Application= Set of Android Components Activities Provides User Interface Usually represents a Single Screen Can contain one/more Views Extends the Activity Base class Services No User Interface Runs in Background to handle long running operations Connected with Intents Intent/Broadcast Receiver Receives and Reacts to broadcast Intents No UI but can start an Activity Content Provider Makes application data available to other apps Data stored in SQLite database

Service

Service An application component that can perform long- running operations in the background and does not provide a user interface Load music to play Download, decode and show an email Download a html from Internet to display Why Service? Put the time-consuming job in the background

What a good should provide? Desirable Functions Attractive UI Good user experience Quick response and reliable (never crash) Secure and safe to use

Using Service Step 1: Create custom class extended from Service class Reason to use custom class We need to override the callback functions to specify the behavior of app

Using Service Step 2: Use the custom in the Activity to launch service public class MainActivity extends Activity { ... // Inside the method where you need to launch the service // Create the service object MyService myService = new MyService(); // Create the Intent for launching the service Intent intent = new Intent(MainActivity.this, MediaPlayerService.class); // Call startService() method to start the service MainActivity.this.startService(intent); MainActivity.this.stopService(intent); }

Using Service Step 3: Register the Service class in AndroidManifest.xml <manifest . . . > . . . <application . . .> <service android:name=".MyService" > </service> </application> </manifest>

Lifecycle of Service Android provides different callback functions at different states of a service Developer can override the methods to specify the app behaviors like what we do in Activity Android provides two type of services: Started services Bound services

MainActivity.this.startService(intent); MainActivity.this.stopService(intent);

Bound Service A bound service offers a client-server interface that allows components to interact with the service Using custom class Using a messenger Using AIDL (Android Interface Definition Language) http://developer.android.com/guide/components/bound-services.html

Broadcast Receiver

Broadcast Receiver Responds to system-wide Broadcast announcements. Many broadcasts originate from the system—for example, screen has turned off, the battery is low, or a picture was captured or an SMS is received. Applications can also initiate broadcasts. Although broadcast receivers don't display a user interface, they may create a status bar notification to alert the user when a broadcast event occurs.

Using Broadcast Receiver Step 1: Create custom class extended from the Broadcast Receiver class public class MyReciever extends BroadcastReceiver { // callback function called when the broadcast event is captured @Override public void onReceive(Context context, Intent intent) { // Override the method } Reason to use custom class We need to override the callback functions to specify the behavior of app

Using Broadcast Receiver Step 2: Register as the broadcast receiver in Java or declare in AndroidManefist.xml public class MainActivity extends Activity { . . . // Create an IntentFilter object (in Java) IntentFilter filter = new IntentFilter(); // Specify the Action of the Intent Filter filter.addAction(Intent.ACTION_CAMERA_BUTTON); // Create the receiver object reciever = new MyReciever(MainActivity.this); // Register the receiver MainActivity.this.registerReceiver(reciever, filter); // Unregister Register the receiver MainActivity.this.unregisterReceiver(reciever); }

Using Broadcast Receiver Step 2: Register as the broadcast receiver in Java or declare in AndroidManefist.xml <manifest . . . > . . . <application . . .> <receiver android:name=".MyReceiver" > </receiver> </application> </manifest>

Summary What is an Activity? What is a Service? What is a Broadcast Receiver? What is a Content Provider? How to enable a service inside an activity? How to enable a broadcast receiver inside an activity?

Recommended Reading Vogella tutorial on Services, BroadcastReceiver and NotificationManager http://www.vogella.com/tutorials/android.html Android Developer Site: Services: http://developer.android.com/guide/components/servi ces.html Content Provider: http://developer.android.com/guide/topics/providers/c ontent-providers.html