IPC Activity-Srvice communications:
ServiceConnection
Override onServiceConnected()
Create a new Messenger using IBinder returned from Service
Override onServiceDisconnected
Call to bindService()
Send the Message to Service : Using messenger Reference
Receive the Message from the Service: Handler which is set by
msg.replyTo.send(new Messenger(new MyResponseHandler))
[initt ConnectionService
int a messenger inside ConnectionService
pass ConnectionService to BindService
declare a Handler object (which is also a callback)
init in Messenger where u want to send]
On Service side:
Create a Handler and Override its handleMessage()
Create a Messenger and pass the Handler to it
Override onBind()
It returns the IBinder using Messenger
Receive Message : By Handler
Send Message : msg.replyTo.send(response)
[Init a messenger
return a binder (IBinder) from overridden On]
****************************
Fundemental:
Service and Activity communicates using Message object(data packets) throgh Ibinder channel interface
so you should have these objects , 1) Initialize a Messenger has Two constructors;Ibinder and Handler
In Service side you are initiantiate it by Handler and in Activity side you do by Ibinder receive from ConnectionService
Handler is a Class derived from Handler.
A Message object used as data packet , Message has a Method replyTo which return a Messenger (which is previously received from sender) and itself has send() methode u use this when reply,but when you intiate sending
you must first intiate Message packet by copy data trough Bundle then call send() method of Messenger
In both side data transfered is a Message Packet and Connection is managed by ConnectionService
Messenger has a method send this method take a Bundle object
===============
Notes: when using IPC ot RPC the channel of connection which is a ConnectionService could be broken in any time
in this case DeadObjectException will be thrown , this exception actually is the only exception thrown by remote methods! , so it will be better to trap this exception on your client side .
AIDL is a standard way of IPC but it's for an user case in which you have multiple process communicatiing with a remote service in a multithread environment.if your remote is not supposed to handle maltithread request use a IPC by Messages.
To implement an AIDL remote service
1)Create an Interface (IMyInterface.aidl) which will be exposed to outside world in"\src" folder
2)compile it, in "gen" folder or "build" a IMyInterface.java is generated ,this file include aidl and also Stub class
"public static abstract class Stub extends android.os.Binder "
3)Create a subclass of Stub (MySub) in previous section which implements all declared methods of your interfaces , this subclass maybe an inner class like:
class MyStub extends IKhosrovAidlInterface.Stub {
@Override
public void basicTypes(int anInt, long aLong, boolean aBoolean, float aFloat, double aDouble, String aString) throws RemoteException {
}
@Override
public int getAdd(int a,int b)
{
return (a+b);
}
}
4)you return an instance of this subclass(MySub) in onBind of your remote service
to complet.......
********************
IntentService:
This is a service that automatically create a new thread to run, an ordinary Service run in the main thread of your app,so it's abest choice if your service doesnt handle more simultinous request.it's acting like a Thread
onStartCommand not called in bindService
***************************
Intent intent = new Intent(android.content.Intent.ACTION_VIEW,
Uri.parse("http://maps.google.com/maps?saddr=20.344,34.34&daddr=20.5666,45.345"));
startActivity(intent);
===============
Remark1
post should be called in a worker Thread rather than the UI Thread,
no any handler associated by default in a Thread
so when calling post , it will be executed in the handler's owner UI Thread.
1)When using AsyncTask by subclassing for example MyAsyncTask ,AsyncTask should be as
AsyncTask<Type1,Type2,TypeResult> these types must be primitive type(Integer,Long,...
last argument is the type of Result
2)doInBackground's arguments is type1...atype ????? and it must return ResltType
When using JSON , never get data back from server in your main UI Thread,instead fetch data in a sepaarated thread or AsyncTask otherwise it goes to ANR or throw an exception
**********************************************************************************
In thread synchronization when you're using synchronized block on two function make sure both functions are static or non-static otherwise locking may not work because static method is locked on Object but non-static Object locks on current object(this)!!!!
***************************************************
IntentService:
Note1:
When register a Broadcast receiver (for example for an IntentService) if yo register it in code (by registerReceiver) dont declare it in manifest otherwise it may generate exception!
Note2:
When using IntentService to perform a task generally you send the result to a Broadcast receiver, dont update UI directly from your Broadcast receiver instead use sendmessage from your receiver to your UI Handler.