Reader small image

You're reading from  Android Things Projects

Product typeBook
Published inJun 2017
Reading LevelBeginner
PublisherPackt
ISBN-139781787289246
Edition1st Edition
Languages
Right arrow
Author (1)
Francesco Azzola
Francesco Azzola
author image
Francesco Azzola

Francesco Azzola is an electronic engineer with over 15 years of experience in computer programming and JEE architecture. He is SCEA certified (Sun Certified Enterprise Architect), SCWCD, and SCJP. He is an Android and IoT enthusiast. He loves creating IoT projects using Arduino, Raspberry Pi, Android, and other platforms. He is interested in the convergence between IoT and mobile applications. Previously, he worked in the mobile development field for several years. He has created a blog called survivingwithandroid,where he shares posts about coding in Android and IoT projects.
Read more about Francesco Azzola

Right arrow

How to close the connection with a GPIO pin


In this last step, we will learn how to close the connection with a GPIO pin. This is an important step because in this way, we free the resources and remove all the listeners we added to the GPIO pins.

An Android Things app has a life cycle very similar to an Android app. The place where we implement these actions is the Activity onDestroy method. In this method we have to:

  • Remove all the listeners attached to the GPIO pins
  • Close the connection to the GPIO pins

So, open MainActivity.java again and look for the onDestroy method and modify it:

@Override
protected void onDestroy()
  { super.onDestroy(); Log.d(TAG, "onDestroy");
  if (gpioPin != null) {
    gpioPin.unregisterGpioCallback(sensorCallback);
      try {
    gpioPin.close(); gpioPin = null;
      }
    catch(Exception e) {}
  }
}
lock icon
The rest of the page is locked
Previous PageNext Page
You have been reading a chapter from
Android Things Projects
Published in: Jun 2017Publisher: PacktISBN-13: 9781787289246

Author (1)

author image
Francesco Azzola

Francesco Azzola is an electronic engineer with over 15 years of experience in computer programming and JEE architecture. He is SCEA certified (Sun Certified Enterprise Architect), SCWCD, and SCJP. He is an Android and IoT enthusiast. He loves creating IoT projects using Arduino, Raspberry Pi, Android, and other platforms. He is interested in the convergence between IoT and mobile applications. Previously, he worked in the mobile development field for several years. He has created a blog called survivingwithandroid,where he shares posts about coding in Android and IoT projects.
Read more about Francesco Azzola