Data transfer in Intent

1.An intent can contain additional data for target component.
2.Bundle class is used for achieving data transfer between android activities
3.For adding data to Bundle   use putExtra()
4.It will be always in key-value pair format
5.key will be always type of String
6.value can be primitive data type,Bundle,Parceable and Serializable.

How to pass data in Intent?

Intent intent = new Intent(MainActivity.this,SubActivity.class);
intent.putExtra(“KEY”,”Hello”);
startActivity(intent);

How to Get data from Intent?

Bundle bundle = getIntent().getExtras();
String got = bundle.getString(“KEY”);
* write inside onCreate();

Find Full code in this link

https://github.com/kavidriod/DataTranferViaIntent

Pending Intent

What is Pending Intent?

Pending intent is an object that wraps another intent object with the target action to be performed.

It can be used to launch an activity,service,Broadcast receiver.

It is passed to other application to perform desired actions.

Types of Pending Intent?

getActivity(Context,int,Intent ,int);
getBroadcast(Context,int,Intent ,int);
getService(Context,int,Intent ,int);

Arguments of Pending Intent?

this(Context) – the context in which the PendingIntent starts the activity.

requestCode – a private request code for the sender.

intent – An explicit intent of the activity to be launched.

PendingIntent.FLAG_CANCEL_CURRENT – If described PendingIntent already exists,then cancel first once,and n new pending Intent is generated.

Broadcast Receiver

What is broadcast receiver?

It is an Application component.
Allows you to register for system or application events.
All registered receiver for an events are notified by android runtime once this event happens.
The broadcast can be generated either by system or by other application.
App can listen important System events such as Low battery,new Messages,System Reboot.

How to register broadcast receiver?

1.Statically – Declare in manifest file.
1.1.Broadcast receiver is declared in manifest.xml file like other components of android
1.2.Can’t be Unregistered.

2.Dynamically – In Java code
2.1.Broadcast receiver is registered using register() method.
2.2.Can Unregistered later.

Processes and Threads

Processes ?

Android operating system is built on Linux kernel,So Linux kernel process concepts are valid for android too.

When an app component start,and the application does not have any other components running,the android system initiate a new linux process for the application with a single thread of execution.This thread is called “Main Thread” .

To identify which processes should be removed or retained,The system places each processes into an “Importance Hierarchy”.The processed with the lowest importance are removed first.

Types ?

Active Processes,
Visible Processes,
Service Processes,
Background Processes,
Empty Processes,

Threads?

An android application automatically creates  a “main” thread when it starts.This is also called the UI thread.

UI thread is used for user interaction.All android applications runs on the UI main application thread.

Running all the components on a single thread will result in poor response time.

If UI thread is blocked for morethan 5 secs ,“Application Not Responding” dialog box will reveal.

Reason for ANR?

1.Doing Network operation.
2.Performing Long Calculation.
3.Accessing Database Operation on the UI thread.

Using Background Thread?

Android UI is not “Thread-Safe”.
Should not access UI operations on outside main thread or any other Thread.

Single Thread Model

1.Don’t block the UI thread – Long running operations should not perform on UI thread,It should be done in Worker thread.
2.Don’t access UI from outside main thread.Use “Message Passing” methods.

Message Passing?

To establish communication between main and worker thread,Android using message passing approach.

1.Handler : Handler object registers itself with the thread in which it is created.It provides a channel to send data to the thread.

2.Looper: It is a class which turns thread to pipeline thread.It implements the “loop” – It takes assigned task,execute it,then takes the next one,So on.
Use Looper class if you want to pass multiple messages.3.PNG

Android Service & Background Thread ?

For Long running operation you can use either background thread or Async Task

Background Thread:

1.Worker Thread,
2.Both UI and background thread runs Simultaneously.
3.Used for Performing Long running operations.

Android Service:

1.An application component.
2.Performs Long running operations in the background.
3.No user interface

Services

What is Service?

A Service is an application component,that perform some operations in the backgroud
It does not have user interface
An activity starts a service
Service can be started by component which belong to other application.

Service can perform

1.Downloading & Uploading file over network interface.
2.Play music in background.
3.File operations.

Types of Service?

1.Started .

Component calls startService(),Once start service can run in background even if application component which started it moves to background or destroyed.

LifeCycle:

5.PNG

2.Bounded.

It provides client-server interface.
It runs as long as other application component bound to it.
Multiple application component can be bind to a single service.
Service is destroyed when all application component unBind.

LifeCycle:

6.PNG

How to create Service?

1.Must be declared in Android manifest.xml file
2. added as a child in tag
3.Attributes?
3.1.Enabled
3.2.Exported
3.3.Name
3.4.Permission
4.Defined service class should be the sub Class of Service
5.Override default methods in defined service class.

Intent Service?

Android Service that uses a single background thread to perform an operation
To start an operation request is sent through startService(),and It is handled through onHandleIntent().

Limitation of Intent Service?

1.Cannot directly send result to UI. It has to first communicate with activity.
2.Operation once started cannot interrupt in mid way.
3.Cannot run multiple operations Asynchronously.

Creating Intent Service?

Creating by extending IntentService
It overrides onHandleIntent().it will execute each time a new request comes in.
on Request completion,the response is sent back to the activity via LocalBroadcastManager method.