Android Application Development Tutorial. Topics Lecture 5 Overview Overview of Networking Programming Tutorial 2: Downloading from the Internet.

Slides:



Advertisements
Similar presentations
Android Application Development Tutorial Accessing Sensors and the Network Deepa Shinde and Cindy Atherton.
Advertisements

Application Fundamentals. See: developer.android.com/guide/developing/building/index.html.
Who Am I And Why Am I Here I’m professor Stephen Fickas in CIS – you can call me Steve. I have a research group that works with mobile devices (since 1995!)
User Interface Android Applications. Activities An activity presents a visual user interface. Each activity is given a default window to draw in. The.
Client / Server Programming in Android Eclipse IDE Android Development Tools (ADT) Android SDK
Basic, Basic, Basic Android. What are Packages? Page 346 in text Package statement goes before any import statements Indicates that the class declared.
Android Application Development with Java UPenn CS4HS 2011 Chris Murphy
@2011 Mihail L. Sichitiu1 Android Introduction Hello World.
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.
Introduction to Android Programming Content Basic environmental structure Building a simple app Debugging.
Android development the first app. Andoid vs iOS which is better? Short answer: neither Proponents on both sides For an iOS side, see this article on.
1 CSCE 4013: Mobile Systems Programming Nilanjan Banerjee Mobile Systems Programming (Acknowledgment to Deepa Shinde and Cindy Atheron University of Arkansas.
1 Mobile Computing Monetizing An App Copyright 2014 by Janson Industries.
Chapter 2: Simplify! The Android User Interface
Android Activities 1. What are Android Activities? Activities are like windows in an Android application An application can have any number of activities.
Networking: Part 2 (Accessing the Internet). The UI Thread When an application is launched, the system creates a “main” UI thread responsible for handling.
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.
1/29/ Android Programming: FrameLayout By Dr. Ramji M. Makwana Professor and Head, Computer Engineering Department A.D. Patel.
Chapter 2 The Android User Interface. Objectives  In this chapter, you learn to:  Develop a user interface using the TextView, ImageView, and Button.
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.
Android - Broadcast Receivers
Import import android.graphics.Bitmap; import android.widget.ImageView;
1 Introducing Activity and Intent. 2 Memory LinearLayout, weight=2 LinearLayout, weight=1 TextView ListView.
ANDROID – DRAWING IMAGES – SIMPLE EXAMPLE IN INTERFACE AND EVENT HANDLING L. Grewe.
Announcements Homework #2 will be posted after class due Thursday Feb 7, 1:30pm you may work with one other person No office hours tonight (sorry!) I will.
Android App Basics Dr. David Janzen Except as otherwise noted, the content of this presentation is licensed under the Creative Commons Attribution 2.5.
Android Using Menus Notes are based on: The Busy Coder's Guide to Android Development by Mark L. Murphy Copyright © CommonsWare, LLC. ISBN:
ANDROID – A FIRST PROGRAM L. Grewe Using AndroidStudio –basic Android  Lets do a “Hello World Project”  Start up AndroidStudio (assume you have installed.
HW#9 Clues CSCI 571 Fall, HW#9 Prototype
Copyright© Jeffrey Jongko, Ateneo de Manila University Deconstructing HelloWorld.
Android and s Ken Nguyen Clayton state University 2012.
Adding network connections. Connecting to the Network To perform the network operations, your application manifest must include the following permissions:
Android 基本 I/O. 基本 I/O 介面元件 在此節中主要介紹常見的 I/O 使用者介 面元件 – Button, TextView, 以及 EditText , 學習者可以學會: – Android 的視窗表單設計 res/layout/main.xml – Android SDK –
CMPE419 Mobile Application Development Asst.Prof.Dr.Ahmet Ünveren SPRING Computer Engineering Department Asst.Prof.Dr.Ahmet Ünveren
Messaging and Networking. Objectives Send SMS messages using the Messaging app Send SMS messages programmatically from within your app Receive and consume.
CMPE419 Mobile Application Development Asst.Prof.Dr.Ahmet Ünveren SPRING Computer Engineering Department Asst.Prof.Dr.Ahmet Ünveren
Android Programming.
Chapter 2: Simplify! The Android User Interface
Lab7 – Appendix.
Lab7 – Advanced.
Android Programming - Features
Lecture 3 Zablon Ochomo Android Layouts Lecture 3 Zablon Ochomo
Android Introduction Hello World
Android Application Development 1 6 May 2018
Android N Amanquah.
GUI Programming Fundamentals
Android – Event Handling
Android Introduction Hello World.
Android Widgets 1 7 August 2018
ITEC535 – Mobile Programming
Android – Read/Write to External Storage
תכנות ב android אליהו חלסצ'י.
Picasso Revisted.
CIS 470 Mobile App Development
CIS 470 Mobile App Development
CIS 470 Mobile App Development
CIS 470 Mobile App Development
BMI Android Application will take weight and height from the users to calculate Body Mass Index (BMI) with the information, whether user is underweight,
CIS 470 Mobile App Development
CIS 470 Mobile App Development
CIS 493/EEC 492 Android Sensor Programming
CIS 470 Mobile App Development
Adding Components to Activity
Uniform Resource Locator: URL
// Please Setting App_name to Send Successful
Chapter 5 Your Second Activity.
Activities, Fragments, and Intents
Android Sensor Programming
Android Sensor Programming
CIS 694/EEC 693 Android Sensor Programming
Presentation transcript:

Android Application Development Tutorial

Topics Lecture 5 Overview Overview of Networking Programming Tutorial 2: Downloading from the Internet

Programming Tutorial 2 Accessing a website from the Android Emulator

Required Packages

Layout

Link Activity and View View object may have an integer ID associated with it To get the reference of the view object in activity Button myButton = (Button)findViewById(R.id.my_button);

Adding Event to View Object View.OnClickListener() ◦Interface definition for a callback to be invoked when a view is clicked. onClick(View v) ◦Called when a view has been clicked. Inside this function you can specify what actions to perform on a click.

Strings.xml

AndroidManifest.xml

Network Settings If you are using the emulator then there are limitations. Each instance of the emulator runs behind a virtual router/firewall service that isolates it from your development machine's network interfaces and settings and from the internet. Communication with the emulated device may be blocked by a firewall program running on your machine. Reference

Behind Proxy Server

App to Download jpg file Step1 Add permissions to AndroidManifest.xml Step 2 Import files import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.net.HttpURLConnection; import java.net.URL; import java.net.URLConnection; import android.app.Activity; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.os.Bundle; import android.widget.ImageView; import android.widget.TextView; import android.widget.Toast;

App to Download jpg file Step 3 Writing OpenHttpConnection() ◦To open a connection to a HTTP server using OpenHttpConnection() ◦We first create an instance of the URL class and initialize it with the URL of the server ◦When the connection is established, you pass this connection to an URLConnection object. To check if the connection established is using a HTTP protocol. ◦The URLConnection object is then cast into an HttpURLConnection object and you set the various properties of the HTTP connection. ◦Next, you connect to the HTTP server and get a response from the server. If the response code is HTTP_OK, you then get the InputStream object from the connection so that you can begin to read incoming data from the server ◦The function then returns the InputStream object obtained.

App to Download jpg file public class HttpDownload extends Activity { /** Called when the activity is first public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); } private InputStream OpenHttpConnection(String urlString) throws IOException { InputStream in = null; int response = -1; URL url = new URL(urlString); URLConnection conn = url.openConnection(); if (!(conn instanceof HttpURLConnection)) throw new IOException("Not an HTTP connection"); try{ HttpURLConnection httpConn = (HttpURLConnection) conn; httpConn.setAllowUserInteraction(false); httpConn.setInstanceFollowRedirects(true); httpConn.setRequestMethod("GET"); httpConn.connect(); response = httpConn.getResponseCode(); if (response == HttpURLConnection.HTTP_OK) { in = httpConn.getInputStream(); } } catch (Exception ex) { throw new IOException("Error connecting"); } return in; }

App to Download jpg file Step 4 Modify the Main.xml code <LinearLayout xmlns:android=" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" /> <TextView android:textStyle="bold" android:layout_width="wrap_content" android:layout_height="wrap_content" />

App to Download jpg file Step 5 writing DownloadImage() ◦The DownloadImage() function takes in a string containing the URL of the image to download. ◦It then calls the OpenHttpConnection() function to obtain an InputStream object for reading the image data. ◦The InputStream object is sent to the decodeStream() method of the BitmapFactory class. ◦The decodeStream() method decodes an InputStream object into a bitmap. ◦The decoded bitmap is then returned by the DownloadImage() function. private Bitmap DownloadImage(String URL) { Bitmap bitmap = null; InputStream in = null; try { in = OpenHttpConnection(URL); bitmap = BitmapFactory.decodeStream(in); in.close(); } catch (IOException e1) { e1.printStackTrace(); } return bitmap; }

Step 6 T est the DownloadImage() function, modify the onCreate() event as public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); Bitmap bitmap = DownloadImage( " img = (ImageView) findViewById(R.id.img); img.setImageBitmap(bitmap); }

App to Download jpg file Step 7:Output

End of Tutorial 2