Integrating with Android Services. Introduction  Android has numerous built-in functionality that can be called from within your applications  SMS/MMS.

Slides:



Advertisements
Similar presentations
Android Application Development Tutorial. Topics Lecture 6 Overview Programming Tutorial 3: Sending/Receiving SMS Messages.
Advertisements

MESSAGING. Overview  SMS sends short text messages between mobile phones.  Supports sending both text messages and data messages  MMS (multimedia messaging.
Manifest File, Intents, and Multiple Activities. Manifest File.
George Blank University Lecturer. CS 602 Java and the Web Object Oriented Software Development Using Java Chapter 4.
Interrupts (contd..) Multiple I/O devices may be connected to the processor and the memory via a bus. Some or all of these devices may be capable of generating.
Inter Process Communication:  It is an essential aspect of process management. By allowing processes to communicate with each other: 1.We can synchronize.
Cosc 5/4730 Android SMS. A note first Depending on the API level, an import changes because of a deprecated API 3 uses – import android.telephony.gsm.SmsManager;
System broadcasts and services. System broadcast events. EventDescription Intent.ACTION_BOOT_COMPLETEDBoot completed. Requires the android.permission.RECE.
Mobile Programming Pertemuan 6 Presented by Mulyono Poltek NSC Surabaya.
Cosc 5/4730 Android SMS and MMS.
SMS. Short Message Service – Primarily text messages between mobile phones – First one sent December 3, 1982 “Merry Christmas” – In 2008 Approximately.
CS378 - Mobile Computing Speech to Text, Text to Speech, Telephony.
PROG Mobile Java Application Development PROG Mobile Java Application Development Event Handling Creating Menus.
J2ME Messaging Khanh Le. Objective  The objective of wireless messaging is to extend the networking and I/O capabilities of J2ME applications to send.
Developing Push Notifications (C2DM) for Android Vijai Co-Founder Adhish Technologies, Sweet’N’Spicy apps.
TCP/IP protocols Communication over Internet is mostly TCP/IP (Transmission Control Protocol over Internet Protocol) TCP/IP "stack" is software which allows.
© Keren Kalif Intro to Android Development Written by Keren Kalif, Edited by Liron Blecher Contains slides from Google I/O presentation.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Concurrency in Android with.
Java Mobile Application sms,sim,mms and barcode application Presented by Ayedh(SIM and MMS) Asad(SMS and Barcode Application)
Mobile Computing Lecture#08 IntentFilters & BroadcastReceivers.
Google Cloud Messaging for Android (GCM) is a free service that helps developers send data from servers to their Android.
Cosc 5/4730 Introduction: Threads, Android Activities, and MVC.
Data Storage: Part 4 (Content Providers). Content Providers Content providers allow the sharing of data between applications. Inter-process communication.
Using Intents to Broadcast Events Intents Can be used to broadcast messages anonymously Between components via the sendBroadcast method As a result Broadcast.
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.
DUE Hello World on the Android Platform.
16 Services and Broadcast Receivers CSNB544 Mobile Application Development Thanks to Utexas Austin.
1 Announcements Homework #2 due Feb 7 at 1:30pm Submit the entire Eclipse project in Blackboard Please fill out the when2meets when your Project Manager.
Spring 2008 Mark Fontenot CSE 1341 Principles of Computer Science I Note Set 2.
Threads and Services. Background Processes One of the key differences between Android and iPhone is the ability to run things in the background on Android.
Cosc 5/4730 Android App Widgets. App Widgets App Widgets are miniature application views that can be embedded in other applications (such as the Home.
Android - Broadcast Receivers
COMP 365 Android Development.  Every android application has a manifest file called AndroidManifest.xml  Found in the Project folder  Contains critical.
Telephony and sms API’S. Objective Telephony ➤ Initiating phone calls ➤ Reading the phone, network, data connectivity, and SIM states ➤ Monitoring changes.
1 Week 12 l Overview of Streams and File I/O l Text File I/O Streams and File I/O.
Cosc 5/4730 Android Communications Intents, callbacks, and setters.
Chapter 10 Defining Classes. The Internal Structure of Classes and Objects Object – collection of data and operations, in which the data can be accessed.
Mobile Programming Midterm Review
Processes CS 6560: Operating Systems Design. 2 Von Neuman Model Both text (program) and data reside in memory Execution cycle Fetch instruction Decode.
Working with Multiple Activities. Slide 2 Introduction Working with multiple activities Creating multiple views Introduction to intents Passing data to.
CS212: Object Oriented Analysis and Design Lecture 19: Exception Handling.
 Control Flow statements ◦ Selection statements ◦ Iteration statements ◦ Jump statements.
Lecture 2: Android Concepts
Technische Universität München Services, IPC and RPC Gökhan Yilmaz, Benedikt Brück.
Lecture 6: Process and Threads Topics: Process, Threads, Worker Thread, Async Task Date: Mar 1, 2016.
Intents and Broadcast Receivers Dr. David Janzen Except as otherwise noted, the content of this presentation is licensed under the Creative Commons Attribution.
Mobile Software Development for Android - I397 IT COLLEGE, ANDRES KÄVER, WEB:
David Sutton SMS TELEPHONY IN ANDROID. OUTLINE  This week’s exercise, an SMS Pub Quiz  Simulating telephony on an emulator  Broadcast Intents and broadcast.
The Ingredients of Android Applications. A simple application in a process In a classical programming environment, the OS would load the program code.
Working with Multiple Activities. Slide 2 Introduction Working with multiple activities Putting together the AndroidManifest.xml file Creating multiple.
Messaging and Networking. Objectives Send SMS messages using the Messaging app Send SMS messages programmatically from within your app Receive and consume.
Cosc 4735 Nougat API 24+ additions.
Intents and Broadcast Receivers
Mobile Software Development for Android - I397
Broadcast receivers.
CS371m - Mobile Computing Services and Broadcast Receivers
Messaging Unit-4.
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.
Mobile Software Development for Android - I397
Developing Android Services
CIS 470 Mobile App Development
Android Topics UI Thread and Limited processing resources
Android Topics Asynchronous Callsbacks
CIS 470 Mobile App Development
Android Developer Fundamentals V2
Android Developer Fundamentals V2 Lesson 5
Java Programming Language
Mobile Programming Broadcast Receivers.
Presentation transcript:

Integrating with Android Services

Introduction  Android has numerous built-in functionality that can be called from within your applications  SMS/MMS  Telephony (calls)  GPRS/3G  WIFI  These functions can be seamlessly integrated into your apps  Due to the use of Intents

Permissions  Just like with earlier topics requiring file system access many built-in functions require the use of permissions  These are needed to inform the user what sort of features an application requires  Very important if you download apps, e.g. from the Android Market

SMS

Basic SMS Concepts  Message types  Text (max 160 characters/message)  Binary (max 140 bytes/message)  Multi-part messages  if a message is too large it can be sent as several parts and reassembled at the destination  each part is a separate message with special information indicating the different parts form one whole message  Usually, only a maximum of 3 parts is recommended since parts can be lost in transit  If a part is lost, essentially the message is now useless

Basic SMS Concepts  Port number  a special value indication what type of application is going to use the message  e.g. sending vCard via SMS uses port 9204  Applications can define their own port number for their specific purposes

Overview  Android provides a means to access the phone’s SMS service  Applications can both send and receive SMS messages  Port-based messages are also supported on device  Sending of ported and non-ported sms is supported  However, the emulator does not properly handle receiving port-based SMS (they never arrive)  Non-port based messages are properly handled  They can be received with no problem

Permissions  To be able to access SMS you need to make sure the following permissions are placed in your AndroidManifest.xml  For sending:  For receiving:

Sending SMS  To send SMS on Android you need to use the following classes  SmsManager  PendingIntent

SmsManager  SmsManager is a system class that holds the instance referencing the phone’s SMS system  android.telephony.SmsManager  You do not create instances of this class; it is a singleton  You refer to this instance using the static getDefault() method

SmsManager  SmsManager has several methods for sending SMS  Each is used for specific circumstances  sendTextMessage(String destinationAddress, String scAddress, String text, PendingIntent sen tIntent, PendingIntent deliveryIntent) sendTextMessageString PendingIntent  Send a text based SMS.  sendMultipartTextMessage(String destinationAddress, String scAddress, ArrayList parts, ArrayList sentIntents, ArrayList deliveryIntents) sendMultipartTextMessageString ArrayListStringArrayListPendingIntentArrayListPendingIntent  Send a multi-part text based SMS.  sendDataMessage(String destinationAddress, String scAddress, short destinationPort, byte[] data, PendingIntent sentIntent, PendingIntent deliveryIntent) sendDataMessageString PendingIntent  Send a data based SMS to a specific application port

Simple sending  The simplest way to send a message is to use sendTextMessage()  It makes the assumption that the message is at most 160 characters long  Anything beyond that will be truncated and lost

sendTextMessage()  sendTextMessage has several parameters: sendTextMessage  String destinationAddress – phone number to send to String  String scAddress – smsc address (usually null) String  String text – message that will be sent String  PendingIntent sentIntent – intent that will be triggered when the message is sent PendingIntent  PendingIntent deliveryIntent – intent that will be triggered when the message is delivered PendingIntent

PendingIntent  A PendingIntent is a special kind of intent in that it is deferred  The intent is triggered after a specific condition is met  E.g. when message is sent or delivered  There are two ways of getting PendingIntents used for SMS  PendingIntent.getActivity()  PendingIntent.getBroadcast()

PendingIntent.getActivity()  This is used to signal that you want to open a specific activity when this PendingIntent is triggered  This method has several parameters  Context - The Context in which this PendingIntent should start the activity.  requestCode - (currently not used).  intent - Intent of the activity to be launched.  flags - any of the flags as supported by Intent.fillIn() to control which unspecified parts of the intent that can be supplied when the actual send happens (usually just 0)Intent.fillIn()

Example NOTE: the intent used will be triggered using startActivity() with this being the context for the activity PendingIntent pi = PendingIntent.getActivity(this, 0, new Intent(this, Sms1Activity.class), 0);

Example private void sendSMS(String phoneNumber, String message) { // this will re-open the Sms1Activity upon completion PendingIntent pi = PendingIntent.getActivity(this, 0, new Intent(this, Sms1Activity.class), 0); SmsManager sms = SmsManager.getDefault(); sms.sendTextMessage(phoneNumber, null, // null == use default SMSC message, pi, // sent intent null); }

Listening for SMS status  Status updates for services are send out using a broadcast  You need to specify a broadcast receiver to listen for these broadcasts  Broadcast receivers are all sub-types of the BroadcastReceiver class  android.content.BroadcastReceiver  Defines an onReceive() method  These must then be registered with the Context/Activity in order for them to be triggered

BroadcastReceiver  The values returned by getResultCode() are defined by the Broadcast being made private class DeliveredBroadCastReceiver extends BroadcastReceiver public void onReceive(Context arg0, Intent arg1) { switch (getResultCode()) { case Activity.RESULT_OK: Toast.makeText(getBaseContext(), "SMS delivered", Toast.LENGTH_SHORT).show(); break; case Activity.RESULT_CANCELED: Toast.makeText(getBaseContext(), "SMS not delivered", Toast.LENGTH_SHORT).show(); break; }

Example private class SentBroadcastReceiver extends BroadcastReceiver public void onReceive(Context arg0, Intent arg1) { switch (getResultCode()) { case Activity.RESULT_OK: break; // these constants are defined in the SmsManager case SmsManager.RESULT_ERROR_GENERIC_FAILURE: break; case SmsManager.RESULT_ERROR_NO_SERVICE: break; case SmsManager.RESULT_ERROR_NULL_PDU: break; case SmsManager.RESULT_ERROR_RADIO_OFF: break; }

Unregistering receivers  Receivers must be unregistered from an activity when the activity finishes.  unregisterReceiver(receiver)  NOTE: this means you may need to hold on to references of the receivers so you can pass them as parameters to this method

Example  A good place to put this where the back behaviour is done or where you close the Activity public void onBackPressed() { back(); } private void back() { finish(); // must remove receivers or else and error will occur unregisterReceiver(sentBroadcastReceiver); }

Multipart Messages  Multipart messages are handled in a similar fashion to single messages  However, you will need to split the long message into smaller parts  Use SmsManager.divideMessage(String)  This will return an ArrayList containing the individual Strings to be sent

Multipart Messages  Like single messages, multipart messages use PendingIntents to handle post-send/delivery actions  Since multiple messages are used, multiple intents are also used per part  These are passed as an ArrayList  See example

sendMultipartTextMessage()  sendMultipartTextMessage has several parameters: sendMultipartTextMessage  String destinationAddress – phone number to send to String  String scAddress – smsc address (usually null) String  String text – message that will be sent String  ArrayList sentIntents – intents that will be triggered when each part is sentPendingIntent  ArrayList deliveryIntents – intents that will be triggered when each part is deliveredPendingIntent

Threads  Usually sending SMS is done in a separate thread to insure the UI thread is not blocked by the action  This is particularly important with multipart messages if you want to track the progress of the sending process

Upcoming  In the next lecture:  Threaded updates using Handler  Receiving SMS messages  Using multiple emulators to simulate multiple phones  Programmatically triggering a call

Threaded updates  Android is very specific as to which thread alters the contents of the UI  Only the UI thread is allowed to touch the UI related objects  Any other thead doing this will trigger an Exception  Updates from another thread are done via a Handler