Your approach is right way to do it. Here is some sample code which worked on a device. public void ...
stackoverflow.com