Use local service
I. define java interface
public interface CalculateI { int addab(int a, int b); }
Create a Binder subclass, inherit Binder and implement interface methods
public class CalculateBinder extends Binder implements CalculateI{ @Override public int addab(int a, int b) { return a+b; } }
III. Onbind in the Service returns the new Binder subclass
@Override public IBinder onBind(Intent intent) { return new CalculateBinder(); }
IV. binding Service in Activity
Intent serviceintent = new Intent(SERVISE_INTENT); serviceintent.setPackage(getPackageName()); bindService(serviceintent, serviceConnection, BIND_AUTO_CREATE);
- Where Intent requires setPackage
- ServiceConnection needs to directly convert the service object to the IBinder subclass when connecting
private ServiceConnection serviceConnection = new ServiceConnection() { @Override public void onServiceConnected(ComponentName name, IBinder service) { calculateBinder = (CalculateBinder) service; }
V. using IBinder object to realize functions
calculateBinder.addab(8, 4);
Vi. Android manifest.xml configuration
<service android:name=".CalculateServie" android:enabled="true" android:exported="true"> <intent-filter> <action android:name="xxxxx.action.open.calculateservice" /> </intent-filter> </service>
Use of remote services
I. define the Aidl interface. The suffix of the file name is Aidl
interface ICalculateInter { int addab(int a, int b); }
2. Create a subclass of IBindler, which inherits from ICalculateInter.Stub
public class CalculateBinder extends ICalculateInter.Stub{ @Override public int addab(int a, int b) { return a+b; } }
III. Onbind in the Service still returns the new Binder subclass
@Override public IBinder onBind(Intent intent) { return new CalculateBinder(); }
IV. binding Service in Activity
//At this time, onServiceConnected will be different
private ServiceConnection serviceConnection = new ServiceConnection() { @Override public void onServiceConnected(ComponentName name, IBinder service) { iCalculateInter = ICalculateInter.Stub.asInterface(service); }
V. to use IBinder object to implement functions, you need to catch RemoteException
try { iCalculateInter.addab(8, 4); } catch (RemoteException e) { e.printStackTrace(); }
Vi. Android manifest.xml configuration. Note that the Service is running in a separate process
<service android:process="com.example.xxxxxxx" android:name=".CalculateServie" android:enabled="true" android:exported="true"> <intent-filter> <action android:name="xxxxx.action.open.calculateservice" /> </intent-filter> </service>
What is ICalculateInter.Stub?
After creating the aidl interface file, the compiler will automatically generate a class, which is ICalculateInter.java
public interface ICalculateInter extends android.os.IInterface { public static abstract class Stub extends android.os.Binder implements com.example.xxxx.ICalculateInter{ //Ellipsis content public static ICalculateInter asInterface(android.os.IBinder obj) { if ((obj==null)) { return null; } } public int addab(int a, int b) throws android.os.RemoteException; }
ICalculateInter has an internal abstract class Stub, which inherits Binder and implements the interface in ICalculateInter.aidl,
This is the same way we used to bind local services.