{"id":862,"date":"2016-07-28T16:33:09","date_gmt":"2016-07-28T16:33:09","guid":{"rendered":"http:\/\/blog.silverterra.net\/?p=862"},"modified":"2016-07-28T16:33:44","modified_gmt":"2016-07-28T16:33:44","slug":"using-context-in-a-static-environment","status":"publish","type":"post","link":"https:\/\/blog.silverterra.net\/?p=862","title":{"rendered":"using Context in a static environment"},"content":{"rendered":"<blockquote><p>Goal: get Context in a static method<\/p><\/blockquote>\n<p>Sometimes we need a context in a static method, but we don&#8217;t have&#8230; Let&#8217;s dig one.<\/p>\n<pre data-enlighter-language=\"java\" class=\"EnlighterJSRAW\">\r\n  public static Application getGlobalContext(){\r\n    try {\r\n      final Class&lt;?&gt; activityThreadClass =  Class.forName(&quot;android.app.ActivityThread&quot;);\r\n      final Method method = activityThreadClass.getMethod(&quot;currentApplication&quot;);\r\n      return (Application) method.invoke(null, (Object[]) null);\r\n    } catch (final ClassNotFoundException e) { \/\/ handle exception\r\n    } catch (final NoSuchMethodException e) { \/\/ handle exception\r\n    } catch (final IllegalArgumentException e) { \/\/ handle exception\r\n    } catch (final IllegalAccessException e) { \/\/ handle exception\r\n    } catch (final InvocationTargetException e) { \/\/ handle exception\r\n    }\r\n    return null;\r\n  }\r\n<\/pre>\n<p>example: get the device ID (IMEI) in a static function.<\/p>\n<pre data-enlighter-language=\"java\" class=\"EnlighterJSRAW\">\r\n  public static String deviceID_IMEI(){\r\n    TelephonyManager tm = \r\n      (TelephonyManager)getGlobalContext().getSystemService(Context.TELEPHONY_SERVICE);\r\n    String device_id = tm.getDeviceId();\r\n    return device_id;\r\n  }\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Goal: get Context in a static method Sometimes we need a context in a static method, but we don&#8217;t have&#8230; Let&#8217;s dig one. example: get the device ID (IMEI) in a static function.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[18],"tags":[],"class_list":["post-862","post","type-post","status-publish","format-standard","hentry","category-android"],"_links":{"self":[{"href":"https:\/\/blog.silverterra.net\/index.php?rest_route=\/wp\/v2\/posts\/862","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.silverterra.net\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.silverterra.net\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.silverterra.net\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.silverterra.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=862"}],"version-history":[{"count":2,"href":"https:\/\/blog.silverterra.net\/index.php?rest_route=\/wp\/v2\/posts\/862\/revisions"}],"predecessor-version":[{"id":864,"href":"https:\/\/blog.silverterra.net\/index.php?rest_route=\/wp\/v2\/posts\/862\/revisions\/864"}],"wp:attachment":[{"href":"https:\/\/blog.silverterra.net\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=862"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.silverterra.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=862"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.silverterra.net\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=862"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}