You are on page 1of 8

Handler Class :

Android supports standard java threads.


you can use standard Threads and the tools from the package
"java.util.concurrent" to put
actions into background.
Onl limitation is that you cant update UI directly from background process.
To do that you need to use some android specific classes
eg. class android.os.Handler or class AsyncTasks.

Thread class :
Threads help in multi tasking
Threads can be taught as mini process running under a man process.
Threads enable at least appearance parallel execution.
Application of thread :
When android app is first started, runtime system will create a single main
thread, this main thread will take care of execution of all the components in
the android.

Android UI tolkit is not thread safe !


There are many components in the android main thread, if one takes a long
time to execute, then it becomes unresponsive.
Subthreads cannot directl manipulate the app.
Handler acts as interface and collects the messees from subthreads. and
updates the main thread one by one as the messaes comes along the
way.Thread handlers are implemented in main thread.

Handler Class :
For the purpose of multithreading, we shall use the handler class which
comes from the package android.os.Handler

Fro fig, we can see that each threas is handled by one one instance of a
Handler class.
Threads communicate with each other with the help of messeges.
This handler class helps to maintain snc-co ord b/w threads by allowing them
to run togather achieveing multithreading.

Handler Class :

Header class
Instance One ----------------------> Thread1--------Threads communicate with messeges.
Instance Two -------------------------> Thread 2-------

There are two main uses of Handler :


TO schedule messeges and runnables to be executed at some point in future.
to enqueue th action to be performed on different than our own.

Intents
Android Intent is a messege that is passed between components such as
activities, content providers, broadcast receivers etc.
Intents are objects of android.content.intent type. your code can send them
to system defining the target components.

startActivity() To launch new activity

atartService() to initiate a new service or deliver a new instruction.


sendBroadcast() to deliver the messege to all the interested broadcast receivers.

Different Types of Intent


1. Explicit Intents :
Exp. intetnts xplicitely define the component which should be called by the
android system, by using java class as an identifier.
2. Implicit Intents
specify the action which should be performed and optionaly data which
provides content for the action.

Intent Filters
Intents are used to signal the android system that some event has occured.
Intents often describe the action which should be performed and provide data
upon which such an action should be done.
A component can register itself via intent filter for specific action & data.
intent filter specifies the types of intents to which an activity, service,
broadcast receiver can respond by declaring the capabilities of a component.
android contents register intent filters either statically in the
AndroidManifest.xml or in case of a broadcast receiver also dynamically via
code. an intent filter is defined by its category, action and data filters. It can
also contail additional metadata.

A filter has fields that parallel the action data and ctegory fields of an intent
object. and implicit filter is tested against all the three areas.
Action :
A string that specifies the generic action to perform(such as view or pick). You
can specify your own actions for use by using intents within your app, but you
should usually use action constants defined by the intent class or other
framework classes. Here are some common actions for starting an activity.

Data : The URI (a uri object) that references the data to be acted upon and /or
the MIME(Multipurpose Internet Mail Extensions) type of data.

Category :
A string containing additional information about the kind of component that
should handle the intent. Android treats all the implicit intents passed to
startActivity() as if they contained at least one category
"android.intent.category.DEFAULT"
Extras :
Key-Value pairs that carry additional information required to ccomplish the
requested action. Just as some actions use perticular kinds of data like URLs,
& extras.
you can add extra data with various putExtra() methods, each accepting two
parameters : the key & the vcalue.
Yo can also create bundle object & insert the bundle in the intent with
putExtras();

Flags :
Flags defined in the intetnt class that function as metadata for the intent. The
flags may instruct the Android system how to launch an activity and how to
treat it after its launched.

Data transfer between Activities


Optionally an intent can also contain adidtional dat bases on an instance of a
bundle class.wich csn be retrived from the intent cia the getExtras() method.
You can also add data directly to the bundle via the overloaded putExtra()
methods of the intent obects. Extras are key/value pairs. The key is always of
type string. As value you can use the primitive data types string, bundle,
Parceable and serializable.
the receiving component can access this information via the getAction() and
getData() methods on the Intent object

Retriving Result data from a sub-activity

Project 1
there shouldnt be any UI files in the newly made package All
UI files must be in default package.
Singleton Onject : Only one object will be created
throughout the project.
It will establish a connecton between array and data as the
database does!
We need only one object! whatoever.

*Constructor is made private here. So it cant be used


at any other places outside the class. That is the
whole singleton concept.Only one object will be used
throughout project
Generate geter setters :
Write click ->> source ->> generate geter setters
& generate constructor using field. same method.
geter seter is the property to access the private data
members of the class.

JSON(Javascript Object Notation)


JSON is built on two structures
A collection of name/vallue pairs. in various languages, this
is realized as an object, record, struct, dictionary, hash table,
keyed list, or associative array.
An ordered list of values. It is realizred as an array vector, list
or seq.
An Object :
Anobject is an unordered set of name/value pairs. An object
begins with
{(left brace) and ends with}(right brace). Each name is
followed by : colon and the name/value pairs are separated
by , (comma).
syntax : { string : value1,value2}
An array
An array is an ordered collection of values.
An array is written in between {...} & values are separated
by -- , -- comma.
eg syntax : { value1, value2,value3,....valueN}
arrays can contain anything like string num boolean arary
object etc.

AsyncTask

public abstract class AsyncTask


mainThread handles the modification inUI and handles IP events from
one single user interface thread.
Programmer does not use any concurrency constructs, all code of an
android application runs in this thread.
in long lasting operations like downloading data from internet, UI
blocks untill the code is finished with downloading.
To provide good user exp, all slow runing process shoul run
asynchronously.(you shouldnt have to wait for downloading task to
end, ou can move on to other task.)

android enforces that with app not responding if the activity does not
interacts for 5 seconds. from this dialog, you can choose to stop app.

What does asyncTask do :

Android modifies the user interface via one thread, the so called UI
thread. If you perform a long running operation directly on UI thread,
i.e. UI will freeze untill the task is finished.

AsyncTask enables proper and easy use of the UI thread. Thiss class
allowes to perform background operations and publish results on the UI
thread without having having to manipulate threads and/or handlers.
Go to android developers/AsyncTask for more info.

Create a Class :
public class JsonAsync extends AsyncTask<params, progress,Result>
{
}
1 params(i.e. in parameters)
2 progress i.e progress

3. Results i.e. return


JsonAsync ja = new JsonAsync();
ja.execute();
execute() executes the task with the specified parameters.

Three types used by an asynchronous task


params the type of the parameters sent to the task upon execution.
progress, the type of the progress units published during the
background computation.
Result : type of result of the background computation.

Methods :
onPreExecute() , invoke on UI thread before the task is executed.
doInBackground(params) invoked on background thread immeidately
after onPreExecute finishes. used to perform background computation.
onProcessUpdate(Progress) : invoked on UI thread after a call to
publishProgress(progress). timing of execution is undefined.Used to
display any kind of progress in UI while BackgroundComputation is still
executing.

onPostExecuted(Result) :
invoked on the UI thread after the background computation finishes.
Result of background computation is passed to this step as parameter.

You might also like