Cosc 5/4730 Introduction: Threads, Android Activities, and MVC.

Slides:



Advertisements
Similar presentations
Google Android Introduction to Mobile Computing. Android is part of the build a better phone process Open Handset Alliance produces Android Comprises.
Advertisements

Application Fundamentals Android Development. Announcements Posting in D2L Tutorials.
Cosc 5/4730 Android Services. What is a service? From android developer web pages: Most confusion about the Service class actually revolves around what.
All About Android Introduction to Android 1. Creating a New App “These aren’t the droids we’re looking for.” Obi-wan Kenobi 1. Bring up Eclipse. 2. Click.
Application Fundamentals. See: developer.android.com/guide/developing/building/index.html.
Cosc 5/4730 Game Design. A short game design primer. A game or animation is built on an animation loop. – Instance variables of “objects” are updated.
6/13/20151 CS 160: Lecture 13 Professor John Canny Fall 2004.
Cosc 4755 Phone programming: GUI Concepts & Threads.
Mobile Programming Pertemuan 6 Presented by Mulyono Poltek NSC Surabaya.
Object-Oriented Analysis and Design
Cosc 4730 Android TabActivity and ListView. TabActivity A TabActivity allows for multiple “tabs”. – Each Tab is it’s own activity and the “root” activity.
Android Development (Basics)
Emerging Platform#4: Android Bina Ramamurthy.  Android is an Operating system.  Android is an emerging platform for mobile devices.  Initially developed.
Chien-Chung Shen Manifest and Activity Chien-Chung Shen
Intro to Android Programming George Nychis Srinivasan Seshan.
Cosc 5/4730 Information, Resources, and basic GUI concepts.
Networking Nasrullah. Input stream Most clients will use input streams that read data from the file system (FileInputStream), the network (getInputStream()/getInputStream()),
JavaScript & jQuery the missing manual Chapter 11
CS5103 Software Engineering Lecture 08 Android Development II.
© 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.
Software Architecture of Android Yaodong Bi, Ph.D. Department of Computing Sciences University of Scranton.
Rajab Davudov. Agenda Eclipse, ADT and Android SDK APK file Fundamentals – Activity – Service – Content Provider – Broadcast Receiver – Intent Hello World.
Cosc 5/4730 Broadcast Receiver. Broadcast receiver A broadcast receiver (short receiver) – is an Android component which allows you to register for system.
Mobile Application Development using Android Lecture 2.
DUE Hello World on the Android Platform.
CS378 - Mobile Computing Intents.
16 Services and Broadcast Receivers CSNB544 Mobile Application Development Thanks to Utexas Austin.
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.
CS378 - Mobile Computing Intents. Allow us to use applications and components that are part of Android System – start activities – start services – deliver.
Android Boot Camp for Developers Using Java, 3E
Networking: Part 1 (Web Content). Networking with Android Android provides A full-featured web browser based on Chromium, the open source browser engine.
CSC 205 – Java Programming II Applet. Types of Java Programs Applets Applications Console applications Graphics applications Applications are stand-alone.
Cosc 5/4730 Android Communications Intents, callbacks, and setters.
ANDROID L. Grewe Components  Java Standard Development Kit (JDK) (download) (latest version)  AndroidStudio.
Working in the Background Radan Ganchev Astea Solutions.
Cosc 4730 Android Fragments. Fragments You can think of a fragment as a modular section of an activity, which has its own lifecycle, receives its own.
Concurrent Programming and Threads Threads Blocking a User Interface.
Model View Controller MVC Web Software Architecture.
Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 1 McGraw-Hill/Irwin Chapter 5 Creating Classes.
HW#9 Clues CSCI 571 Fall, HW#9 Prototype
(1) Introduction to Java GUIs Philip Johnson Collaborative Software Development Laboratory Information and Computer Sciences University of Hawaii Honolulu.
Android: “Dynamic” data and Preferences data.
Recap of Part 1 Terminology Windows FormsAndroidMVP FormActivityView? ControlViewView? ?ServiceModel? Activities Views / ViewGroups Intents Services.
Services Background operating component without a visual interface Running in the background indefinitely Differently from Activity, Service in Android.
Lecture 2: Android Concepts
1 CSC160 Chapter 1: Introduction to JavaScript Chapter 2: Placing JavaScript in an HTML File.
Flux & React Web Application Development Mark Repka, Rich McNeary, Steve Mueller.
Class on Fragments & threads. Fragments fragment is a modular section of an activity, which has its own lifecycle, receives its own input events, and.
Introduction to Android OS Димитър Н. Димитров Astea Solutions AD.
Mobile Programming Lecture 4 Resources, Selection, Activities, Intents.
CHAPTER 6 Threads, Handlers, and Programmatic Movement.
Services. What is a Service? A Service is not a separate process. A Service is not a thread. A Service itself is actually very simple, providing two main.
School of Engineering and Information and Communication Technology KIT305/KIT607 Mobile Application Development Android OS –Permissions (cont.), Fragments,
The Ingredients of Android Applications. A simple application in a process In a classical programming environment, the OS would load the program code.
Cosc 4735 Nougat API 24+ additions.
Android Application Development 1 6 May 2018
Android – Event Handling
Android Studio, Android System Basics and Git
Lecture 27 Creating Custom GUIs
Android Mobile Application Development
Android Programming Lecture 9
Android Topics UI Thread and Limited processing resources
Android Topics Asynchronous Callsbacks
Activities and Intents
Constructors, GUI’s(Using Swing) and ActionListner
Emerging Platform#3 Android & Programming an App
Mobile Programming Dr. Mohsin Ali Memon.
Cosc 4730 An Introduction.
Activities, Fragments, and Intents
Presentation transcript:

