Notification mechanism

Similar documents
Lecture 14. Android Application Development

Manifest.xml. Activity.java

Android Services. Victor Matos Cleveland State University. Services

Android Workshop: Model View Controller ( MVC):

Simple Currency Converter

Android Application Development. By : Shibaji Debnath

Adaptation of materials: dr Tomasz Xięski. Based on presentations made available by Victor Matos, Cleveland State University.

Created By: Keith Acosta Instructor: Wei Zhong Courses: Senior Seminar Cryptography

Basic UI elements: Defining Activity UI in the code. Marco Ronchetti Università degli Studi di Trento

Android Beginners Workshop

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

EMBEDDED SYSTEMS PROGRAMMING Application Tip: Managing Screen Orientation

Intents. Your first app assignment

Vienos veiklos būsena. Theory

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

LifeStreet Media Android Publisher SDK Integration Guide

Learn about Android Content Providers and SQLite

Tłumaczenie i adaptacja materiałów: dr Tomasz Xięski. Na podstawie prezentacji udostępnionych przez Victor Matos, Cleveland State University.

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

Solving an Android Threading Problem

Android UI Development

Services. Marco Ronchetti Università degli Studi di Trento

Diving into Android. By Jeroen Tietema. Jeroen Tietema,

Group B: Assignment No 8. Title of Assignment: To verify the operating system name and version of Mobile devices.

Software Practice 3 Before we start Today s lecture Today s Task Team organization

EMBEDDED SYSTEMS PROGRAMMING Application Tip: Switching UIs

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

Android Data Storage

Basic GUI elements - exercises

Broadcast receivers. Marco Ronchetti Università degli Studi di Trento

Intents, Intent Filters, and Invoking Activities: Part I: Using Class Name

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

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

INTRODUCTION TO ANDROID

Saving application preferences

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

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

EMBEDDED SYSTEMS PROGRAMMING Android Services

Applications. Marco Ronchetti Università degli Studi di Trento

Android HelloWorld - Example. Tushar B. Kute,

UI (User Interface) overview Supporting Multiple Screens Touch events and listeners

Real-Time Embedded Systems

Embedded Systems Programming - PA8001

Services. Marco Ronchetti Università degli Studi di Trento

Android Basics. - Bhaumik Shukla Android Application STEALTH FLASH

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

South Africa Version Control.

Produced by. Mobile Application Development. Higher Diploma in Science in Computer Science. Eamonn de Leastar

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

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

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

1 카메라 1.1 제어절차 1.2 관련주요메서드 1.3 제작철차 서피스뷰를생성하고이를제어하는서피스홀더객체를참조해야함. 매니페스트에퍼미션을지정해야한다.

Our First Android Application

Meniu. Create a project:

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

Android & iphone. Amir Eibagi. Localization

Android Using Menus. Victor Matos Cleveland State University

Writing and reading files

Figure 2.10 demonstrates the creation of a new project named Chapter2 using the wizard.

CSE 660 Lab 7. Submitted by: Arumugam Thendramil Pavai. 1)Simple Remote Calculator. Server is created using ServerSocket class of java. Server.

Arrays of Buttons. Inside Android

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.

android:orientation="horizontal" android:layout_margintop="30dp"> <Button android:text="button2"

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

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

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

Android. YÉÇàá. Victor Matos Cleveland State University

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

Android Exam AND-401 Android Application Development Version: 7.0 [ Total Questions: 129 ]

App Development for Android. Prabhaker Matet

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

Android Development Tutorial. Yi Huang

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

Security model. Marco Ronchetti Università degli Studi di Trento

MODULE 2: GETTING STARTED WITH ANDROID PROGRAMMING

LECTURE NOTES OF APPLICATION ACTIVITIES

Multiple Activities. Many apps have multiple activities

Android. Broadcasts Services Notifications

Introduction to Android

EMBEDDED SYSTEMS PROGRAMMING Application Tip: Saving State

Java & Android. Java Fundamentals. Madis Pink 2016 Tartu

Embedded Systems Programming - PA8001

Android Notification

Android App Development. Mr. Michaud ICE Programs Georgia Institute of Technology

EMBEDDED SYSTEMS PROGRAMMING UI Specification: Approaches

