Skip to content

Instantly share code, notes, and snippets.

@zodani
Created December 15, 2016 02:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zodani/ac3b262c68102dfc3a4e1c69acb590f1 to your computer and use it in GitHub Desktop.
Save zodani/ac3b262c68102dfc3a4e1c69acb590f1 to your computer and use it in GitHub Desktop.
AIDL client
public class MainActivity extends AppCompatActivity {
private static final String SERVER_PACKAGE = "com.jaeryong.android.aidlserver";
private static final String SERVER_ACTION = "com.jaeryong.android.action.aidlserver";
private IRemoteService mRemoteService;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Intent serviceIntent = new Intent(SERVER_ACTION);
serviceIntent.setPackage(SERVER_PACKAGE);
bindService(serviceIntent, serviceConnection, Context.BIND_AUTO_CREATE);
Button button = (Button)findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
try {
int sum = mRemoteService.sum(2, 5);
Toast.makeText(getApplicationContext(), String.valueOf(sum), Toast.LENGTH_SHORT).show();
} catch (RemoteException e) {
e.printStackTrace();
}
}
});
}
@Override
protected void onDestroy() {
super.onDestroy();
unbindService(serviceConnection);
}
private ServiceConnection serviceConnection = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
mRemoteService = IRemoteService.Stub.asInterface(service);
}
@Override
public void onServiceDisconnected(ComponentName name) {
mRemoteService = null;
}
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment