* The server and client of this demo are written in reverse.
New Project Client,server new Aidl file on server side, content:
Imyaidlinterface.aidl
Package com.example.server;
Declare any non-default types here with import statements
Interface Imyaidlinterface {
String getString ();
}
Make project automatically generates Java classes in Server\build\generated\source\aidl\debug\com\example\server
Create the same file in client and generate Java class
* Note that the two package names should be consistent
Writing service services in server
PackageCom.example.client;
ImportAndroid.app.Service;
ImportAndroid.content.Intent;
ImportAndroid.os.IBinder;
ImportAndroid.os.RemoteException;
Importandroid.support.annotation.Nullable;
ImportAndroid.util.Log;
ImportCom.example.server.IMyAidlInterface;
/**
* Created by LUOZHENLONGHP on 2017/5/13.
*/
Public classMyServiceextendsService {
Public static FinalStringTAG="MyService";
@Nullable
@Override
PublicIBinder Onbind (Intent Intent) {
returnresult;
}
Imyaidlinterface.stubresult=NewImyaidlinterface.stub () {
@Override
PublicString getString ()throwsremoteexception {
return"You get It";
}
};
@Override
Public voidOnCreate () {
Log.I(TAG,"OnCreate is called");
Super. OnCreate ();
}
@Override
public intOnstartcommand (Intent Intent,intFlagsintStartid) {
Log.I(TAG,"Onstartcommand is called");
return Super. Onstartcommand (Intent, flags, Startid);
}
}
Registering in the Menifest file
<android: Name=
// This indicates the process name
Android :p rocess= "Com.test.myservice">
<intent-filter>
<android: name="Android.intent.action.RESPOND_VIA_MESSAGE"> </Action>
</Intent-filter>
</Service>
Write code binding service on client side
Bindmyservice ()
{
Intent ();
Here's the action.
Intent.setaction ("Android.intent.action.RESPOND_VIA_MESSAGE");
This is the service side of the package name, this time the demo write the reverse
Intent.setpackage ("Com.example.client");
Serviceconnection connection =NewServiceconnection () {
@Override
Public voidonserviceconnected (componentname name, IBinder service) {
Get an example here
Imyaidlinterface= Imyaidlinterface.stub.Asinterface(service);
Try{
String result =Imyaidlinterface. getString ();
Toast.Maketext(Mainactivity. This, result, Toast.Length_short). Show ();
}Catch(RemoteException e) {
E.printstacktrace ();
}
}
@Override
Public voidonservicedisconnected (componentname name) {
}
};
Bindservice (Intent,connection,bind_auto_create);
}
Done
Android Aidl Practice Pass Simple string