Text Properties Data Validation Styles/Themes Material Design

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

Tip Calculator. xmlns:tools=" android:layout_width="match_parent"

ANDROID PROGRAMS DAY 3

COMP4521 EMBEDDED SYSTEMS SOFTWARE

Tabel mysql. Kode di PHP. Config.php. Service.php

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

Starting Another Activity Preferences

COMP61242: Task /04/18

Tutorial: Setup for Android Development

Android Using Menus. Victor Matos Cleveland State University

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

Open Lecture Mobile Programming. Intro to Material Design

Developed and taught by well-known Contact author and developer. At public for details venues or onsite at your location.

EMBEDDED SYSTEMS PROGRAMMING Application Basics

Transcription:

Notification mechanism

Adaptation of materials: dr Tomasz Xięski. Based on presentations made available by Victor Matos, Cleveland State University. Portions of this page are reproduced from work created and shared by Google and used according to terms

Notifications What is a Notification? A notification is a short message briefly displayed on the status line. It typically announces the happening of an special event for which a trigger has been set. After opening the Notification Panel the user may choose to selection and execute an associated activity. click on a 2

What is a Notification? Notifications Notification shown on the status line Drag down Click on Notification Panel to execute associated application 3

Notifications Notification Manager This class notifies the user of events that happen in the background. Notifications can take different forms: 1. A persistent icon that goes in the status bar and is accessible through the launcher, (when the user selects it, a designated Intent can be launched), 2. Turning on or flashing LEDs on the device, or 3. Alerting the user by flashing the backlight, playing a sound, or vibrating. 4

Notifications Since Jelly Bean 4.0 Base Layout All notifications include in their MINIMUM configurations three parts: 1. the sending application's notification icon or the sender's photo a notification title and message 2. a timestamp 3. a secondary icon to identify the sending application when the senders image is shown for the main icon Optional Entries 1. Additional lines 2. Up to three actions 3. A summary line 5

Notifications Since Jelly Bean 4.0 Extended Layout Sender s Icon / Photo Extra lines Up to 3 Actions to call Summary Activity to be called 6

Notifications Notification Manager You do not instantiate this class directly; instead, retrieve it through getsystemservice ( String ). Example: String servname = Context.NOTIFICATION_SERVICE; notificationmanager = (NotificationManager) getsystemservice (servname); 7

Notifications Notification Builder A convenient way to set up various fields of a Notification Example: Notification noti = new Notification.Builder(Context).setContentTitle( Important message for you... ).setcontenttext(subject).setsmallicon(r.drawable.new_mail).setlargeicon(abitmap).build(); 8

Example Notifications 9

Example Notifications alled by ACTIONl notiflcation 10

Example Notifications 11

Example: Notifications MainActivity package com.example.mynotificationmanager; import... public class MainActivity extends Activity implements OnClickListener { NotificationManager notificationmanager; final int NOTIFICATION_ID = 12345; @Override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.main); findviewbyid(r.id.btnbig).setonclicklistener(this); findviewbyid(r.id.btncancel).setonclicklistener(this) ; }// oncreate @SuppressLint("NewApi") public void createbignotification(view view) { Intent intent = new Intent(this, NotificationReceiverActivity.class); intent.putextra("callerintent", PendingIntent "main"); intent.putextra("notificationid", pintent = PendingIntent.getActivity(this, NOTIFICATION_ID); 0, intent, 0); PendingIntent pintent = PendingIntent.getActivity(this, 0, intent, 0); 12

Example: Notifications MainActivity // better way to do previous work PendingIntent pintent1 = makependingintent( NotificationReceiverActivity1.class, "Action1"); PendingIntent pintent2 = makependingintent( NotificationReceiverActivity2.class, "Action2"); PendingIntent pintent3 = makependingintent( NotificationReceiverActivity3.class, "Action3"); // a bitmap to be added in the notification view Bitmap mybitmap = BitmapFactory.decodeResource(getResources(), R.drawable.my_large_bitmap); Notification.Builder basenotification = new Notification.Builder(this).setContentTitle("TITLE goes here...").setcontenttext("second Line of text goes here").setticker("ticker tape1...ticker tape2...").addaction(r.drawable.icon1, "Action1", pintent1).addaction(r.drawable.icon2, "Action2", pintent2).addaction(r.drawable.icon3, "Action3", pintent3).setsmallicon(r.drawable.icon0).setlargeicon(mybitmap).setlights(0xffcc00, 1000, 500).setContentIntent(pIntent) ; 13

