Wednesday, June 26, 2013

Android App Development 5

Reference: Professional Android 2 Application development by Reto meier


You need to use AIDL if you want a class outside of your application's process to access the Service.
If you're only using the service from inside your application, you can use a local service.

Remote procedure calls:

  In RPC, only methods are exposed.we cannot expose variabls.
All RPC methods are synchronous. Calling process gets blocked
until the remote method finishes execution.
AIDL generated Javainterface is shared between server and client.
Server implements java interface. client invokeds java interface methods.

uses proxy class[stub] to pass values between server and client.

Server Side steps:
1)create the .aidl file & declare RPC interfaces
2)AIDL ->generates the java interfaces stub [.java file] from .aidl
3)use stub class to implement RPC methods
    ->Stub class is derived from IBinder interface
4)Expose RPC methods via Service
    -> service will be started if it is a background task
    -> Here we will bind to the service if it is a interface

    in, out, inout parameters

Few rules are implementing your interface:
1)client has to call the RPC methods in a separate thread
if particular RPC method is taking time or complex calculation   
2)No exceptions from RPC methods to caller. It wont be passed to
client, handle the exception locally dont throw it to caller
3)only methods can be exposed, data cannot be exposed in RPC
4)To make the service available to other or make it as public service,
 we have to add action in intent filter. the client will make use of this action
in Intent & start the service.

Client side steps:
0)Stub class is shared across clients
1)Declare the interface variable
2)Implement ServiceConnection interface
    onServiceConnected(componentName name, IBinder binder)
3)bindService (intent,serviceConnectionObject, flag);//while binding, if the service is not running,
  it will create the service and bind to it
    bindService will call bind() method of the service.
Once service connected, onServiceConnected() called. We will typecast
IBinder object to our interface variable.
4) call the methods on your interface
5) To disconnec call Context.unbindService() with the instance of the service connection.
    ->This will triggers onServiceDisconnected() method will be triggered

Range 10 will makes use of MAC address.

1)setting up bluetooth
2)find available devices or paired devices
3)connect with the device
4)Transfer data

classes to be used:   
    ->represents local bluetooth adapter/bluetooth radio
5)Bluetooth class

android.permission.BLUETOOTH_ADMIN-> modify settings/discovering devices

1)Bluetooth application to take attendance of students in classroom automatically.

1) measure motion,orientation and various environmental conditions [pressure,temparature,light]

sensor types:
1)Motion sensors
    acclerometers-> how fast the phone is moving,monitoring device movement
    gravity sensors->
    rotational vector sensors->
2)Environmental sensors
3)Position sensors

sensor framework android:
1) Determine available sensors on a device
2) Determine the sensors capabilities power,range...
3) acquire raw sensor data frequency of the data you want[ for every minute...]
4) Register and unregister sensor event listeners

location,Bluetooth & sensors

Based on accelerometer, detect whether the phone has fallen or the old person has fallen down...
and send the location details to provided contact number as SMS, trigger the call to predefined number and
enable the phone

GPS phones to track the travel buses and its locations from Travels company

  --> Actitvity is a container for views.
    - address large screen devices like tablet
    - introduced Android 3.0 HoneyComb
    - one or more fragments can be combined to an activity
    ->mini activities with its own set of view
        ->can reuse fragments in multiple activities
        -> multipane UI

Localization of UI resources are important.

res/layout/abc.xml-> two  fragments for normal phone
res/layout-port/abc.xml- can have many fragments
Android will decide dynamically at runtime... if it is a portrait mode, it will load
the abc.xml from res/layout-port/ path. User doesnt need to add code for it.

Lifecycle of a fragment:
   depends upon the activity. if the activity is paused, all the fragments are paused
if the activity is running,all the fragments are running.
if the activity is stopped,all the fragments are stopped.


Fragment inside Activity:
    1)Fragment can be in any ViewGroup
DialogFragment, ListFragment, PreferenceFragment, WebViewFragment

Fragment's onCreateView()->Load the UI layout or create it dynamically.

android's Inflate() method will read xml and create UI resources.

Dynamically adding fragments to an Activity:
  1)via FragmentTransaction

Emulator ->press ctrl+F11 to move to portrait mode in emulator

Try this

    res/layout/abc.xml-> UI for  landscape mode
    res/layout-port/abc.xml- UI for portrait mode

FragmentManager,FragmentTransaction classes are important;

Localization qualitifier for tablet:
1) screen width
2) screen size

    1)NDK development in practice
    c/c++ ->business logic
    java  ->

    1)porting the existing C/C++ source to Android only activity we have to develop it in java.
    2)Execution is faster since it is converted to assembly language
       Skips Dalvik VM, byte code to assembly language code.

C/C++ functions are invoked from Java via JNI calls.

java -> output ->.dex
c/cpp=> output ->library
Final apk => .dex + library done by NDK tool

For Windows, cygwin 1.7 or higher is required to build NDK [binutils,gcc-g++,make,splint]

NDK Project:
    src-> java files
    jni-> .c/.cpp files &> output: library file copied under "lib" folder.
        jni/ ->optional

cd NDKProject
NDK-build clean
  =>This will build the C/C++ source code, copy it to libs folder
  =>After this open the project in eclipse, and compile it to create the apk file   

static block is executed before loading the activity or creating an object.

  System.loadLibrary("fileloader"); //for loading

JNI function name:


className-> where we declared the loadFile() as native
PackageName-> where the class belongs to

JNIEnv-> from Java environment
     Conversion of java string to C/C++ string or C/C++ string to java string done by Java environment
LOCAL_PATH->Current project source directory
CLEAR_VARS->Earlier variables are cleared
LOCAL_MODULE :=fileloader #creates
LOCAL_LDLIBS=>used to specify linking libraries and system libraries


 samsung android reference:

No comments: