{"id":869,"date":"2016-07-29T11:54:36","date_gmt":"2016-07-29T11:54:36","guid":{"rendered":"http:\/\/blog.silverterra.net\/?p=869"},"modified":"2016-07-29T12:06:57","modified_gmt":"2016-07-29T12:06:57","slug":"using-context-in-a-static-environment-easy-way","status":"publish","type":"post","link":"https:\/\/blog.silverterra.net\/?p=869","title":{"rendered":"using Context in a static environment (simple way)"},"content":{"rendered":"<blockquote><p>Goal: find a simple way to reach a usable context in a static method<\/p><\/blockquote>\n<p>I have found many complicated ways to get a usable context, but I have an easy and simple way.<\/p>\n<p>1. we need a static class, with a global context variable<\/p>\n<pre data-enlighter-language=\"java\" class=\"EnlighterJSRAW\">\r\npublic class GLB {\r\n  ...\r\n  public static Context context; \/\/ context or static use\r\n  ...\r\n  public static Context globalContext(){ \/\/ get the global context\r\n    return GLB.context;\r\n  }\r\n  ...\r\n}\r\n<\/pre>\n<p>2. set this variable in the main activity<\/p>\n<pre data-enlighter-language=\"java\" class=\"EnlighterJSRAW\">\r\n@Override\r\nprotected void onCreate(Bundle savedInstanceState) {\r\n  super.onCreate(savedInstanceState);\r\n  GLB.context = getApplicationContext();\r\n  ...\r\n}\r\n<\/pre>\n<p>3. and of course we can get this context anywhere where we want<\/p>\n<pre data-enlighter-language=\"java\" class=\"EnlighterJSRAW\">\r\npublic static String deviceID_IMEI(){\r\n  TelephonyManager tm = \r\n  (TelephonyManager)globalContext().getSystemService(Context.TELEPHONY_SERVICE);\r\n  String device_id = tm.getDeviceId();\r\n  return device_id;\r\n}\r\n<\/pre>\n<p>so easy enough&#8230;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Goal: find a simple way to reach a usable context in a static method I have found many complicated ways to get a usable context, but I have an easy and simple way. 1. we need a static class, with a global context variable 2. set this variable in the main activity 3. and of [&hellip;]<\/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-869","post","type-post","status-publish","format-standard","hentry","category-android"],"_links":{"self":[{"href":"https:\/\/blog.silverterra.net\/index.php?rest_route=\/wp\/v2\/posts\/869","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=869"}],"version-history":[{"count":5,"href":"https:\/\/blog.silverterra.net\/index.php?rest_route=\/wp\/v2\/posts\/869\/revisions"}],"predecessor-version":[{"id":874,"href":"https:\/\/blog.silverterra.net\/index.php?rest_route=\/wp\/v2\/posts\/869\/revisions\/874"}],"wp:attachment":[{"href":"https:\/\/blog.silverterra.net\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=869"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.silverterra.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=869"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.silverterra.net\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=869"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}