SlideShare a Scribd company logo
1 of 31
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
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,
}
}
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.
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

Android contentprovider
Android contentproviderAndroid contentprovider
Android contentproviderKrazy Koder
 
Day 8: Dealing with Lists and ListViews
Day 8: Dealing with Lists and ListViewsDay 8: Dealing with Lists and ListViews
Day 8: Dealing with Lists and ListViewsAhsanul Karim
 
Secure Sharing PHI PCI PII -Android app_Content Provider
Secure Sharing PHI PCI PII -Android app_Content ProviderSecure Sharing PHI PCI PII -Android app_Content Provider
Secure Sharing PHI PCI PII -Android app_Content ProviderAvinash Sinha
 
Day 4: Android: UI Widgets
Day 4: Android: UI WidgetsDay 4: Android: UI Widgets
Day 4: Android: UI WidgetsAhsanul Karim
 
Android Bootcamp Tanzania:intents
Android Bootcamp Tanzania:intentsAndroid Bootcamp Tanzania:intents
Android Bootcamp Tanzania:intentsDenis Minja
 
Android App Development - 02 Activity and intent
Android App Development - 02 Activity and intentAndroid App Development - 02 Activity and intent
Android App Development - 02 Activity and intentDiego Grancini
 
Murach: How to use Entity Framework EF Core
Murach: How to use Entity Framework EF  CoreMurach: How to use Entity Framework EF  Core
Murach: How to use Entity Framework EF CoreMahmoudOHassouna
 
Asp net interview_questions
Asp net interview_questionsAsp net interview_questions
Asp net interview_questionsBilam
 
Murach: How to transfer data from controllers
Murach: How to transfer data from controllersMurach: How to transfer data from controllers
Murach: How to transfer data from controllersMahmoudOHassouna
 
Recovered file 1
Recovered file 1Recovered file 1
Recovered file 1Uthara Iyer
 
Android Tutorials : Basic widgets
Android Tutorials : Basic widgetsAndroid Tutorials : Basic widgets
Android Tutorials : Basic widgetsPrajyot Mainkar
 
Murach : HOW to work with controllers and routing
Murach : HOW to work with controllers and routingMurach : HOW to work with controllers and routing
Murach : HOW to work with controllers and routingMahmoudOHassouna
 

What's hot (20)

Android contentprovider
Android contentproviderAndroid contentprovider
Android contentprovider
 
Day 8: Dealing with Lists and ListViews
Day 8: Dealing with Lists and ListViewsDay 8: Dealing with Lists and ListViews
Day 8: Dealing with Lists and ListViews
 
Communication Diagram
Communication DiagramCommunication Diagram
Communication Diagram
 
Secure Sharing PHI PCI PII -Android app_Content Provider
Secure Sharing PHI PCI PII -Android app_Content ProviderSecure Sharing PHI PCI PII -Android app_Content Provider
Secure Sharing PHI PCI PII -Android app_Content Provider
 
Day 4: Android: UI Widgets
Day 4: Android: UI WidgetsDay 4: Android: UI Widgets
Day 4: Android: UI Widgets
 
Android Services
Android ServicesAndroid Services
Android Services
 
Android Bootcamp Tanzania:intents
Android Bootcamp Tanzania:intentsAndroid Bootcamp Tanzania:intents
Android Bootcamp Tanzania:intents
 
Ado
AdoAdo
Ado
 
Android App Development - 02 Activity and intent
Android App Development - 02 Activity and intentAndroid App Development - 02 Activity and intent
Android App Development - 02 Activity and intent
 
Learn Drupal 8 Render Pipeline
Learn Drupal 8 Render PipelineLearn Drupal 8 Render Pipeline
Learn Drupal 8 Render Pipeline
 
Android development session 2 - intent and activity
Android development   session 2 - intent and activityAndroid development   session 2 - intent and activity
Android development session 2 - intent and activity
 
Murach: How to use Entity Framework EF Core
Murach: How to use Entity Framework EF  CoreMurach: How to use Entity Framework EF  Core
Murach: How to use Entity Framework EF Core
 
Android - Android Intent Types
Android - Android Intent TypesAndroid - Android Intent Types
Android - Android Intent Types
 
Asp net interview_questions
Asp net interview_questionsAsp net interview_questions
Asp net interview_questions
 
Murach: How to transfer data from controllers
Murach: How to transfer data from controllersMurach: How to transfer data from controllers
Murach: How to transfer data from controllers
 
Recovered file 1
Recovered file 1Recovered file 1
Recovered file 1
 
Android Tutorials : Basic widgets
Android Tutorials : Basic widgetsAndroid Tutorials : Basic widgets
Android Tutorials : Basic widgets
 
Android intents
Android intentsAndroid intents
Android intents
 
Activity & Shared Preference
Activity & Shared PreferenceActivity & Shared Preference
Activity & Shared Preference
 
