Wednesday 27 February 2013

What is Sticky intent?

A normal broadcast Intent is not available anymore after is was send and processed by the system. If you use the sendStickyBroadcast(Intent) method, the Intent is sticky, meaning the Intent you are sending stays around after the broadcast is complete.

You can can retrieve that data through the return value of registerReceiver(BroadcastReceiver, IntentFilter) . This works also for a null BroadcastReceiver.

In all other ways, this behaves the same as sendBroadcast(Intent).

The Android system uses sticky broadcast for certain system information. For example the battery status is send as sticky Intent and can get received at any time. The following example demonstrates that.


// Register for the battery changed event
IntentFilter filter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);

/ Intent is sticky so using null as receiver works fine
// return value contains the status
Intent batteryStatus = this.registerReceiver(null, filter);

// Are we charging / charged?
int status = batteryStatus.getIntExtra(BatteryManager.EXTRA_STATUS, -1);
boolean isCharging = status == BatteryManager.BATTERY_STATUS_CHARGING
  || status == BatteryManager.BATTERY_STATUS_FULL;

boolean isFull = status == BatteryManager.BATTERY_STATUS_FULL;

// How are we charging?
int chargePlug = batteryStatus.getIntExtra(BatteryManager.EXTRA_PLUGGED, -1);
boolean usbCharge = chargePlug == BatteryManager.BATTERY_PLUGGED_USB;
boolean acCharge = chargePlug == BatteryManager.BATTERY_PLUGGED_AC; 

Sticky Broadcast Intents typically require special permissions. 

AIDL - Android Interface Definition Language

 
AIDL – Android Interface Definition Language is an IDL language used to generate code that enables two process on an Android-powered device to talk using inter process communication(IPC). If you have code in one process that needs to call on an object on another process, you would use AIDL to generate code to marshall the parameters.

Implementing IPC using AIDL

To implement an IPC service using AIDL

  1. Create your .aidl file – This file defines an interface that defines the methods and fields available to a client.
  2. Add the .aidl file to your make file. The ADT plugin for eclipse manages this for you. Android includes the compiler called AIDL , in the tools/directory.
  3. Implement your interface methods – The AIDL compiler creates an interface in the java programming language from your AIDL interface. This interface has an inner abstract class named stub that inherits the interface . You must create a class that extends your interface stub and implements the methods you declared in your .aidl file.
  4. Expose your interface to clients – If you're writing a service , you should extend service and override service onBind(intent) to retain an instance of your class that implements your interface.

What is a Service in Android with Demo

 
Service : A Service is a application component that can perform long running operations in background and does not provide a user interface.A service will continue to run in background even if the user switches to another application.

A service can be of two forms:-

  1. Started – A service is started when an application component start it by calling startservice().Once started, a service can run un background indefinitely , even if the component it is destroyed.
  2. Bound – A service is bound when an application component binds to ot by calling bindServices(). A bound service offers a client server interface that allow components to interact with the service, send requests , get results and even do so across process with Inter process Communication(IPC). A bound service runs as long as another application component is bound to it.

Feature of a Service
  1. A facility for the application to tell the system about something it want to be doing in the background.This corresponds to call to Context.startService(), which ask the system to schedule work for the service, to be run until the service or someone else explicitly stop it.
  2. A facility for an application to expose some of the functionality to other application . This corresponds to calls to context.bindService(), which allows a long standing connectivity to be made to the service in order to interact with it.


    To create a application to run in the background of other current activities, one needs to create a Service. The Service can run indefinitely (unbounded) or can run at the lifespan of the calling activity(bounded).

    Please note that a Service has a different lifecycle than activities therefore have different methods. But to begin a service in the application a call to startService() which envokes the service onCreate() method and onStart() beginning running the service. - See more at: http://marakana.com/forums/android/examples/60.html#sthash.1gmztZyv.dpuf
     
    To create a application to run in the background of other current activities, one needs to create a Service. The Service can run indefinitely (unbounded) or can run at the lifespan of the calling activity(bounded).

    Please note that a Service has a different lifecycle than activities therefore have different methods. But to begin a service in the application a call to startService() which envokes the service onCreate() method and onStart() beginning running the service.


    Download the source code here

    Please note that a Service has a different lifecycle than activities therefore have different methods. But to begin a service in the application a call to startService() which envokes the service onCreate() method and onStart() beginning running the service. - See more at: http://marakana.com/forums/android/examples/60.html#sthash.1gmztZyv.dpuf

    To create a application to run in the background of other current activities, one needs to create a Service. The Service can run indefinitely (unbounded) or can run at the lifespan of the calling activity(bounded).

    Please note that a Service has a different lifecycle than activities therefore have different methods. But to begin a service in the application a call to startService() which envokes the service onCreate() method and onStart() beginning running the service. - See more at: http://marakana.com/forums/android/examples/60.html#sthash.1gmztZyv.dpuf

    To create a application to run in the background of other current activities, one needs to create a Service. The Service can run indefinitely (unbounded) or can run at the lifespan of the calling activity(bounded).

    Please note that a Service has a different lifecycle than activities therefore have different methods. But to begin a service in the application a call to startService() which envokes the service onCreate() method and onStart() beginning running the service. - See more at: http://marakana.com/forums/android/examples/60.html#sthash.1gmztZyv.dpuf