In app, that provide service i have two AIDL files:
interface ICountTest {
oneway void count(in INotifierTest test);
}
interface INotifierTest {
oneway void notify(int count);
}
Basically i want to count till Integer.MAX_VALUE
in for
loop and then give this value to notifier, so he could show it visually in other app
For this i have a service
xml:
<service android:name=".service.TestService"
android:process=".remote"
android:exported="true">
<intent-filter>
<action android:name="TestService.START_SERVICE" />
</intent-filter>
</service>
(I've added intent filter to try start it from different approach)
java:
public class TestService extends Service{
private ICountTest.Stub mBinder;
@Override
public void onCreate() {
super.onCreate();
mBinder = new ICountTest.Stub() {
@Override
public void count(INotifierTest notifier) throws RemoteException {
int k = 0;
for(int i = 0; i < Integer.MAX_VALUE; i++) {
k = i;
}
notifier.notify(k);
}
};
}
@Nullable
@Override
public IBinder onBind(Intent intent) {
return mBinder;
}
}
In my MainActivity
class i've overriden notify
method:
private INotifierTest.Stub mINotifierTest = new INotifierTest.Stub() {
@Override
public void notify(int count) throws RemoteException {
final String result = String.valueOf(count);
Log.d("aidl_test", result);
MainActivity.this.runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(MainActivity.this, result, Toast.LENGTH_LONG).show();
}
});
}
};
I'm calling count
method in onServiceConnected
:
private ServiceConnection mServiceConnection = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
mCountTest = ICountTest.Stub.asInterface(iBinder);
try {
Log.d("aidl_test", Thread.currentThread().getName());
mCountTest.count(mINotifierTest);
} catch (RemoteException e) {
e.printStackTrace();
}
}
@Override
public void onServiceDisconnected(ComponentName componentName) {
}
};
But when i need to run it from another app, for example, like this:
Intent i = new Intent();
i.setComponent(new ComponentName("com.xxx.xxx", "com.xxx.xxxx.service.TestService"));
getActivity().bindService(i, mServiceConntection, Context.BIND_AUTO_CREATE);
I need to provide my own ServiceConnection
class, which will override needed functionality, i'm getting binder in it, but basically i have only class name for needed interface and therefore, i can't do nothing with it.
I've also tried to just startService
but, logically, it won't work.
I think i'm doing something really wrong, please help me to understand.
My goal is : to run count
method from TestService
binder in another application, and see Toast
and Log
with result.
EDIT: I've ried to create same AIDL files with the same package and code as in first app, and using them, but it doesn't work.
EDIT: Wow, it was my fault, i've just overriden not INotifierTest.Stub
, but just INotifierTest
, thanks to all for the help