Murach : HOW to work with controllers and routing
Murach : HOW to work with controllers and routingMurach : HOW to work with controllers and routing
Murach : HOW to work with controllers and routing
 

Similar to Android Trainning Session 2

Data Transfer between activities and Database
Data Transfer between activities and Database Data Transfer between activities and Database
Data Transfer between activities and Database faiz324545
 
Introduction to Java Enterprise Edition
Introduction to Java Enterprise EditionIntroduction to Java Enterprise Edition
Introduction to Java Enterprise EditionAbdalla Mahmoud
 
Mobile Application Development -Lecture 09 & 10.pdf
Mobile Application Development -Lecture 09 & 10.pdfMobile Application Development -Lecture 09 & 10.pdf
Mobile Application Development -Lecture 09 & 10.pdfAbdullahMunir32
 
OPEN TEXT ADMINISTRATION
OPEN TEXT ADMINISTRATIONOPEN TEXT ADMINISTRATION
OPEN TEXT ADMINISTRATIONSUMIT KUMAR
 
Azure + WP7 - CodePaLOUsa
Azure + WP7 - CodePaLOUsaAzure + WP7 - CodePaLOUsa
Azure + WP7 - CodePaLOUsaSam Basu
 
Data Transfer between Activities & Databases
Data Transfer between Activities & DatabasesData Transfer between Activities & Databases
Data Transfer between Activities & DatabasesMuhammad Sajid
 
Android Jumpstart Jfokus
Android Jumpstart JfokusAndroid Jumpstart Jfokus
Android Jumpstart JfokusLars Vogel
 
Android application development
Android application developmentAndroid application development
Android application developmentMd. Mujahid Islam
 
Android Application Components
Android Application ComponentsAndroid Application Components
Android Application ComponentsJAINAM KAPADIYA
 
Andriod Lecture 8 A.pptx
Andriod Lecture 8 A.pptxAndriod Lecture 8 A.pptx
Andriod Lecture 8 A.pptxfaiz324545
 
Android service, aidl - day 1
Android service, aidl - day 1Android service, aidl - day 1
Android service, aidl - day 1Utkarsh Mankad
 

Similar to Android Trainning Session 2 (20)

Data Transfer between activities and Database
Data Transfer between activities and Database Data Transfer between activities and Database
Data Transfer between activities and Database
 
Chapter 6-Remoting
Chapter 6-RemotingChapter 6-Remoting
Chapter 6-Remoting
 
Meet with Meteor
Meet with MeteorMeet with Meteor
Meet with Meteor
 
Basics 4
Basics   4Basics   4
Basics 4
 
Introduction to Java Enterprise Edition
Introduction to Java Enterprise EditionIntroduction to Java Enterprise Edition
Introduction to Java Enterprise Edition
 
Android Development Basics
Android Development BasicsAndroid Development Basics
Android Development Basics
 
Basic API Creation with Node.JS
Basic API Creation with Node.JSBasic API Creation with Node.JS
Basic API Creation with Node.JS
 
Mobile Application Development -Lecture 09 & 10.pdf
Mobile Application Development -Lecture 09 & 10.pdfMobile Application Development -Lecture 09 & 10.pdf
Mobile Application Development -Lecture 09 & 10.pdf
 
OPEN TEXT ADMINISTRATION
OPEN TEXT ADMINISTRATIONOPEN TEXT ADMINISTRATION
OPEN TEXT ADMINISTRATION
 
Lecture 10.pptx
Lecture 10.pptxLecture 10.pptx
Lecture 10.pptx
 
Azure + WP7 - CodePaLOUsa
Azure + WP7 - CodePaLOUsaAzure + WP7 - CodePaLOUsa
Azure + WP7 - CodePaLOUsa
 
Components of client server application
Components of client server applicationComponents of client server application
Components of client server application
 
Mobile web development
Mobile web developmentMobile web development
Mobile web development
 
Data Transfer between Activities & Databases
Data Transfer between Activities & DatabasesData Transfer between Activities & Databases
Data Transfer between Activities & Databases
 
Android Jumpstart Jfokus
Android Jumpstart JfokusAndroid Jumpstart Jfokus
Android Jumpstart Jfokus
 
Android application development
Android application developmentAndroid application development
Android application development
 
Android Application Components
Android Application ComponentsAndroid Application Components
Android Application Components
 
Android101
Android101Android101
Android101
 
Andriod Lecture 8 A.pptx
Andriod Lecture 8 A.pptxAndriod Lecture 8 A.pptx
Andriod Lecture 8 A.pptx
 
Android service, aidl - day 1
Android service, aidl - day 1Android service, aidl - day 1
Android service, aidl - day 1
 

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.
  • 12.
  • 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, } }
  • 17.
  • 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.
  • 28.
  • 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