Example: Notifications MainActivity Notification noti = new Notification.InboxStyle(baseNotification).addLine("Line-1").addLine("Line-2").addLine("Line-2").setSummaryText("SUMMARY-Line-1 here").build(); notificationmanager = (NotificationManager) getsystemservice(notification_service); // Hide the notification after its selected noti.flags = Notification.FLAG_AUTO_CANCEL; // notification ID is 12345 notificationmanager.notify(12345, noti); }// createbignotification @Override public void onclick(view v) { switch (v.getid()) { case R.id.btnBig: createbignotification(v); break; 14

Example: Notifications MainActivity case R.id.btnCancel : try { if ( notificationmanager!= null){ notificationmanager.cancel(notification_id); } } catch (Exception e) { Log.e("<<MAIN>>", e.getmessage() ); } break; } }// onclick { public PendingIntent makependingintent(class partnerclass, String callername) } Intent intent = new Intent(this, partnerclass); intent.putextra("callerintent", callername); intent.putextra("notificationid", NOTIFICATION_ID); PendingIntent pintent = PendingIntent.getActivity(this, 0, intent, 0); return pintent; }// class 15

Example: Notifications NotificationReceiverActivity package com.example.mynotificationmanager; import... public class NotificationReceiverActivity extends Activity { @Override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.result); String callername = getintent().getstringextra("callerintent"); } } Toast.makeText(this, "Called by: " + callername, 1).show(); 16

Example: Manifest Notifications <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.mynotificationmanager" android:versioncode="1" android:versionname="1.0" > <uses-sdk android:minsdkversion="8" android:targetsdkversion="14" /> <application android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/apptheme" ><activity android:name=".mainactivity" android:label="@string/title_activity_main" > <intent-filter> <action android:name="android.intent.action.main" /> <category android:name="android.intent.category.launcher" /> </intent-filter> </activity> <activity android:name=".notificationreceiveractivity" android:icon="@drawable/icon0" > </activity> 17

Example: Manifest Notifications <activity android:name=".notificationreceiveractivity1" android:icon="@drawable/icon1" > </activity> <activity android:name=".notificationreceiveractivity2" android:icon="@drawable/icon2" > </activity> <activity android:name=".notificationreceiveractivity3" android:icon="@drawable/icon3" > </activity> </application> </manifest> 18

Example - Layout: Notifications main.xml <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <Button android:id="@+id/btnbig" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="create Big Notification" android:ems="20" > </Button> <Button android:id="@+id/btncancel" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="cancel Notification" android:ems="20" > </Button> </LinearLayout> 19

Example - Layout: Notifications main.xml <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" > <TextView android:id="@+id/txtmsg" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="called from the MASTER notification" > </TextView> </LinearLayout> 20

Notifications (Before SDK 4.0) Notification This class represents how a persistent notification is to be presented to the user using the NotificationManager. public Notification (int icon, CharSequence tickertext, long when) Parameters icon tickertext when The resource id of the icon to put in the status bar. The text that flows by in the status bar when the notification first activates. The time to show in the time field. In the System.currentTimeMillis timebase. 21

Notifications (Before SDK 4.0) Notification - Methods public void notify (int id, Notification notification) Places a persistent notification on the status bar. Parameters id notification An identifier for this notification unique within your application. A Notification object describing how to notify the user, other than the view you're providing. Must not be null. 22

Notifications (Before SDK 4.0) Notification Methods public void setlatesteventinfo ( Context context, CharSequence contenttitle, CharSequence contenttext, PendingIntent contentintent) Sets the contentview field to be a view with the standard "Latest Event" layout. Parameters context contenttitle contenttext contentintent The context for your application / activity. The title that goes in the expanded entry. The text that goes in the expanded entry. The intent to launch when the user clicks the expanded notification. 23

Notifications (Before SDK 4.0) Notification Methods public void cancel ( int id ) public void cancelall ( ) Cancel a previously shown notification. If it's transient, the view will be hidden. If it's persistent, it will be removed from the status bar. Parameters Id An identifier for this notification unique within your application. 24

