Mobile Programming Dr. Mohsin Ali Memon.

Slides:



Advertisements
Similar presentations
Programming with Android: Android for Tablets Luca Bedogni Marco Di Felice Dipartimento di Scienze dellInformazione Università di Bologna.
Advertisements

1 Working with the Bluetooth radio Nilanjan Banerjee Mobile Systems Programming University of Arkansas Fayetteville, AR
Services. Application component No user interface Two main uses Performing background processing Supporting remote method execution.
Programming with Android: Android for Tablets Luca Bedogni Marco Di Felice Dipartimento di Scienze dell’Informazione Università di Bologna.
Fragments: Introduction Fragments were introduced in Android 3.0 to support flexible and dynamic UI designs represent portions of an application’s user.
Android 02: Activities David Meredith
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.
The Android Activity Lifecycle. Slide 2 Introduction Working with the Android logging system Rotation and multiple layouts Understanding the Android activity.
Android Fragments.
Bluetooth. Bluetooth is an open, wireless protocol for exchanging data between devices over a short distance. –managed by the Bluetooth Special Interest.
Programming with Android: Android Fragments Luca Bedogni Marco Di Felice Dipartimento di Scienze dell’Informazione Università di Bologna.
Android Fragments A very brief introduction Android Fragments1.
Chien-Chung Shen Manifest and Activity Chien-Chung Shen
ANDROID UI – FRAGMENTS. Fragment  An activity is a container for views  When you have a larger screen device than a phone –like a tablet it can look.
COMP 365 Android Development.  Perform operations in the background  Services do not have a user interface (UI)  Can run without appearing on screen.
This work is licensed under the Creative Commons Attribution 4.0 International License. To view a copy of this license, visit
Mobile Application Development using Android Lecture 2.
16 Services and Broadcast Receivers CSNB544 Mobile Application Development Thanks to Utexas Austin.
SpotOn Game App Android How to Program © by Pearson Education, Inc. All Rights Reserved.
Understand applications and their components activity service broadcast receiver content provider intent AndroidManifest.xml.
Android – Fragments L. Grewe.
Services A Service is an application component that can perform long-running operations in the background and does not provide a user interface. An application.
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.
Applications with Multiple Activities. Most applications will have more than one activity. The main activity is started when the application is started.
DKU-MUST Mobile ICT Education Center 10. Activity and Intent.
Services Background operating component without a visual interface Running in the background indefinitely Differently from Activity, Service in Android.
Class on Fragments & threads. Fragments fragment is a modular section of an activity, which has its own lifecycle, receives its own input events, and.
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,
Android Fragments. Slide 2 Lecture Overview Getting resources and configuration information Conceptualizing the Back Stack Introduction to fragments.
The Flag Quiz app tests your ability to correctly identify 10 flags from various countries and territories.
Developing Android Services. Objectives Creating a service that runs in background Performing long-running tasks in a separate thread Performing repeated.
Android Application -Architecture.
Mobile Applications (Android Programming)
Bluetooth.
Activity and Fragment.
CS371m - Mobile Computing Services and Broadcast Receivers
Lecture 7: Service Topics: Services, Playing Media.
Instructor: Mazhar Hussain
Android Studio, Android System Basics and Git
Activities, Fragments, and Events
Fragment ?.
Fragments: Introduction
CS499 – Mobile Application Development
Mobile Application Development BSCS-7 Lecture # 6
Mobile Applications (Android Programming)
Activities and Intents
Anatomy of an Android App and the App Lifecycle
Widgets & Fragments Kalin Kadiev Astea Solutions AD.
Android Mobile Application Development
Mobile Application Development Chapter 4 [Android Navigation and Interface Design] IT448-Fall 2017 IT448- Fall2017.
The Android Activity Lifecycle
Android – Fragments L. Grewe.
ANDROID UI – FRAGMENTS UNIT II.
Android Application Development android.cs.uchicago.edu
Android Programming Lecture 9
Developing Android Services
Anatomy of an Android App and the App Lifecycle
Application Development A Tutorial Driven Course
Activities and Intents
HNDIT2417 Mobile Application Development
Activities and Intents
Lecture 7: Service Topics: Services, Broadcast Receiver, Playing Media.
Mobile Programming Dr. Mohsin Ali Memon.
SE4S701 Mobile Application Development
Activities and Fragments
Android Development Tools
Preference Activity class
Activities, Fragments, and Intents
CIS 470 Mobile App Development
Presentation transcript:

Mobile Programming Dr. Mohsin Ali Memon

Bluetooth in Android Bluetooth is a way to send or receive data between two different devices. Android platform includes support for the Bluetooth framework that allows a device to wirelessly exchange data with other Bluetooth devices. Android provides Bluetooth API to perform these different operations. Scan for other Bluetooth devices Get a list of paired devices Connect to other devices through service discovery

BluetoothAdapter Class Android provides BluetoothAdapter class to communicate with Bluetooth. Create an object of this calling by calling the static method getDefaultAdapter(). private BluetoothAdapter BA; BA = BluetoothAdapter.getDefaultAdapter();

Calling Intent In order to enable the Bluetooth of your device, call the intent with the following Bluetooth constant ACTION_REQUEST_ENABLE.  Intent turnOn = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE); startActivityForResult(turnOn, 0);

