{"id":360,"date":"2014-07-10T20:22:11","date_gmt":"2014-07-10T19:22:11","guid":{"rendered":"http:\/\/www.sebcharrot.com\/blog\/?p=360"},"modified":"2014-07-10T20:22:11","modified_gmt":"2014-07-10T19:22:11","slug":"debugging-an-android-service","status":"publish","type":"post","link":"http:\/\/www.sebcharrot.com\/blog\/debugging-an-android-service\/","title":{"rendered":"Debugger this! Debugging an Android Service"},"content":{"rendered":"<p>I recently found out that I couldn\u2019t hit any breakpoints in an Android Service I was developing. I found that this was easily sorted by adding the following line:<\/p>\n<pre style=\"padding-left: 30px;\">android.os.Debug.waitForDebugger()<\/pre>\n<p>This approach was suggested on various websites including <a href=\"http:\/\/stackoverflow.com\/questions\/13562011\/breakpoint-in-service-not-working\" target=\"_blank\" rel=\"noopener noreferrer\">StackOverflow <\/a>and <a href=\"http:\/\/www.helloandroid.com\/tutorials\/how-debug-service\" target=\"_blank\" rel=\"noopener noreferrer\">HelloAndroid <\/a>amongst many. Great.<\/p>\n<p>I found out much later than when I ran the code \u201cin the wild\u201d i.e. in a production environment unattached to a debugger, my application was failing.<\/p>\n<p>Long story short, this is caused by the waitForDebugger() call, which will cause any code following your invocation to not be executed if there is no debugger.<\/p>\n<p>I suppose I should have realised, but I had assumed that maybe the runtime or the call itself would be clever enough to know whether the application was in debug mode at all, and ignore it if it wasn\u2019t.<\/p>\n<p>But thankfully, <a href=\"https:\/\/developer.android.com\/reference\/android\/os\/Debug.html#isDebuggerConnected()\" target=\"_blank\" rel=\"noopener noreferrer\">we can do that<\/a>:<\/p>\n<pre style=\"padding-left: 30px;\">if (android.os.Debug.isDebuggerConnected)\n{\n    android.os.Debug.waitForDebugger()\n}<\/pre>\n<p>So I\u2019d recommend this is the approach you take over having to remember to take that call out.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I recently found out that I couldn\u2019t hit any breakpoints in an Android Service I was developing. I found that this was easily sorted by adding the following line: android.os.Debug.waitForDebugger() This approach was suggested on various websites including StackOverflow and HelloAndroid amongst many. Great. I found out much later than when I ran the code&hellip;&nbsp;<a href=\"http:\/\/www.sebcharrot.com\/blog\/debugging-an-android-service\/\" class=\"\" rel=\"bookmark\">Read More &raquo;<span class=\"screen-reader-text\">Debugger this! Debugging an Android Service<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"neve_meta_sidebar":"","neve_meta_container":"","neve_meta_enable_content_width":"","neve_meta_content_width":0,"neve_meta_title_alignment":"","neve_meta_author_avatar":"","neve_post_elements_order":"","neve_meta_disable_header":"","neve_meta_disable_footer":"","neve_meta_disable_title":""},"categories":[21],"tags":[28,66,91,92,123,160],"_links":{"self":[{"href":"http:\/\/www.sebcharrot.com\/blog\/wp-json\/wp\/v2\/posts\/360"}],"collection":[{"href":"http:\/\/www.sebcharrot.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.sebcharrot.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.sebcharrot.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.sebcharrot.com\/blog\/wp-json\/wp\/v2\/comments?post=360"}],"version-history":[{"count":0,"href":"http:\/\/www.sebcharrot.com\/blog\/wp-json\/wp\/v2\/posts\/360\/revisions"}],"wp:attachment":[{"href":"http:\/\/www.sebcharrot.com\/blog\/wp-json\/wp\/v2\/media?parent=360"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.sebcharrot.com\/blog\/wp-json\/wp\/v2\/categories?post=360"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.sebcharrot.com\/blog\/wp-json\/wp\/v2\/tags?post=360"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}