SlideShare a Scribd company logo
Fragments
A Fragment is a piece of an activity which enable
more modular activity design. It will not be wrong
if we say, a fragment is a kind of sub-activity
Design Philosophy
Fragments life cycle
Example:
http://examples.javacodegeeks.com/android/core/app/fra
gment/android-fragments-example/
Toast
• A toast provides simple feedback about an operation in a
small popup. It only fills the amount of space required for the
message and the current activity remains visible and
interactive. For example, navigating away from an email
before you send it triggers a "Draft saved" toast to let you
know that you can continue editing later. Toasts automatically
disappear after a timeout.
• EX:
Context context = getApplicationContext();
CharSequence text = "Hello toast!";
int duration = Toast.LENGTH_SHORT;
Toast toast = Toast.makeText(context, text, duration);
toast.show();
Dialog
• A dialog is a small window that prompts the user to make a decision or
enter additional information. A dialog does not fill the screen and is
normally used for modal events that require users to take an action before
they can proceed.
Example:
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
// Add the buttons
builder.setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// User clicked OK button
}
});
builder.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// User cancelled the dialog
}
});
// Set other dialog properties
...
// Create the AlertDialog
AlertDialog dialog = builder.create();
Notification
• A notification is a message you can display to the user outside of your application's
normal UI. When you tell the system to issue a notification, it first appears as an
icon in the notification area. To see the details of the notification, the user opens
the notification drawer. Both the notification area and the notification drawer are
system-controlled areas that the user can view at any time.
Services
• A service is an application component that can perform long-running
operations in the background and does not provide a user interface.
Another application component can start a service and it will continue
to run in the background even if the user switches to another
application. Additionally, a component can bind to a service to interact
with it and even perform interprocess communication (IPC). For
example, a service might handle network transactions, play music,
perform file I/O, or interact with a content provider, all from the
background.
Services life cycle
Implementing a Services
Android Trainning  Session 2
Connecting to the Network
• your application manifest must include the following
permissions:
– <uses-permission
android:name="android.permission.INTERNET" />
– <uses-permission
android:name="android.permission.ACCESS_NETWORK_STA
TE" />
• Choose an HTTP Client:
– Most network-connected Android apps use HTTP to send
and receive data. Android includes two HTTP clients:
HttpURLConnection and Apache HttpClient. Both support
HTTPS, streaming uploads and downloads, configurable
timeouts, IPv6, and connection pooling.
– HttpURLConnection => for applications targeted at
Gingerbread and higher.
Check the Network Connection
public void myClickHandler(View view) {
...
ConnectivityManager connMgr = (ConnectivityManager)
getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = connMgr.getActiveNetworkInfo();
if (networkInfo != null && networkInfo.isConnected()) {
// fetch data
} else {
// display error
}
...
}
AsyncTask
• AsyncTask is an abstract class provided by Android which helps us
to use the UI thread properly. This class allows us to perform
long/background operations and show its result on the UI thread
without having to manipulate threads.
• AsyncTask has four steps:
– doInBackground: Code performing long running operation goes in this
method. When onClick method is executed on click of button, it
calls execute method which accepts parameters and automatically calls
doInBackground method with the parameters passed.
– onPostExecute: This method is called after doInBackground method
completes processing. Result from doInBackground is passed to this
method.
– onPreExecute: This method is called before doInBackground method is
called.
– onProgressUpdate: This method is invoked by
calling publishProgress anytime from doInBackground call this method.
JSON parsing
• JSON stands for JavaScript Object Notation.It is an independent data exchange format and is the
best alternative for XML.
• Ex:
{
"sys":
{
"country":"GB",
"sunrise":1381107633,
"sunset":1381149604
},
"weather":[
{
"id":711,
"main":"Smoke",
"description":"smoke",
"icon":"50n"
}
],
"main":
{
"temp":304.15,
"pressure":1009,
}
}
Android Trainning  Session 2
Broadcast Receiver
Broadcast Receivers simply respond to broadcast
messages from other applications or from the system
itself. These messages are sometime called events or
intents. For example, applications can also initiate
broadcasts to let other applications know that some data
has been downloaded to the device and is available for
them to use, so this is broadcast receiver who will
intercept this communication and will initiate appropriate
action
• There are following two important steps to make
Broadcast Receiver works for the system broadcasted
intents −
– Creating the Broadcast Receiver.
– Registering Broadcast Receiver
Creating the Broadcast Receiver:
A broadcast receiver is implemented as a subclass of BroadcastReceiver class and
overriding the onReceive() method where each message is received as a Intent object
parameter.
public class MyReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(context, "Intent Detected.", Toast.LENGTH_LONG).show();
}
}
Registering Broadcast Receiver:
An application listens for specific broadcast intents by registering a broadcast receiver
in AndroidManifest.xml file. Consider we are going to register MyReceiver for system
generated event ACTION_BOOT_COMPLETED which is fired by the system once the
Android system has completed the boot process.
public void broadcastIntent(View view)
{
Intent intent = new Intent();
intent.setAction("com.ex.CUSTOM_INTENT");
sendBroadcast(intent);
}
Mainfest.xml :
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<receiver android:name="MyReceiver">
<intent-filter>
<action android:name=“com.ex.CUSTOM_INTENT">
</action>
</intent-filter
Content Provider
• A content provider component supplies data from one application to
others on request. Such requests are handled by the methods of the
ContentResolver class. A content provider can use different ways to
store its data and the data can be stored in a database, in files, or
even over a network.
public class My Application extends ContentProvider {
}
Create Content Provider
This involves number of simple steps to create your own content provider.
1. First of all you need to create a Content Provider class that extends the
ContentProviderbaseclass.
2. Second, you need to define your content provider URI address which will be used to access
the content.
3. Next you will need to create your own database to keep the content. Usually, Android uses
SQLite database and framework needs to override onCreate()method which will use SQLite
Open Helper method to create or open the provider's database. When your application is
launched, the onCreate() handler of each of its Content Providers is called on the main
application thread.
4. Next you will have to implement Content Provider queries to perform different database
specific operations.
5. Finally register your Content Provider in your activity file using <provider> tag.
Mainfest.xml:
<provider android:name="StudentsProvider"
<android:authorities="com.example.provider.College">
</provider>
Data Storage
Android provides several options for you to save
persistent application data. The solution you choose
depends on your specific needs, such as whether the
data should be private to your application or
accessible to other applications (and the user) and
how much space your data requires.
STORAGE OPTIONS
Data Backup
Android’s backup service allows you to copy your persistent
application data to remote "cloud" storage, in order to
provide a restore point for the application data and settings. If
a user performs a factory reset or converts to a new Android-
powered device, the system automatically restores your
backup data when the application is re-installed. This way,
your users don't need to reproduce their previous data or
application settings. This process is completely transparent to
the user and does not affect the functionality or user
experience in your application.
Android Trainning  Session 2
Location API
Specify App Permissions:
<manifest
xmlns:android="http://schemas.android.com/apk/res/andr
oid"
package="com.google.android.gms.location.sample.basicl
ocationsample" >
<uses-permission
android:name="android.permission.ACCESS_FINE_LOCATIO
N"/>
<uses-permission
android:name="android.permission.ACCESS_COARSE_LOCA
TION"/>
</manifest>
END