Notifications (Before SDK 4.0) Example. Produce a notification. Allow the user to click on the Notification Panel and execute appropriate activity to attend the message. 25

Notifications (Before SDK 4.0) Example - Layouts main.xml <?xml version="1.0" encoding="utf-8"?> <LinearLayout android:id="@+id/mylinearlayout1" android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="#ff000066" android:orientation="vertical" xmlns:android="http://schemas.android.com/apk/res/android" > <Button android:id="@+id/btngo" android:layout_width="106dp" android:layout_height="61dp" android:layout_margin="10dp" android:text=" Show Notification " > </Button> <Button android:id="@+id/btnstop" android:layout_width="106dp" android:layout_height="61dp" android:layout_margin="10dp" android:text=" Cancel Notification " > </Button> main2.xml <?xml version="1.0" encoding="utf-8"?> <LinearLayout android:id="@+id/main2linlayout" android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="#ff660000" android:orientation="vertical" xmlns:android="http://schemas.android.com/apk/res/an droid" > <TextView android:id="@+id/widget29" android:layout_width="251dp" android:layout_height="69dp" android:text="hola this is screen 2 - Layout:main2.xml" > </TextView> </LinearLayout> </LinearLayout> 26

Notifications (Before SDK 4.0) Example Manifest <?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="cis493.demos" android:versioncode="1" android:versionname="1.0"> <application android:icon="@drawable/icon" android:label="@string/app_name"> <activity android:name=".notifydemo1" android:label="@string/app_name"> <intent-filter> <action android:name="android.intent.action.main" /> <category android:name="android.intent.category.launcher" /> </intent-filter> </activity> /drawable btn_star_big_on_selected.png Note: Obtain the icon from the folder C:\Android\platforms\android- 1.x\data\res\drawable <activity android:name=".notifyhelper" > </activity> </application> <uses-sdk android:minsdkversion="4" /> </manifest> 27

Notifications (Before SDK 4.0) Example Create & Cancel a Notification package cis493.demos; import... public class NotifyDemo1 extends Activity { Button btngo; Button btnstop; int notificationid = 1; NotificationManager notificationmanager; @Override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.main); 28

Notifications (Before SDK 4.0) Example Create & Cancel a Notification btngo = (Button)findViewById(R.id.btnGo); btngo.setonclicklistener(new OnClickListener() { public void onclick(view v) { //define a notification manager String sername = Context.NOTIFICATION_SERVICE; notificationmanager = (NotificationManager)getSystemService(serName); //define notification using: icon, text, and timing. int icon = R.drawable.btn_star_big_on_selected; String tickertext = "1. My Notification TickerText"; long when = System.currentTimeMillis(); Notification notification = new Notification(icon, tickertext, when); //configure appearance of the notification String extendedtitle = "2. My Extended Title"; String extendedtext = "3. This is an extended and very important message"; Intent intent = new Intent(getApplicationContext(), NotifyHelper.class); intent.putextra("extendedtext", extendedtext); intent.putextra("extendedtitle", extendedtitle); PendingIntent launchintent = PendingIntent.getActivity(getApplicationContext(),0,intent,0); 29292929

Notifications (Before SDK 4.0) Example Create & Cancel a Notification notification.setlatesteventinfo(getapplicationcontext(), extendedtitle, extendedtext, launchintent); //trigger notification notificationid = 1; notificationmanager.notify(notificationid, notification); }//click }); btnstop = (Button)findViewById(R.id.btnStop); btnstop.setonclicklistener(new OnClickListener() { public void onclick(view v) { //canceling a notification notificationid = 1; notificationmanager.cancel(notificationid); } }); }//oncreate }//NotifyDemo1 30303030

Notifications (Before SDK 4.0) Example - SubActivity Attending the Notification package cis493.demos; Import... public class NotifyHelper extends Activity { @Override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.main2); Intent mydata = getintent(); // extract the extra-data in the Notification String msg = mydata.getstringextra("extendedtitle") + "\n" + mydata.getstringextra("extendedtext"); } } Toast.makeText(getApplicationContext(), msg, 1).show(); 31

Notifications Questions 32