Android. Broadcasts Services Notifications

Similar documents
Services. Marco Ronchetti Università degli Studi di Trento

EMBEDDED SYSTEMS PROGRAMMING Android Services

Services are software components designed specifically to perform long background operations.

Services. Background operating component without a visual interface Running in the background indefinitely

CS378 -Mobile Computing. Services and Broadcast Receivers

MOBILE APPLICATION DEVELOPMENT LECTURE 10 SERVICES IMRAN IHSAN ASSISTANT PROFESSOR

Android Services & Local IPC: Overview of Programming Bound Services

Broadcast receivers. Marco Ronchetti Università degli Studi di Trento

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

Android Fundamentals - Part 1

Services. Marco Ronchetti Università degli Studi di Trento

Android Services. Victor Matos Cleveland State University. Services

Services & Interprocess Communication (IPC) CS 282 Principles of Operating Systems II Systems Programming for Android

App Development for Smart Devices. Lec #4: Services and Broadcast Receivers

Programming with Android: Notifications, Threads, Services. Luca Bedogni. Dipartimento di Scienze dell Informazione Università di Bologna

Software Practice 3 Today s lecture Today s Task

IPN-ESCOM Application Development for Mobile Devices. Extraordinary. A Web service, invoking the SOAP protocol, in an Android application.

ANDROID SERVICES, BROADCAST RECEIVER, APPLICATION RESOURCES AND PROCESS

Upcoming Assignments Quiz Friday? Lab 5 due today Alpha Version due Friday, February 26

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

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

Termite WiFi Direct API

Implementing a Download Background Service

Android Service. Lecture 19

Embedded Systems Programming - PA8001

Services Broadcast Receivers Permissions

Lecture 14. Android Application Development

Understanding Intents and Intent Filters

Table of Content Android Tutorial... 2 Audience... 2 Prerequisites... 2 Copyright & Disclaimer Notice... 2 Overview... 7

Lab 5 Periodic Task Scheduling

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.

Diving into Android. By Jeroen Tietema. Jeroen Tietema,

INTRODUCTION TO ANDROID

Notification mechanism

Mobile and Ubiquitous Computing: Android Programming (part 3)

App Development for Android. Prabhaker Matet

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

App Development for Smart Devices. Lec #7: Windows Azure

Android User Interface Android Smartphone Programming. Outline University of Freiburg

Solving an Android Threading Problem

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

ANDROID APPS DEVELOPMENT FOR MOBILE AND TABLET DEVICE (LEVEL II)

Database Development In Android Applications

Android User Interface

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

Mobile Application Development Android

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

Mobile Application Development Android

Android permissions Defining and using permissions Component permissions and related APIs

Import the code in the top level sdl_android folder as a module in Android Studio:

Android Notification

EMBEDDED SYSTEMS PROGRAMMING Application Tip: Switching UIs

Mobile Platforms and Middleware

Mobile Application Development Android

Practical 1.ListView example

Programming Android UI. J. Serrat Software Design December 2017

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

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

Understanding Application

Android HelloWorld - Example. Tushar B. Kute,

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

Stanislav Rost CSAIL, MIT

Programming with Android: Android for Tablets. Dipartimento di Scienze dell Informazione Università di Bologna

Mobile Application (Design and) Development

Vienos veiklos būsena. Theory

Mobile Computing. Introduction to Android

Activities. Repo:

Introduction to Android Development

Mobile Application Development L14: Miscellaneous

Security model. Marco Ronchetti Università degli Studi di Trento

Real-Time Embedded Systems

Mobile and Ubiquitous Computing: Android Programming (part 4)

Midterm Examination. CSCE 4623 (Fall 2017) October 20, 2017

Extended Atomicity Checking with Blame Assignment for Android Applications

TextView. A label is called a TextView. TextViews are typically used to display a caption TextViews are not editable, therefore they take no input

ACTIVITY, FRAGMENT, NAVIGATION. Roberto Beraldi

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

EMBEDDED SYSTEMS PROGRAMMING Application Basics

Starting Another Activity Preferences

Notifications and Services. Landon Cox March 28, 2017

Embedded Systems Programming - PA8001

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

In-app Billing Version 3


Android Activities. Akhilesh Tyagi

CMSC436: Fall 2013 Week 3 Lab

Android - Widgets Tutorial

Application Fundamentals

Lecture 2 Android SDK

1. GOALS and MOTIVATION

ANDROID APPS DEVELOPMENT FOR MOBILE AND TABLET DEVICE (LEVEL I)

LECTURE 12 BROADCAST RECEIVERS

Multiple Activities. Many apps have multiple activities

Intents & Intent Filters. codeandroid.org

Coding for Life--Battery Life, That Is

SunmiPrinter Developer documentation

ANDROID APPS DEVELOPMENT FOR MOBILE AND TABLET DEVICE (LEVEL I)

Xin Pan. CSCI Fall

Android Services & Local IPC: The Activator Pattern (Part 2)

Android Application Development

Transcription:

Android Broadcasts Services Notifications