More Related Content

What's hot (20)

PDF
Android contentprovider
Krazy Koder
 
PDF
Day 8: Dealing with Lists and ListViews
Ahsanul Karim
 
PDF
Communication Diagram
University of Texas at Dallas
 
PDF
Secure Sharing PHI PCI PII -Android app_Content Provider
Avinash Sinha
 
PPT
Day 4: Android: UI Widgets
Ahsanul Karim
 
PPTX
Android Services
Ahsanul Karim
 
PPT
Android Bootcamp Tanzania:intents
Denis Minja
 
DOC
Ado
actacademy
 
ODP
Android App Development - 02 Activity and intent
Diego Grancini
 
PDF
Learn Drupal 8 Render Pipeline
Zyxware Technologies
 
PPTX
Android development session 2 - intent and activity
Farabi Technology Middle East
 
PDF
Murach: How to use Entity Framework EF Core
MahmoudOHassouna
 
PPT
Android - Android Intent Types
Vibrant Technologies & Computers
 
PDF
Asp net interview_questions
Bilam
 
PDF
Murach: How to transfer data from controllers
MahmoudOHassouna
 
PPTX
Recovered file 1
Uthara Iyer
 
PPTX
Android Tutorials : Basic widgets
Prajyot Mainkar
 
PDF
Android intents
Siva Ramakrishna kv
 
