Messaging and Networking. Objectives Send SMS messages using the Messaging app Send SMS messages programmatically from within your app Receive and consume.

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.
COEN 445 Communication Networks and Protocols Lab 4
Network Programming Chapter 11 Lecture 6. Networks.
A CHAT CLIENT-SERVER MODULE IN JAVA BY MAHTAB M HUSSAIN MAYANK MOHAN ISE 582 FALL 2003 PROJECT.
1 Java Networking – Part I CS , Spring 2008/9.
1 Overview r Socket programming with TCP r Socket programming with UDP r Building a Web server.
System Programming Practical session 10 Java sockets.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved L22 (Chapter 25) Networking.
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;
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 25 Networking.
System broadcasts and services. System broadcast events. EventDescription Intent.ACTION_BOOT_COMPLETEDBoot completed. Requires the android.permission.RECE.
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.
Networking with Java CSc 335 Object-Oriented Programming and Design Spring 2009.
Servlets Compiled by Dr. Billy B. L. Lim. Servlets Servlets are Java programs which are invoked to service client requests on a Web server. Servlets extend.
Bluetooth. Bluetooth is an open, wireless protocol for exchanging data between devices over a short distance. –managed by the Bluetooth Special Interest.
Web Proxy Server. Proxy Server Introduction Returns status and error messages. Handles http CGI requests. –For more information about CGI please refer.
Android Application Development Tutorial. Topics Lecture 5 Overview Overview of Networking Programming Tutorial 2: Downloading from the Internet.
TCP Sockets Reliable Communication. TCP As mentioned before, TCP sits on top of other layers (IP, hardware) and implements Reliability In-order delivery.
HTTP and Threads. Download some code I’ve created an Android Project which gives examples of everything covered in this lecture. Download code here.here.
J2ME Messaging Khanh Le. Objective  The objective of wireless messaging is to extend the networking and I/O capabilities of J2ME applications to send.
An program As a simple example of socket programming we can implement a program that sends to a remote site As a simple example of socket.
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.
Simple Web Services. Internet Basics The Internet is based on a communication protocol named TCP (Transmission Control Protocol) TCP allows programs running.
© Keren Kalif Intro to Android Development Written by Keren Kalif, Edited by Liron Blecher Contains slides from Google I/O presentation.
Simple Web Services. Internet Basics The Internet is based on a communication protocol named TCP (Transmission Control Protocol) TCP allows programs running.
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.
An program As a simple example of socket programming we can implement a program that sends to a remote site As a simple example of socket.
Integrating with Android Services. Introduction  Android has numerous built-in functionality that can be called from within your applications  SMS/MMS.
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.
CS378 - Mobile Computing Intents.
16 Services and Broadcast Receivers CSNB544 Mobile Application Development Thanks to Utexas Austin.
HTTP and Threads. Download some code I’ve created an Android Project which gives examples of everything covered in this lecture. Download code here.here.
Vassil Roussev 2 A socket is the basic remote communication abstraction provided by the OS to processes. controlled by operating system.
CS378 - Mobile Computing Intents. Allow us to use applications and components that are part of Android System – start activities – start services – deliver.
Practicum: - Client-Server Computing in Java Fundamental Data Structures and Algorithms Margaret Reid-Miller 13 April 2004.
Lecture 9 Network programming. Manipulating URLs URL is an acronym for Uniform Resource Locator and is a reference (an address) to a resource on the Internet.
Li Tak Sing COMPS311F. Case study: consumers and producers A fixed size buffer which can hold at most certain integers. A number of producers which generate.
1 Chapter 28 Networking. 2 Objectives F To comprehend socket-based communication in Java (§28.2). F To understand client/server computing (§28.2). F To.
1 Network Programming and Java Sockets. 2 Network Request Result a client, a server, and network Client Server Client machine Server machine Elements.
CS390- Unix Programming Environment CS 390 Unix Programming Environment Java Socket Programming.
Android networking 1. Network programming with Android If your Android is connected to a WIFI, you can connect to servers using the usual Java API, like.
Server - Client Communication Getting data from server.
FCM Workflow using GCM.
UNIT-6. Basics of Networking TCP/IP Sockets Simple Client Server program Multiple clients Sending file from Server to Client Parallel search server.
1 Lecture 9: Network programming. 2 Manipulating URLs URL is an acronym for Uniform Resource Locator and is a reference (an address) to a resource on.
MT311 Java Application Development and Programming Languages Li Tak Sing ( 李德成 )
Adding network connections. Connecting to the Network To perform the network operations, your application manifest must include the following permissions:
Networking Code CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L.
Spring/2002 Distributed Software Engineering C:\unocourses\4350\slides\DefiningThreads 1 Java API for distributed computing.
Li Tak Sing COMPS311F. Case study: a multithreaded chat server The source contains 3 files: ChatServer //the chat server ChatThread //the thread on the.
Simple Web Services. Internet Basics The Internet is based on a communication protocol named TCP (Transmission Control Protocol) TCP allows programs running.
Intents and Broadcast Receivers Dr. David Janzen Except as otherwise noted, the content of this presentation is licensed under the Creative Commons Attribution.
David Sutton SMS TELEPHONY IN ANDROID. OUTLINE  This week’s exercise, an SMS Pub Quiz  Simulating telephony on an emulator  Broadcast Intents and broadcast.
Network Programming. These days almost all devices.
CS371m - Mobile Computing Intents 1. Allow us to use applications and components that are already part of Android System – start activities – start services.
Broadcast receivers.
CS371m - Mobile Computing Services and Broadcast Receivers
Messaging Unit-4.
Developing Android Services
CIS 470 Mobile App Development
Android Notifications (Part 2) Plus Alarms and BroadcastReceivers
CIS 470 Mobile App Development
Android Developer Fundamentals V2 Lesson 5
Mobile Programming Broadcast Receivers.
Presentation transcript:

