Copy the source to the "spare parts" and see how they do it.
(also copy it from the emulator, install it on your phone and make sure it works)
Note: while this remains accurate for the Android versions in 2010 that were written about, restrictions added years later in 4.4 and 5.0 would significantly hinder achieving this in later versions.
source share