PPTX
Activity & Shared Preference
nationalmobileapps
 
PPTX
Murach : HOW to work with controllers and routing
MahmoudOHassouna
 
Android contentprovider
Krazy Koder
 
Day 8: Dealing with Lists and ListViews
Ahsanul Karim
 
Communication Diagram
University of Texas at Dallas
 
Secure Sharing PHI PCI PII -Android app_Content Provider
Avinash Sinha
 
Day 4: Android: UI Widgets
Ahsanul Karim
 
Android Services
Ahsanul Karim
 
Android Bootcamp Tanzania:intents
Denis Minja
 
Android App Development - 02 Activity and intent
Diego Grancini
 
Learn Drupal 8 Render Pipeline
Zyxware Technologies
 
Android development session 2 - intent and activity
Farabi Technology Middle East
 
Murach: How to use Entity Framework EF Core
MahmoudOHassouna
 
Android - Android Intent Types
Vibrant Technologies & Computers
 
Asp net interview_questions
Bilam
 
Murach: How to transfer data from controllers
MahmoudOHassouna
 
Recovered file 1
Uthara Iyer
 
Android Tutorials : Basic widgets
Prajyot Mainkar
 
Android intents
Siva Ramakrishna kv
 
Activity & Shared Preference
nationalmobileapps
 
Murach : HOW to work with controllers and routing
MahmoudOHassouna
 

Similar to Android Trainning Session 2 (20)

PDF
04 programmation mobile - android - (db, receivers, services...)
TECOS
 
PPTX
02. Android application development_Lec2.pptx
anychowdhury2
 
PDF
Android101
David Marques
 
PPTX
Data Transfer between Activities & Databases
Muhammad Sajid
 
PPTX
Basics 4
Michael Shrove
 
PDF
INTRODUCTION TO FORMS OF SERVICES AND ITS LIFE CYCLE
Kalpana Mohan
 
PPTX
Android - Activity, Services
Dr Karthikeyan Periasamy
 
PPTX
App Fundamentals and Activity life cycle.pptx
ridzah12
 
PDF
Mobile Application Development -Lecture 09 & 10.pdf
AbdullahMunir32
 
PPTX
Introduction to Android Development
Owain Lewis
 
PPTX
MAD Unit 5.pptxxxxxxxxxxxxxxxxxxxxxxxxxx
34ShreyaChauhan
 
PPT
Android overview
Ashish Agrawal
 
PPTX
Android developer fundamentals training overview Part II
Yoza Aprilio
 
PPTX
Communication in android
eleksdev
 
PDF
Android Development Tutorial
Germán Bringas
 
PPTX
Android beginners David
Arun David Johnson R
 
PPTX
Android Application Development
Azfar Siddiqui
 
PPT
Synapseindia android apps overview
Synapseindiappsdevelopment
 
PPTX
Data Transfer between activities and Database
faiz324545
 
PPTX
Android application componenets for android app development
BalewKassie
 
04 programmation mobile - android - (db, receivers, services...)
TECOS
 
02. Android application development_Lec2.pptx
anychowdhury2
 
Android101
David Marques
 
Data Transfer between Activities & Databases
Muhammad Sajid
 
Basics 4
Michael Shrove
 
