You can study this sample code:
Also pay attention to the section with code examples, there are a lot of useful things there.