Wednesday, June 26, 2013

Android App Development 5


Reference: Professional Android 2 Application development by Reto meier


1)AIDL
2)IPC,Process,Thread
3)Bluetooth
4)Fragments
5)NDK

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:
========================
AIDL:

  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)
    onServiceDisconnected()
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




Bluetooth:
=========
Range 10 meters.it 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:   
===============
1)BluetoothAdapter
    ->represents local bluetooth adapter/bluetooth radio
2)BluetoothDevice
3)BluetoothSocket
4)BluetoothServerSocket
5)Bluetooth class

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


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



Sensors:
=============
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
    barometers
    thermameters
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




Fragments:
============
  --> Actitvity is a container for views.
Fragments
    - 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.
onCreate()
{
 setcontentview(r.id.abc);
}


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.


Backstack:...
 

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


NDK:
=========
    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
    res
    jni-> .c/.cpp files & Android.mk-> output: library file copied under "lib" folder.
        jni/Application.mk ->optional
   

cd NDKProject
NDK-build clean
NDK-build
  =>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.

static
{
  System.loadLibrary("fileloader"); //for loading fileloader.so
}

JNI function name:


Java_<packageName>_<ClassName>_loadFile()


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


Android.mk:
===============
LOCAL_PATH->Current project source directory
CLEAR_VARS->Earlier variables are cleared
LOCAL_MODULE :=fileloader #creates libfileloader.so
LOCAL_LDLIBS=>used to specify linking libraries and system libraries
LOCAL_LDLIBS := -llog

BUILD_SHARED_LIBRARY
BUILD_STATIC_LIBRARY



 samsung android reference:

http://developer.samsung.com/android/samples









No comments: