Android AIDL between two applications
I've recently read an article regarding Android AIDL here and as you see, this example is made on one single project with multiple modules.
I won't present you the basics of the Android AIDL, because it's written very clear here and on the Android official site. So the main ideea of this article is to show you how to make an Android AIDL between two applications.
Step 1. Create a new application called "myservice". Create an .aidl file and copy and paste the following code
Step 2. After you build the project, create a service like this
Step 4. Create a new application called "myapp". Create a new package with the same name as the service: i. e. com.ccc.myservice. After this create an .aidl file and copy the following code
Step 5. Finally create a button in your layout file and in your MainActivity.java file copy the bellow code
Step 6. If it gives you an error, add
Note: You don't need AIDL for simple applications. You can just wrap your service into a jar or aar and import it to your client application. You can click here to see when you should use AIDL and when you should just work with jar or aar files.
I won't present you the basics of the Android AIDL, because it's written very clear here and on the Android official site. So the main ideea of this article is to show you how to make an Android AIDL between two applications.
Step 1. Create a new application called "myservice". Create an .aidl file and copy and paste the following code
package com.ccc.myservice; interface IMyAidlInterface { String hello(); }
Step 2. After you build the project, create a service like this
package com.ccc.myservice; import android.app.Service; import android.content.Intent; import android.os.IBinder; import android.os.RemoteException;
public class RemoteService extends Service { public RemoteService() { } @Override
public IBinder onBind(Intent intent) { // Return the interface return mBinder; } private final IMyAidlInterface.Stub mBinder = new IMyAidlInterface.Stub() { @Override
public String hello() throws RemoteException { return "hello!!!"; } }; }
Step 3. Hit the Run button. This will help you to avoid connection errors, like this.
Step 4. Create a new application called "myapp". Create a new package with the same name as the service: i. e. com.ccc.myservice. After this create an .aidl file and copy the following code
package com.ccc.myservice; interface IMyAidlInterface { String hello(); }
Step 5. Finally create a button in your layout file and in your MainActivity.java file copy the bellow code
package com.ccc.myapp; import android.content.ComponentName; import android.content.Context; import android.content.Intent; import android.content.ServiceConnection; import android.os.IBinder; import android.os.RemoteException; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.Toast; import com.ccc.myservice.IMyAidlInterface; public class MainActivity extends AppCompatActivity { private RemoteServiceConnection serviceConnection; private IMyAidlInterface service;
@Override
protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); connectService(); } private void connectService() { serviceConnection = new RemoteServiceConnection(); Intent i = new Intent("com.ccc.myservice.RemoteService"); i.setPackage("com.ccc.myservice"); boolean ret = bindService(i, serviceConnection, Context.BIND_AUTO_CREATE); System.out.println("ret = " + ret); } public void clickHello(View view) throws RemoteException { System.out.println(service.hello()); } class RemoteServiceConnection implements ServiceConnection { public void onServiceConnected(ComponentName name, IBinder boundService) { service = IMyAidlInterface.Stub.asInterface((IBinder) boundService); Toast.makeText(MainActivity.this, "Service connected", Toast.LENGTH_LONG) .show(); } public void onServiceDisconnected(ComponentName name) { service = null; Toast.makeText(MainActivity.this, "Service disconnected", Toast.LENGTH_LONG) .show(); } } }
Step 6. If it gives you an error, add
<service
android:name=".RemoteService"
android:enabled="true"
android:exported="true"> <intent-filter> <action android:name="com.ccc.myservice.RemoteService" /> </intent-filter> </service>in the manifest file of the service app.
Note: You don't need AIDL for simple applications. You can just wrap your service into a jar or aar and import it to your client application. You can click here to see when you should use AIDL and when you should just work with jar or aar files.
Comentarii
Trimiteți un comentariu