Broadcast receivers Application components that can receive intents from other applications Broadcast receivers must be declared in the manifest They have an associated intent They are invoked using sendbroadcast() It needs an intent matching the declared one (action) The intent can transport extra data sendbroadcast() can be invoked by any other application component (Activity, Service, Content Provider) in the same or other application Broadcast receivers extend class BroadcastReceiver They must override the method onreceive() They don t have any user interface The application containing the Broadcast receiver is activated and the onreceive() method invoked Background, Services, Notifications 2

Receives notifications (intents) sent by other applications Inherits from android.content.broadcastreceiver Declared in the <receiver> tag in the Manifest Normally execute in response to calls to Context.sendBroadcast(Intent) The onreceive(context, intent) method executes sendbroadcast() Broadcast Receivers onreceive() BroadcastReceiver Android Applications 3

Sending a broadcast Application 1 Application 2 Activity Intent bi = sendbroadcast(bi); should match Manifest <application> <receiver android:name= > <intent-filter> <action android:name= /> </intent-filter> </receiver> BroadcastReceiver onreceive() Background, Services, Notifications 4

Broadcast receiver example The receiver public class MyReceiver extends BroadcastReceiver { public void onreceive(context context, Intent intent) { String msg = intent.getstringextra( somename ); //Do something Manifest definition <manifest> <application> <receiver android:name=.myreceiver > <intent-filter> <action android:name= org.feup.intents.test /> </intent-filter> </receiver> </application> </manifest> The broadcast Activity Public class MyActivity extends Activity { private void invokereceiver() { Intent broadcast = new Intent ( org.feup.intents.test ); broadcast.putextra( somename, Hello ); sendbroadcast(broadcast); Background, Services, Notifications 5

Processes and receivers / services Android process system sendbroadcast() startservice() stopservice() Other process Message queue Activity Receiver Service Other thread bindservice() Main thread pick call service methods call Application Not Responding if the cycle takes more than a few seconds Background, Services, Notifications 6

Services Can be invoked from other clients Clients are in the same process or in other processes Using a local intent (class) or an implicit one (action) Services don t have an user interface If the service process is not in memory it is started the oncreate() method is executed Any client can invoke a service asynchronously calling startservice() which will invoke onstartcommand() stopservice() will try to terminate the service (ondestroy() is invoked in this procedure) A service can terminate itself calling stopself() A client can call bindservice() to establish a channel and obtain a service interface (remote call service) The client can then call the interface methods Background, Services, Notifications 7

Services Services are freed when Stopped explicitly stopservice() from the client stopself() on the service itself Android needs the memory or resources they occupy, terminating the service (always after onstartcommand() had returned) Services have high priorities, but less then the active Activity They can be automatically brought to memory again if terminated by Android Depending on the onstartcommand() return value START_NOT_STICKY they are not brought to memory until a new startservice() is executed START_STICKY they are brought to memory again, but with a NULL intent START_REDELIVER_INTENT - they are brought to memory again with the last processed intent Background, Services, Notifications 8

Services and their lifecycle Creation Can be initiated and terminated from other parts Or the service can be created by a connection (bind) A service inherits from android.app.service Active Lifetime Full Lifetime Android Applications 9

import android.app.service; import android.content.intent; import android.os.ibinder; Service skeleton public class MyService extends Service { public void oncreate() { // TODO: Actions to perform when service is created. Manifest: <service android:name=".myservice"/> Calling the service // Implicitly start a Service Intent myintent = new Intent(MyService.ORDER_PIZZA); myintent.putextra("topping", "Margherita"); startservice(myintent); public IBinder onbind(intent intent) { return null; // mandatory but should return null for // non remote call services public int onstartcommand(intent intent, int flags, int startid) { // Usually launch a background thread to do processing. return Service.START_NOT_STICKY; // or other value public void ondestroy() { // TODO: Actions to perform when service is destroyed // Explicitly start a Service in the same process startservice(new Intent(this, MyService.class)); Stopping the service // With the same intent stopservice(new Intent(MyService.ORDER_PIZZA)); // Stop a service with the service name (same proc). ComponentName service = startservice(new Intent(this, MyService.class));... stopservice(new Intent(this, service.getclass())); // Stop a service explicitly in the same process Class serviceclass = Class.forName(service.getClassName()); stopservice(new Intent(this, serviceclass)); Background, Services, Notifications 10

It s a special purpose Service subclass that creates a single worker thread The intent received on onstartcommand() is passed to the method that the worker thread executes Successive calls on onstartcommand() are queued You only have to override and implement onhandleintent() IntentService public class MyService extends IntentService { public MyService() { super( MyService ); protected void onhandleintent(intent intent) { // Do the work in this single worker thread // and return Background, Services, Notifications 11

Remote call services Their functionality is invoked using RPC Predefined interface specified via an AIDL file Usually they are standalone in their own processes Remote call services are activated (brought to memory and oncreate() invoked) through bindservice() and can be freed when the last bound client calls unbindservice() When a service is ready to be called through its interface a callback onserviceconnected() is called on the client There is also a onservicedisconnected() callback on the client that is called when the service is not available (motivated by a crash or reclaimed by Android) Background, Services, Notifications 12

Remote call service Service Client Activity onbind() Service interface methods bindservice( ) when ready returns the Service interface service method call unbindservice( )... waits for connection.. calls service methods.. passes ServiceConnection object onserviceconnected() onservicedisconnected() Background, Services, Notifications 13

Service interface is defined in an AIDL file // This file is IStockQuoteService.aidl package com.androidbook.services.stockquoteservice; interface IStockQuoteService { double getquote(string ticker); The service must implement the interface public class StockQuoteService extends Service { public class StockQuoteServiceImpl extends IStockQuoteService.Stub { public double getquote(string ticker) throws RemoteException { return 20.0; public IBinder onbind(intent intent) { return new StockQuoteServiceImpl(); Example The client calling the service bindservice(new Intent(IStockQuoteService.class.getName()), serconn, Context.BIND_AUTO_CREATE); private ServiceConnection serconn = new ServiceConnection() { public void onserviceconnected(componentname name, IBinder service) { stockservice = IStockQuoteService.Stub.asInterface(service); callbtn.setenabled(true); public void onservicedisconnected(componentname name) { callbtn.setenabled(false); stockservice = null; ; try { double val = stockservice.getquote("android"); Toast.makeText(this, "Value from service is " + val, Toast.LENGTH_SHORT).show(); catch (RemoteException ee) { Background, Services, Notifications 14

Notifications Are shown in the status bar More details listed in the extended status drawer They can produce sound, vibration and light leds Created using a system service String svcname = Context.NOTIFICATION_SERVICE; NotificationManager notificationmanager; notificationmanager = (NotificationManager) getsystemservice(svcname); Specified in a Notification object through a Build class // A small icon, a title and a text and mandatory (many other features) // get the Notification object using the build() method Notification notf = new Notification.Builder(this).setContentText(message) // the main text of the notification.setcontenttitle(title) // the first line (title).setsmallicon(r.drawable.nticon) // icon on bar and notification.setwhen(system.currenttimemillis()) // for ordering.setpendingintent(pendingintent pi) // Activity to launch on tap.build(); // returns the notification object notf.flags = Notification.FLAG_ONGOING_EVENT; // cannot be cleared Sent using the notify() method of the service Background, Services, Notifications 15

Extended Notification Drawer Notifications with standard views Customized view notification with a RemoteViews object featuring an Icon, TextView and ProgressBar Background, Services, Notifications 16

Layout specification A customized notification <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:padding="5dp" android:layout_width="fill_parent" android:layout_height="fill_parent"> <ImageView android:id="@+id/status_icon" android:layout_width="wrap_content" android:layout_height="fill_parent" android:layout_alignparentleft="true /> <RelativeLayout android:layout_width="fill_parent" android:layout_height="fill_parent" android:paddingleft="10px" android:layout_torightof="@id/status_icon"> <TextView android:id="@+id/status_text" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_alignparenttop="true" android:textcolor="#000" android:textsize="14sp" android:textstyle="bold /> <ProgressBar android:id="@+id/status_progress" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_below="@id/status_text" android:progressdrawable="@android:drawable/progress_horizontal" android:indeterminate="false" android:indeterminateonly="false /> </RelativeLayout> </RelativeLayout> Building the notification Intent intent = new Intent(this, MyActivity.class); PendingIntent pi = PendingIntent.getActivity(this, 0, intent, 0)); Notification notification = new Notification.Builder(this).setSmallIcon(R.drawable.icon).setContentText( Custom Content ).setwhen(system.currenttimemillis()).setcustomcontentview(new RemoteViews(this.getPackageName(), R.layout.my_status_window_layout).setPendingIntent(pi);.build(); // allowing updates notification.flags = Notification.FLAG_ONGOING_EVENT; // Putting state on the layout notification.contentview.setimageviewresource(r.id.status_icon, R.drawable.icon); notification.contentview.settextviewtext(r.id.status_text, "Current Progress:"); notification.contentview.setprogressbar(r.id.status_progress, // emitting the notification 100, 50, false); int notificationref = 1; notificationmanager.notify(notificationref, notification); Cancel // cancelling the notification notificationmanager.cancel(notificationref); Background, Services, Notifications 17

Alarms Calls an application component periodically or after a specified time interval Uses another system service String svcname = Context.ALARM_SERVICE; AlarmManager alarms; alarms = (AlarmManager) getsystemservice(svcname); We can use the methods set(), setrepeating() or setinexactrepeating() to create alarms int alarmtype = AlarmManager.ELAPSED_REALTIME_WAKEUP; long timeorlengthofwait = 10000; String ALARM_ACTION = "ALARM_ACTION"; Intent intenttofire = new Intent(ALARM_ACTION); PendingIntent pendingintent = PendingIntent.getBroadcast(this, 0, intenttofire, 0); alarms.set(alarmtype, timeorlengthofwait, pendingintent); Background, Services, Notifications 18