Calling other Bluetooth Intents Apart from this constant , there are other constants provided the API , that supports different tasks. ACTION_REQUEST_DISCOVERABLE This constant is used for turn on discovering of bluetooth ACTION_STATE_CHANGED This constant will notify that Bluetooth state has been changed ACTION_FOUND This constant is used for receiving information about each device that is discovered

List of Paired devices Once you enable the Bluetooth , you can get a list of paired devices by calling getBondedDevices() method. It returns a set of bluetooth devices.  private Set<BluetoothDevice>pairedDevices; pairedDevices = BA.getBondedDevices();

Methods in Bluetooth API Apart form the pariredDevices , there are other methods in the API that gives more control over Blueetooth. enable() This method enables the adapter if not enabled isEnabled() This method returns true if adapter is enabled disable() This method disables the adapter

Methods in Bluetooth API cont’d getName() This method returns the name of the Bluetooth adapter setName(String name) This method changes the Bluetooth name getState() This method returns the current state of the Bluetooth Adapter. startDiscovery() This method starts the discovery process of the Bluetooth for 120 seconds.

Example This example provides demonstration of BluetoothAdapter class to manipulate Bluetooth and show list of paired devices by the Bluetooth. To experiment with this example , you need to run this on an actual device.

Main Activity

Main Activity cont’d

Main Activity cont’d

Layout

Layout cont’d

Layout cont’d

Manifest

Snapshots

Snapshots

Android Services Services in Android is a component that runs in the background without any user interaction or interface. Unlike Activities, services do not have any graphical interface. Services run invisibly performing long running tasks - doing Internet look ups, playing music, triggering notifications etc. Services run with a higher priority than inactive or invisible activities and therefore it is less likely that the Android system terminates them for resource management.

Services Template The only reason Android will stop a Service prematurely is to provide additional resources for a foreground component usually an Activity. When this happens, your Service can be configured to restart automatically. 

Two forms of services Services takes Two Forms Started  A Service is started when a application component such as an activity invokes it by calling startService(). Once started, a service can run in the background indefinitely, even if the component that started it is destroyed Add the service to AndroidManifest.xml

Two forms of services cont’d Bound If the activity wants to interact with the service directly, it can use the bindService() method to start the service. This method requires a ServiceConnection object as a parameter which is called on the service start and when finishing its onBind() method. This method returns a IBinder object to the ServiceConnection. This IBinder object can be used by the activity to communicate with the service.

Main Activity

Example Layout

Services class

Manifest

Snapshots

Snapshots cont’d

Android Fragments A Fragment is a piece of an application's user interface or behavior that can be placed in an Activity which enable more modular activity design. A fragment has its own layout and its own behavior with its own lifecycle callbacks. You can add or remove fragments in an activity while the activity is running. You can combine multiple fragments in a single activity to build a multi-pane UI.

Fragments cont’d A fragment can be used in multiple activities. Fragment life cycle is closely related to the lifecycle of its host activity which means when the activity is paused, all the fragments available in the activity will also be stopped. Fragment are more flexible and removed the limitation of having a single activity on the screen at a time

Tablet and Handset Fragment

Fragment Lifecycle

Fragment Lifecycle cont’d

How to use Fragments First of all decide how many fragments you want to use in an activity. Next based on number of fragments, create classes which will extend the Fragment class. The Fragment class has above mentioned callback functions. You can override any of the functions based on your requirements. Corresponding to each fragment, you will need to create layout files. Finally modify activity file to define the actual logic of replacing fragments based on your requirement.

Methods to Override onCreate() The system calls this when creating the fragment. You should initialize essential components of the fragment that you want to retain when the fragment is paused or stopped, then resumed. onCreateView() It is called when it's time for the fragment to draw user interface for the first time. onPause() The system calls this method as the first indication that the user is leaving the fragment. This is usually where you should commit any changes that should be persisted beyond the current user session.

Managing Fragments To manage the fragments in your activity, you need to use FragmentManager. To get it, call getFragmentManager() from your activity. With fragments you can Get fragments that exist in the activity, with findFragmentById() (for fragments that provide a UI in the activity layout) or findFragmentByTag() (for fragments that do or don't provide a UI). Pop fragments off the back stack, with popBackStack() (simulating a Back command by the user). Register a listener for changes to the back stack, with addOnBackStackChangedListener().

Performing Fragment Transactions A great feature about using fragments in your activity is the ability to add, remove, replace, and perform other actions with them, in response to user interaction. Each set of changes that you commit to the activity is called a transaction and you can perform one using APIs in FragmentTransaction. You can also save each transaction to a back stack managed by the activity, allowing the user to navigate backward through the fragment changes (similar to navigating backward through activities).

Performing Fragment Transactions Each transaction is a set of changes that you want to perform at the same time. You can set up all the changes you want to perform for a given transaction using methods such as add(), remove(), and replace(). Then, to apply the transaction to the activity, you must call commit(). Before you call commit(), however, you might want to call addToBackStack(), in order to add the transaction to a back stack of fragment transactions. The back stack is managed by the activity and allows the user to return to the previous fragment state, by pressing the Back button.

Example

Fragment classes

Fragment Layouts

Main Activity Layout

String.xml