Aidl Incoming callback method
Assuming that an existing service is receiving data, the data received will be passed to the activity of another application, and the remote service interface can be obtained by using the binder interface of the aidl that invokes the service in the activity. The callback object is then set by the method that the interface provides to receive the callback, so that after the remote service receives the data, the callback object provided by the activity passes the data to the activity (the reason for this is that Aidl is shared memory and the data sharing speed is real-time).
Setting the callback object's Aidl
Package com.test.aidl;
Import Com.test.aidl.InterProcessCallback;
Interface Iaidlsetcallback
{
void Setcallback (Interprocesscallback callback);
}
Callback interface Aidl, because Interprocesscallback is used in both activity and service, it must be defined Aidl
Package com.test.aidl;
Interface Interprocesscallback
{
void recv (int val);
}
Service
public class Aidlservice extends Service {
Callback Object instance
Private Interprocesscallback callback;
Analog Service receive data
private random random;
@Override
public void OnCreate () {
Super.oncreate ();
Random = new Random (System.currenttimemillis ());
New Timer (). Schedule (new TimerTask () {
@Override
public void Run () {
If the activity has passed in the callback object, get the data to the activity.
if (callback! = NULL)
{
try {
Callback.testcallback (Random.nextint ());
} catch (RemoteException e) {
E.printstacktrace ();
}
}
}
},0,10);
}
Private Iaidlsetcallback.stub binder = new Iaidlsetcallback.stub ()
{
@Override
public void Setcallback (Interprocesscallback callback)
Throws RemoteException {
AIDLService.this.callback = callback;
}
};
@Override
Public IBinder Onbind (Intent Intent) {
return binder;
}
}
Activity
public class Mainactivity extends Activity {
Private TextView Tvrecvvalue;
Private Iaidlsetcallback Aidlservice;
Private Interprocesscallback Interprocesscallback = new Interprocesscallback.stub () {
@Override
public void recv (int val) throws RemoteException {
Final int nval = val;
MainActivity.this.runOnUiThread (New Runnable () {
@Override
public void Run () {
Tvrecvvalue.settext (nval+ "");
}
});
}
};
Private Serviceconnection conn = new Serviceconnection () {
@Override
public void onserviceconnected (componentname name, IBinder service)
{
Aidlservice = AidlGetServiceData.Stub.asInterface (service);
try {
Set Callback Object
Aidlservice.setcallback (Interprocesscallback);
} catch (RemoteException e) {
E.printstacktrace ();
}
}
@Override
public void onservicedisconnected (componentname name)
{
Aidlservice = null;
}
};
@Override
protected void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Setcontentview (R.layout.activity_main);
Tvrecvvalue = (TextView) Findviewbyid (R.id.tvrecvvalue);
Intent Intent = new Intent ("Com.example.aidl.action.AIDL_TEST");
Intent.setpackage ("Com.example.aidl_service");
Bindservice (Intent, Conn, service.bind_auto_create);
}
@Override
protected void OnDestroy () {
Super.ondestroy ();
This.unbindservice (conn);
}
}
By doing this, you can receive data in the service and then callback the data to the activity through the callback object Interprocesscallback, thus accomplishing the data sharing of the remote service and activity.
This article is from the "whatever957" blog, make sure to keep this source http://whatever957.blog.51cto.com/6835003/1827431
Aidl sharing data through callbacks