An application cannot directly access the data of another application for security reasons. If you want to access the data from another application then AIDL will help
you to access those data. I have created two examples one of Server which creates the AIDL service and second Client which access the server data.
In order to implement ADIL, we need to follow the following steps:-
For Server App
1. Make one folder named AIDL in which your AIDL file resides.
1 2 3 4 |
interface IRemote { int add(int a, int b); } |
2. Create the Service and Bind the IRemote Service as following
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
public class ArithmeticService extends Service{ @Override public IBinder onBind(Intent intent) { // TODO Auto-generated method stub return mBinder; } /** * IRemote defnition is available here */ private final IRemote.Stub mBinder = new IRemote.Stub() { @Override public int add(int a, int b) throws RemoteException { // TODO Auto-generated method stub return (a + b); } }; } |
3. Mention the service to android manifest file as following and also mention the unique action name
1 2 3 4 5 6 |
<service android:name=".ArithmeticService" android:process=":remote"> <intent-filter > <action android:name="Mention your action Name"/> </intent-filter> </service> |
Client Example :
1. For getting access server data and functionality, we have to create the same aidl package as you used in the server example and create the same(copy) file as you created in the server example IRemote.aidl
2. Then for triggering the functionality, we need to create a ServiceConnection instance to handle:
a.Service connection event
b.Service disconnection event
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
ServiceConnection serviceConnection = new ServiceConnection() { @Override public void onServiceConnected(ComponentName name, IBinder binder) { Log.d("Srv", "Service connected!"); service = IRemote.Stub.asInterface((IBinder) service); Log.d("Srv", "Service interface ["+service+"]"); } @Override public void onServiceDisconnected(ComponentName name) { Log.d("Srv", "Service disconnected!"); service = null; } if(service == null) { Intent it = new Intent(); it.setAction("com.remote.service.CALCULATOR"); //binding to remote service bindService(it, serviceConnection Service.BIND_AUTO_CREATE); } |
So for more information about AIDL file for passing more type of data and client-server app functionality, you can go through the for link:-
https://developer.android.com/guide/components/aidl#java