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,
}
}
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.
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.
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.