ICallBack.aidl
package com.opensource.aidl.service.callback;
interface ICallBack{
void asycMsg(String result);
}
package com.opensource.aidl.service.callback;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.os.RemoteException;
public class CallBackService extends Service
{
@Override
public IBinder onBind(Intent intent)
{
return binder;
}
private ICallBack.Stub binder = new ICallBack.Stub()
{
@Override
public void asycMsg(String result)
throws RemoteException
{
Intent intent = new Intent();
intent.putExtra("hello", "The answter is : " + result);
intent.setAction("com.opensource.aidl.receiver");
sendBroadcast(intent);
}
};
}
IntentFilter inf = new IntentFilter();
inf.addAction("com.opensource.aidl.receiver");
registerReceiver(br, inf);
Intent intent = new Intent("com.opensource.aidl.service.callback.ICallBack");
// unbindService(connection);
bindService(intent, connection, BIND_AUTO_CREATE);
BroadcastReceiver br = new BroadcastReceiver()
{
public void onReceive(android.content.Context context, Intent intent)
{
if (mTextView.getText() != null)
{
mTextView.setText(mTextView.getText().toString() + "\r\n" + intent.getStringExtra("hello"));
}
else
{
mTextView.setText(intent.getAction());
}
};
};
public ICallBack callBackService;
private ServiceConnection connection = new ServiceConnection()
{
public void onServiceDisconnected(ComponentName name)
{
callBackService = null;
}
public void onServiceConnected(ComponentName name, IBinder service)
{
callBackService = ICallBack.Stub.asInterface(service);
try
{
callBackService.asycMsg("OK!!!");
}
catch (RemoteException e)
{
e.printStackTrace();
}
}
};
<service
android:name="com.opensource.aidl.service.callback.CallBackService"
android:process=":remote" >
<intent-filter>
<action android:name="com.opensource.aidl.service.callback.ICallBack" />
</intent-filter>
</service>