Cosc 5/4730 Introduction: Threads, Android Activities, and MVC

CONCURRENT PROGRAMMING Threads

Concurrent programming In it's simplest form – two or more programs, processes, or threads running at the same time to complete a task. This can be two completely separate programs running – may not even be coded in the same language This can be two running instances of the same program This can be one program running multiple threads. – Example Web server, every time a new connection is made, the web server spawns a new process to deal with that connection.

Concurrent programming (2) Example (2) – a program had two+ vectors to add together It spawns (new processes or threads) equal to the length of the vector. – Each new process/thread now adds one row of the vectors together and returns the result. The process/threads new end, leaving the original program with the result. With the exception of overhead, the vector is multiplied together in a time of 1, instead a time of N (where n is the length of the vector)

Threading Similar in nature to launching new processes – Except it in the same program, same process, has access to the same "global" variables in the program. – A process can have many threads. for GUI interfaces: – main thread draws the screen. – A second thread to do calculations » This thread is used to avoid delays in redrawing the screen – More threads to deal with events, such as menu and buttons clicks.

Java Threading When a java program starts, it gets one thread (the main execute line of the program) By extending the thread class, you can add more threads or implements runnable in your class – the void run method must be implemented for either approach.

Java Threading (2) Class myThread extends Thread { //variables, whatever myThread() { //constructor class if needed } public void run() { //called when the thread is started } myThread t = new myThread(); t.start(); //new thread and calls run //this code continues to run //do something else, while thread is also running. Note when execution the run method is done, the thread also ends.

Java Threading (3) A second method exists, called implements, which creates a runnable object (ie a class with a threads), but may extend another class. class myClass implements Runnable { public int a; public void run() { //do something with a } myClass t = new myClass; new Thead(t).start(); //new thread starts and calls run(); System.out.println(t.a);

Quick example import java.io.*; import java.net.*; public class testThread extends Thread { public int count; public String name; testThread(int i, String n) { count = i; name = n; } public void run() { for (int i = 0; i <count; i++) { System.out.println(name + " " + i); } public static void main (String[] args) { // declare the threads to run testThread t1 = new testThread(20,"P1"); testThread t2 = new testThread(30,"P2"); testThread t3 = new testThread(15,"P3"); // start the threads t1.start(); t2.start(); t3.start(); for (int i = 0; i <10; i++) { System.out.println("main" + " " + i); } }

Quick Example 2 In the selfthread class, event() creates a new thread of itself and starts it. – the run method has access to all the same variables and methods in the class as the main thread and can change variables as well. – Note no variable is needed, until the main threads need to interact with the thread. public class selfthread implements runnable{ //variables and other methods void event() { //something happened new Thread(this).start(); //main thread continues and new thread has been created } void run() { //do something, while the main thread is also running. }

But… Android doesn't allow a thread other the UI thread to access the “screen”. – The activity (and fragment) are on the UI thread. – We use a handler method to send "messages" back to the main thread – You can also use a AsyncTask method as well. This allows you to run a “thread” and communicate with the UI thread as well.

APPLICATION/ACTIVITY/FRAGMENT S

Fire hose It’s going to take several lectures to get you all the information about what’s going on – Activities – Fragments (and callbacks) – GUI objects/views (ie buttons and stuff) – intents. Then add in broadcast receiver, notification bar/action bar/tool bar, content providers, and services Then it will come together in hopefully something that makes sense as a whole.

Android Applications Android applications normally have one or more of the following: – Activity: Provides a screen which a user interacts with. An Activity can also start other activities, including activities in separate applications – Service: an application component that can perform long-running operations in the background without a user interface – Fragment: a distinct part of an activity’s behavior, part of it UI (We’ll cover this better later on)

Android Applications (2) – BroadcastReceiver This is way to receive information from other applications and the OS. – ContentProvider You app is providing information to another app and itself via a encapsulated data structure Where the data is stored or how it is stored is not relevant to the provider. – The data maybe on the internet, local file, database, etc.

Abstractly Activity and xml layout – An activity (java code) will normally have a xml layout of how the screen will look. public class MainActivity extends Activity { Button protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Where activity_main describes the layout and button with the text click me.

Activities Activity is the basic version. There is are also – ListActivity Which is an Activity with a built in ListView – PreferenceActivity Which is for user preferences for the activity » Data is stored between runs as well. – Many more except…

Activities (2) activities and API 13+ (HoneyComb, jellybean, etc) All but the basic Activity have been deprecated In API level 13 – Android wants you to use Fragments instead. Also dialogs have been deprecated as well for fragment dialogs. – There is a support.v4 library for api 4 and above so you can write compatible code from android 1.6+

Android… As in the gui example, the “main” class extends an Activity – This gives us access to the android system. – But… Google/Android really wants everyone to use fragments. It allows you to encapsulate everything into the “fragment” and use activity to display different fragments to the screen. But google also breaks this requirement themselves in lot of example code. – For the moment, I’m going to ignore fragments and get you going. It all transfers over to fragments – And we need the support library for (and before)

Abstractly (2) You have an activity with a xml layout – The xml layout will likely have one or more framelayouts (or fragments) A Framelayout can hold fragments. A fragment (java code) also has a layout. – The fragment and layout encapsulate a “section” of a screen, since you have more then more fragment displayed at a time.

Abstractly (3) Most fragments also callbacks and setters/constructors – The setters/constructors are so the activity can pass information to the fragment or information from another fragment. – The callbacks are so a fragment can pass information to another fragment. This is by via the activity. The activity implements the callbacks. The activity is the go between. It is also how we change between fragments.

Abstractly (4) You can also have multiple activities (which could use the same fragments) – When you activity start it receives an intent. That intent may contain data. – You activity can start another activity, creating an intent and adding data to it as well. – When an activity finishes it an also return data (via another intent) as well. – You can also have you activity started later via the system with an intent (or pendingintent) that you create. Again, you normally include data in the intent as well. Intents also carry with the any permissions your activity has requested.

AndroidManifest.xml file – Each new Activity must be registered in your manifest file – Inside the section You can also add intent-filter as well if you want specify different types of intents that can launch this Activity

Services and Broadcast Receivers As you will see in later lectures, an Activity can be launched by a using Receivers – A receiver doesn’t have a screen and can deal with the data in the background (via an intent) and then launch an activity Services are similar to Receivers, except there are normally running in the background, likely started at boot time.

Notification status bar The notification system can also be used to launch a Activity (with user action). – Uses the Intent again, to choose which Activity in your application is to launched. – See the Status Notification lecture for more details.

Closing activities (2) If you launched an Activity with startActivityForResult() and you want to end it – finishActivity(requestCode) will finish any activity with that requestCode Otherwise, you can always exist an activity with finish();

MODEL-VIEW-CONTROLLER

A note. MVC (or maybe MVP Model-View-Presenter) – Is a very good coding style, software engineering model Makes things easier later on as well. The downside, it will take up more memory. – On memory bound devices like older phones, this maybe an issue. – There is a argument going on over good coding style vs small code size.

Model View controller Model – Represents the knowledge, ie data and maybe the “business logic” The data could be a single object or a “structure” of objects – Simple as a java class to a database or data on another system. – There is no controller or view code in the model! View – Is the Visual representation The GUI interface, Layout (in android), html is another example. Controller – Ties the view and the model together. Deal with input, etc. – Contains the application logic The activity/fragment/etc in android For html, the browser, javascript, maybe even the backend code on the server.

Advantages. As the program becomes more complex and accumulates “features” the program becomes harder to understand and update. Separating the data from the code allows an easy translation to different data structures or even data types. – Also data is harder to “lose”, since it is all in the same place. – Also reuse of data/code. The data doesn’t need to know anything about the view/widgets of an android program. – You can delete/change a field and no worry about data.

mvcDemo Model  color List class – Holds all the data about colors to be used. View  activity_main.xml (layout file) Controller  MainActivity – Listeners and code to tie the view to the model.

We’ll take a look at the code.

References Colors: Color.htm Color.htm MVC information – model-view-controller/ model-view-controller/ – design/model-view-controller-explained/ design/model-view-controller-explained/ – ew%E2%80%93controller ew%E2%80%93controller

Q A &