Messaging and Networking

Objectives Send SMS messages using the Messaging app Send SMS messages programmatically from within your app Receive and consume incoming SMS messages Send messages from your app Connect to the web using HTTP Consume Web service Write TCP/IP socket-based network apps 2

Outline SMS messaging Sending messages BroadcastReceiver Receiving messages Sending messages Network programming 3

SMS Messaging Can send SMS messages programmatically or using a built-in Messaging app. Can receive feedback after sending SMS messages Use PendingIntent Use BroadcastReceiver to receive a notification 4

Sending SMS Messages Using Messaging Apps Use an intent to start a built-in messaging app Specify the MIME (Multipurpose Internet Media Extension) type “vnd.android-dir/mms-sms” Can send to multiple recipients 5 Intent intent = new Intent(android.content.Intent.ACTION_VIEW); intent.putExtra("address", "1234; 5678; 2345"); // multiple addresses intent.putExtra("sms_body", "Hello!"); intent.setType("vnd.android-dir/mms-sms"); // MIME type startActivity(intent);

Sending SMS Programmatically Use android.telephony.SmsManager Need a SEND_SMS permission in AndroidManifest.xml, e.g., 6 SmsManager sms = SmsManager.getDefault(); sms.sendTextMessage(“ ", null, "Hello!", null, null); void sendTextMessage( String destAddress, // phone number String scAddress, // service center or null for default SMSC String text, PendingIntent sentIntent, PendingIntent deliveryIntent) sentIntent: broadcast upon message sending deliveryIntent: broadcast upon message delivery

Getting Feedback After Sending Can monitor the status of the SMS message sending process, i.e., Whether the message was sent Whether the message was delivered Use broadcast pending intents along with callbacks PendingIntent: description of an Intent and target action to perform with it, typically, at later time, e.g., a broadcast PendingIntent that will perform a broadcast BroadcastReceiver: receive intents broadcasted, e.g., sent by sendBroadcast() 7

Broadcasting Intents A way for activities to communicate with each other by: Broadcasting intents Receiving broadcasted intents (using BroadcastReceiver) 8 Activity 1 Activity 2 Intent i broadcastIntent(i) registerBroadcastReceiver(receiver)

SMS Sent/Delivery Intents 9 sms.sendMessage(" ", null, "Hello", PendingIntent.getBroadcast(this, 0, new Intent("SMS_SENT"), 0), PendingIntent.getBroadcast(this, 0, new Intent("SMS_DELIVERED"), 0)); getBroadcast: Retrieve an existing or create a new PendingIntent that will perform a broadcast, e.g., by calling Context.sendBroadcast(). this: context in which the broadcast is performed 0: private request code --- currently not used 0: flag --- supply additional control information Specify broadcast PendingIntent when sending an SMS message, e.g.,

Receiving Sent/Delivery Intents 10 registerReceiver(new BroadcastReceiver() { public void onReceive(Context c, Intent i) { // c: Context in which the receiver is running switch (getResultCode()) { case Activity.RESULT_OK:... case SmsManager.RESULT_ERROR_GENERIC_FAILURE: } }, new IntentFilter("SMS_SENT")); Create broadcast receivers and register them BroadcastReceiver: class to receive intents broadcasted across apps

11 registerReceiver(new BroadCastReceiver() { public void onReceive(Context c, Intent i) { switch (getResultCode()) { case Activity.RESULT_OK:... case SmsManager.RESULT_CANCELED:... } }, new IntentFilter("SMS_DELIVERED"));

Outline SMS Messaging Sending SMS messages BroadcastReceiver Receiving SMS messages Sending messages Network programming 12

Receiving SMS Messages 13 In manifest file:... Can receive incoming SMS messages from within an app to perform an action for a certain messages, e.g., tracking the location of your (lost/stolen) phone Use BroadcastReceiver to listen for incoming messages

14 // SmsReceiver.java public class SmsReceiver extends BroadcastReceiver { public void onReceive(Context ctx, Intent i) { Bundle bundle = i.getExtras(); if (bundle != null) { StringBuilder builder = new StringBuilder(); for (Object pdu: (Object[]) bundle.get(“pdus”)) { SmsMessage msg = SmsMessage.createFromPdu((byte[]) pdu); builder.append(“From “+ msg.getOriginatingAddress()); builder.append(“ :” + msg.getMessageBody().toString() + “\n”); } … builder.toString() … } To test, use DDMS (Dalvik Debug Monitor Server) Emulator Control to send a message to the emulator (or use another emulator). Android Studio: Tools > Android > Android Device Monitor

Consuming Messages Completely 15 In manifest file:... By default, all interested apps will receive messages Solution Make your receiver handle it first (by setting its priority high) Call abortBroadcast() method in your receiver.

Communicating with Activities 16 Intent i = new Intent(context, MainActivity.class); i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); context.startActivity(i); Updating activity from BroadcastReceiver Can send received messages to activity Use sendBroadcast(Intent) method along with callbacks, registerReceiver(BroadCastReceiver, IntentFilter) in onResume() unregisterReceiver(BroadCastReceiver) in onPause() Invoking activity from BroadcastReceive Can invoke activity upon receiving messages

In-Class (Pair) Create a simple app that receives all incoming messages and displays only those messages that start with a word "URGENT:". P1: Write a broadcast receiver listening for incoming messages and printing them as toast messages. P2: Filter incoming messages for the word "URGENT:". P3: Invoke an activity to display the filtered messages. 17

Sending Messages 18 Can launch built-in application using intent Intent intent = new Intent(Intent.ACTION_SEND); intent.setData(Uri.parse("mailto:")); intent.putExtra(Intent.EXTRA_ , intent.putExtra(Intent.EXTRA_CC, intent.putExtra(Intent.EXTRA_SUBJECT, "Android"); intent.putExtra(Intent.EXTRA_TEXT, "Hello from android"); intent.setType("message/rfc822"); startActivity(Intent.createChooser(intent, " "));

Network Programming Same APIs as Java, but no networking operation on UI threads Connecting to the Web (HTTP) Can download binary data such as bitmap Can download text data Consuming Web services (XML and JSON) Use Java API for XML such as DOM and SAX Use JSON JavaScript Object Notation, Lightweight data-interchange format (no tags; use bracket and braces) Socket programming (TCP/IP) 19

Making HTTP Connection 20 Use URL.openConnection() URL url = new URL(" URLConnection conn = url.openConnection(); HttpURLConnection httpConn = (HttpURLConnection) conn; httpConn.setAllowUserInteraction(false); httpConn.setInstanceFollowRedirects(true); httpConn.setRequestMethod("GET"); httpConn.connect(); if (HttpURLConnection.HTTP_OK == httpConn.getResponseCode()) { InputStream in = httpConn.getInputStream();... read from in... }

Review: Socket Programming Sockets Ends points of two-way communications, i.e., logical connections between hosts Can be used to send and receive data Supported by most languages and platforms Server vs. client sockets Stream (TCP/IP) vs. datagram (UDP/IP) sockets 21 logical connection sockets (end points)

Server vs. Client Sockets Server sockets Wait for requests to come in over the network Implemented by java.net.ServerSocket class Client sockets Used to send and receive data Can be thought of as a pair of input and output streams Implemented by java.net.Socket class and java.net.DatagramSocket class 22

Server Sockets 23 To implement servers try { ServerSocket server = new ServerSocket(8000); while (true) { Socket incoming = server.accept(); // obtain a client socket // handle client request by creating a new thread // and reading from and writing to the socket // … } } catch (IOException e) { // handle exception on creating a server socket }

Client Sockets 24 To send and receive data Pair of input and output streams try { Socket s = new Socket(“ 80); PrintWriter out = new PrintWriter(new OutputStreamWriter(s.getOutputStream())); BufferedReader in = new BufferedReader(new InputStreamReader(s.getInputStream())); … use out and in to send and receive data … } catch (IOException e) { // handle exception … }

In-Class (Pair) Create a simple chat application using TCP/IP sockets (see handout) 25