element14 Community
element14 Community
    Register Log In
  • Site
  • Search
  • Log In Register
  • About Us
  • Community Hub
    Community Hub
    • What's New on element14
    • Feedback and Support
    • Benefits of Membership
    • Personal Blogs
    • Members Area
    • Achievement Levels
  • Learn
    Learn
    • Ask an Expert
    • eBooks
    • element14 presents
    • Learning Center
    • Tech Spotlight
    • STEM Academy
    • Webinars, Training and Events
    • Learning Groups
  • Technologies
    Technologies
    • 3D Printing
    • FPGA
    • Industrial Automation
    • Internet of Things
    • Power & Energy
    • Sensors
    • Technology Groups
  • Challenges & Projects
    Challenges & Projects
    • Design Challenges
    • element14 presents Projects
    • Project14
    • Arduino Projects
    • Raspberry Pi Projects
    • Project Groups
  • Products
    Products
    • Arduino
    • Avnet Boards Community
    • Dev Tools
    • Manufacturers
    • Multicomp Pro
    • Product Groups
    • Raspberry Pi
    • RoadTests & Reviews
  • Store
    Store
    • Visit Your Store
    • Choose another store...
      • Europe
      •  Austria (German)
      •  Belgium (Dutch, French)
      •  Bulgaria (Bulgarian)
      •  Czech Republic (Czech)
      •  Denmark (Danish)
      •  Estonia (Estonian)
      •  Finland (Finnish)
      •  France (French)
      •  Germany (German)
      •  Hungary (Hungarian)
      •  Ireland
      •  Israel
      •  Italy (Italian)
      •  Latvia (Latvian)
      •  
      •  Lithuania (Lithuanian)
      •  Netherlands (Dutch)
      •  Norway (Norwegian)
      •  Poland (Polish)
      •  Portugal (Portuguese)
      •  Romania (Romanian)
      •  Russia (Russian)
      •  Slovakia (Slovak)
      •  Slovenia (Slovenian)
      •  Spain (Spanish)
      •  Sweden (Swedish)
      •  Switzerland(German, French)
      •  Turkey (Turkish)
      •  United Kingdom
      • Asia Pacific
      •  Australia
      •  China
      •  Hong Kong
      •  India
      •  Korea (Korean)
      •  Malaysia
      •  New Zealand
      •  Philippines
      •  Singapore
      •  Taiwan
      •  Thailand (Thai)
      • Americas
      •  Brazil (Portuguese)
      •  Canada
      •  Mexico (Spanish)
      •  United States
      Can't find the country/region you're looking for? Visit our export site or find a local distributor.
  • Translate
  • Profile
  • Settings
Forget Me Not Design Challenge
  • Challenges & Projects
  • Design Challenges
  • Forget Me Not Design Challenge
  • More
  • Cancel
Forget Me Not Design Challenge
Blog Don't forget the windows 07 - Android programming can be hard
  • Blog
  • Forum
  • Documents
  • Files
  • Events
  • Mentions
  • Sub-Groups
  • Tags
  • More
  • Cancel
  • New
  • Share
  • More
  • Cancel
Group Actions
  • Group RSS
  • More
  • Cancel
Engagement
  • Author Author: hlipka
  • Date Created: 8 Oct 2014 10:06 PM Date Created
  • Views 540 views
  • Likes 0 likes
  • Comments 0 comments
  • forget_me_not
  • more_intelligent_home
Related
Recommended

Don't forget the windows 07 - Android programming can be hard

hlipka
hlipka
8 Oct 2014

Main part of my project is to create a specialized Android client, to get exactly the information I need in a compact and easy to understand way. The official OpenHAB client has two drawbacks that make it not suitable for my needs:

  • it runs only as full-screen application, but I would need a widget for the home screen
  • the update speed is rather slow - changes to the sensors are not reflected right away.

Thats why I set out to a journey in the OpenHAB REST API and the world of Android programming. I already wrote about the experiences with the REST API, so this time its about Android.

Android is different

Being a regular Java developer (doing this on my day job image I'm no stranger to the programming language, and also not to multi-threaded programming or GUI stuff (the OpenHAB library with all its background handling was not that complicated as such).

But since Android as OD for mobile devices needs to care a lot more about  system resources and power management, some concepts differ form what I was used to.

Android Studio can create a basic application for what I needed - a homepage widget with a configuration screen. That included all the needed configuration files. There is also a GUI editor to create the layout for the GUI elements. But there it ended - the is for example no support for editing the configuration files and manifest files (e.g. for adding permissions).

Indirection everywhere

That time-consuming operations are not allowed to be done in the main GUI code (the widegts update() method) is a common pattern for all GUI applications. But its also discouraged to just create a background thread for doing the heavy lifting. Instead, one is supposed to write a background service class, that then gets started in the background. This service then sends around broadcast messages (called 'Intent'), to which the widget then can listen (or rather a special broadcast listener get created that then updates the widget). To make it worse, the explanation in the Android documentation explains that, but the example shown is wrong (the code itself would not compile at all) and leaves out some details.

So it took me several days to find out whats happening here. To make it worse, Android comes with its own version of the Apache HTTP client (that my library uses), which handles some internals slightly different.

Debugging an external device?

And since the app does not run directly on the PC, debugging is a little bit more difficult than what I'm used to. There is an Android emulator available, to which the IDE can connect its debugger. But when an exception is thrown by the code, I did not get a log message - I only saw whats happening when stepping through my code. And even then I did not get a proper stacktrace (but at least an error message: 'Item may not be null' - huh?).

In the end I did fall back to do whats usually the last resort in the debugging world: paving my code with log statements. That allowed me to get a proper stacktrace, and to trace the execution of my program. Also, to make changes and debugging easier, I copied the code of my library into my Android project, instead of just using it as library.

Together with Google I also found the missing pieces of my program configuration (e.g. where to configure the permissions to allow network access) and so I got my first result back from the OpenHAB server (running in the emulator):

image

(I'm using the rocker switches for my experiments because they are much easier to handle)

Whats next?

Next steps will be to clean up the code I wrote and make sure there is nothing in there thats not needed for the current functionality. Then I want to use my library again (so it needs some small modifications). When that has been done, I can go an create a proper widget that shows what I need it to show.

  • Sign in to reply
element14 Community

element14 is the first online community specifically for engineers. Connect with your peers and get expert answers to your questions.

  • Members
  • Learn
  • Technologies
  • Challenges & Projects
  • Products
  • Store
  • About Us
  • Feedback & Support
  • FAQs
  • Terms of Use
  • Privacy Policy
  • Legal and Copyright Notices
  • Sitemap
  • Cookies

An Avnet Company © 2025 Premier Farnell Limited. All Rights Reserved.

Premier Farnell Ltd, registered in England and Wales (no 00876412), registered office: Farnell House, Forge Lane, Leeds LS12 2NE.

ICP 备案号 10220084.

Follow element14

  • X
  • Facebook
  • linkedin
  • YouTube