Android-getting Global Context Tips-android learning journey (68)
Source: Internet
Author: User
<span id="Label3"></p>We often need to get a global context, such as pop-up toast, launch activities, services, receivers, custom controls, operations databases, use notifications, etc. the usual method is to pass in the context parameter where it was called, and sometimes that doesn't Work. Teach you a common way to inherit the application class, and then get the static content code as follows<pre class="prettyprint"><pre class="prettyprint"><code class=" hljs java"><span class="hljs-keyword">public</span><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">MyApplication</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">Application</span>{</span> <span class="hljs-keyword">private</span><span class="hljs-keyword">static</span> Context context; <span class="hljs-annotation">@Override</span> <span class="hljs-keyword">public</span><span class="hljs-keyword">void</span><span class="hljs-title">onCreate</span>() { <span class="hljs-keyword">super</span>.onCreate(); context = getApplicationContext(); } <span class="hljs-keyword">public</span><span class="hljs-keyword">static</span><span class="hljs-title">getContext</span>(){ <span class="hljs-keyword">return</span> context; }}</code></pre></pre>well, it needs to be in manifest, if Application's so Property.<pre class="prettyprint"><pre class="prettyprint"><code class=" hljs perl">application android:name=<span class="hljs-string">"com.example.euler_kalvinhe.takephoto.MyApplication"</span> android:allowBackup=<span class="hljs-string">"true"</span> android:icon=<span class="hljs-string">"<span class="hljs-variable">@drawable</span>/ic_launcher"</span> android:label=<span class="hljs-string">"<span class="hljs-variable">@string</span>/app_name"</span> android:theme=<span class="hljs-string">"<span class="hljs-variable">@style</span>/AppTheme"</span> ><span class="hljs-variable">**</span></code></pre></pre> <p style="font-size:12px;"><p style="font-size:12px;">Copyright Notice: This article for Bo Master original article, without Bo Master permission not Reproduced.</p></p> <p><p>Android-getting Global Context Tips-android learning journey (68)</p></p></span>
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.
A Free Trial That Lets You Build Big!
Start building with 50+ products and up to 12 months usage for Elastic Compute Service