INTRODUCTION TO FORMS OF SERVICES AND ITS LIFE CYCLE
Kalpana Mohan
 
Android - Activity, Services
Dr Karthikeyan Periasamy
 
App Fundamentals and Activity life cycle.pptx
ridzah12
 
Mobile Application Development -Lecture 09 & 10.pdf
AbdullahMunir32
 
Introduction to Android Development
Owain Lewis
 
MAD Unit 5.pptxxxxxxxxxxxxxxxxxxxxxxxxxx
34ShreyaChauhan
 
Android overview
Ashish Agrawal
 
Android developer fundamentals training overview Part II
Yoza Aprilio
 
Communication in android
eleksdev
 
Android Development Tutorial
Germán Bringas
 
Android beginners David
Arun David Johnson R
 
Android Application Development
Azfar Siddiqui
 
Synapseindia android apps overview
Synapseindiappsdevelopment
 
Data Transfer between activities and Database
faiz324545
 
Android application componenets for android app development
BalewKassie
 
Ad

Android Trainning Session 2

  • 1. Fragments A Fragment is a piece of an activity which enable more modular activity design. It will not be wrong if we say, a fragment is a kind of sub-activity
  • 5. Toast • A toast provides simple feedback about an operation in a small popup. It only fills the amount of space required for the message and the current activity remains visible and interactive. For example, navigating away from an email before you send it triggers a "Draft saved" toast to let you know that you can continue editing later. Toasts automatically disappear after a timeout. • EX: Context context = getApplicationContext(); CharSequence text = "Hello toast!"; int duration = Toast.LENGTH_SHORT; Toast toast = Toast.makeText(context, text, duration); toast.show();
  • 6. Dialog • A dialog is a small window that prompts the user to make a decision or enter additional information. A dialog does not fill the screen and is normally used for modal events that require users to take an action before they can proceed.
  • 7. Example: AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); // Add the buttons builder.setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int id) { // User clicked OK button } }); builder.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int id) { // User cancelled the dialog } }); // Set other dialog properties ... // Create the AlertDialog AlertDialog dialog = builder.create();
  • 8. Notification • A notification is a message you can display to the user outside of your application's normal UI. When you tell the system to issue a notification, it first appears as an icon in the notification area. To see the details of the notification, the user opens the notification drawer. Both the notification area and the notification drawer are system-controlled areas that the user can view at any time.
  • 9. Services • A service is an application component that can perform long-running operations in the background and does not provide a user interface. Another application component can start a service and it will continue to run in the background even if the user switches to another application. Additionally, a component can bind to a service to interact with it and even perform interprocess communication (IPC). For example, a service might handle network transactions, play music, perform file I/O, or interact with a content provider, all from the background.
  • 13. Connecting to the Network • your application manifest must include the following permissions: – <uses-permission android:name="android.permission.INTERNET" /> – <uses-permission android:name="android.permission.ACCESS_NETWORK_STA TE" /> • Choose an HTTP Client: – Most network-connected Android apps use HTTP to send and receive data. Android includes two HTTP clients: HttpURLConnection and Apache HttpClient. Both support HTTPS, streaming uploads and downloads, configurable timeouts, IPv6, and connection pooling. – HttpURLConnection => for applications targeted at Gingerbread and higher.
  • 14. Check the Network Connection public void myClickHandler(View view) { ... ConnectivityManager connMgr = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo networkInfo = connMgr.getActiveNetworkInfo(); if (networkInfo != null && networkInfo.isConnected()) { // fetch data } else { // display error } ... }
  • 15. AsyncTask • AsyncTask is an abstract class provided by Android which helps us to use the UI thread properly. This class allows us to perform long/background operations and show its result on the UI thread without having to manipulate threads. • AsyncTask has four steps: – doInBackground: Code performing long running operation goes in this method. When onClick method is executed on click of button, it calls execute method which accepts parameters and automatically calls doInBackground method with the parameters passed. – onPostExecute: This method is called after doInBackground method completes processing. Result from doInBackground is passed to this method. – onPreExecute: This method is called before doInBackground method is called. – onProgressUpdate: This method is invoked by calling publishProgress anytime from doInBackground call this method.
  • 16. JSON parsing • JSON stands for JavaScript Object Notation.It is an independent data exchange format and is the best alternative for XML. • Ex: { "sys": { "country":"GB", "sunrise":1381107633, "sunset":1381149604 }, "weather":[ { "id":711, "main":"Smoke", "description":"smoke", "icon":"50n" } ], "main": { "temp":304.15, "pressure":1009, } }
  • 18. Broadcast Receiver Broadcast Receivers simply respond to broadcast messages from other applications or from the system itself. These messages are sometime called events or intents. For example, applications can also initiate broadcasts to let other applications know that some data has been downloaded to the device and is available for them to use, so this is broadcast receiver who will intercept this communication and will initiate appropriate action • There are following two important steps to make Broadcast Receiver works for the system broadcasted intents − – Creating the Broadcast Receiver. – Registering Broadcast Receiver
  • 19. Creating the Broadcast Receiver: A broadcast receiver is implemented as a subclass of BroadcastReceiver class and overriding the onReceive() method where each message is received as a Intent object parameter. public class MyReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { Toast.makeText(context, "Intent Detected.", Toast.LENGTH_LONG).show(); } }
  • 20. Registering Broadcast Receiver: An application listens for specific broadcast intents by registering a broadcast receiver in AndroidManifest.xml file. Consider we are going to register MyReceiver for system generated event ACTION_BOOT_COMPLETED which is fired by the system once the Android system has completed the boot process.
  • 21. public void broadcastIntent(View view) { Intent intent = new Intent(); intent.setAction("com.ex.CUSTOM_INTENT"); sendBroadcast(intent); } Mainfest.xml : <application android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <receiver android:name="MyReceiver"> <intent-filter> <action android:name=“com.ex.CUSTOM_INTENT"> </action> </intent-filter
  • 22. Content Provider • A content provider component supplies data from one application to others on request. Such requests are handled by the methods of the ContentResolver class. A content provider can use different ways to store its data and the data can be stored in a database, in files, or even over a network.
  • 23. public class My Application extends ContentProvider { }
  • 24. Create Content Provider This involves number of simple steps to create your own content provider. 1. First of all you need to create a Content Provider class that extends the ContentProviderbaseclass. 2. Second, you need to define your content provider URI address which will be used to access the content. 3. Next you will need to create your own database to keep the content. Usually, Android uses SQLite database and framework needs to override onCreate()method which will use SQLite Open Helper method to create or open the provider's database. When your application is launched, the onCreate() handler of each of its Content Providers is called on the main application thread. 4. Next you will have to implement Content Provider queries to perform different database specific operations. 5. Finally register your Content Provider in your activity file using <provider> tag. Mainfest.xml: <provider android:name="StudentsProvider" <android:authorities="com.example.provider.College"> </provider>
  • 25. Data Storage Android provides several options for you to save persistent application data. The solution you choose depends on your specific needs, such as whether the data should be private to your application or accessible to other applications (and the user) and how much space your data requires.
  • 27. Data Backup Android’s backup service allows you to copy your persistent application data to remote "cloud" storage, in order to provide a restore point for the application data and settings. If a user performs a factory reset or converts to a new Android- powered device, the system automatically restores your backup data when the application is re-installed. This way, your users don't need to reproduce their previous data or application settings. This process is completely transparent to the user and does not affect the functionality or user experience in your application.
  • 30. Specify App Permissions: <manifest xmlns:android="http://schemas.android.com/apk/res/andr oid" package="com.google.android.gms.location.sample.basicl ocationsample" > <uses-permission android:name="android.permission.ACCESS_FINE_LOCATIO N"/> <uses-permission android:name="android.permission.ACCESS_COARSE_LOCA TION"/> </manifest>
  • 31. END