Android Basics Getting Started tutorial--3.6 responding to System-set events (configuration Class)
Source: Internet
Author: User
<span id="Label3"></p>Android Basics Getting Started tutorial--3.6 responding to System-set events (configuration Class)<p><p>Tags (space delimited): android basics Getting Started Tutorial</p></p>Introduction to this Section: <blockquote> <blockquote> <p>This section describes the configuration class is used to describe the mobile phone device information, such as the screen direction,<br>Touch screen Touch mode, and so on, believe that the custom ROM friends should know that we can in:<br>Frameworks/base/core/java/android/content/res/configuration.java<br>Find this class, and then change the settings, such as resizing the default font! Have the interest to know for yourself!<br>This section explains the use of the configuration class in our Android development ~<br>API Documentation: Configuration</p> </blockquote> </blockquote>1.Configuration give us a list of methods <blockquote> <ul> <li><li><strong>densitydpi</strong>: Screen density</li></li> <li><li><strong>Fontscale</strong>: The scaling factor for the font currently set by the user</li></li> <li><li><strong>Hardkeyboardhidden</strong>: To determine if the hard keyboard is visible, there are two optional values: hardkeyboardhidden_no,hardkeyboardhidden_yes, hexadecimal 0 and 1, respectively</li></li> <li><li><strong>keyboard</strong>: Gets the current associated amount keyboard type: The return value of the property: Keyboard_12key (keypad with 12 keys only), keyboard_nokeys, keyboard_qwerty (normal keyboard)</li></li> <li><li><strong>Keyboardhidden</strong>: This property returns a Boolean value that identifies whether the current keyboard is Available. This property will not only judge the System's hardware keyboard, It will also determine the System's soft keyboard (located on the screen).</li></li> <li><li><strong>locale</strong>: Get the User's current language environment</li></li> <li><li><strong>MCC</strong>: Get the country code for the mobile signal</li></li> <li><li><strong>MNC</strong>: Get the network code for the mobile signal<br>Ps: the country Code and Network code together determine the current mobile network operator</li></li> <li><li><strong>navigation</strong>: Determine the type of directional navigation device on the System. Return value for this property: navigation_nonav (no navigation),<br>Navigation_dpad (DPAD navigation) navigation_trackball (trackball navigation), navigation_wheel (wheel Navigation)</li></li> <li><li><strong>Orientation</strong>: Gets the orientation of the system Screen. Return value for this property: orientation_landscape (landscape screen), orientation_portrait (vertical screen)</li></li> <li><li><strong>SCREENHEIGHTDP</strong>,<strong>screenwidthdp</strong>: The screen can be high and wide, expressed in DP</li></li> <li><li><strong>touchscreen</strong>: Gets the touch mode of the system touch Screen. Return value for this property: touchscreen_notouch (no touch screen), touchscreen_stylus (touch pen Touch screen), touchscreen_finger (touch screen receiving Finger)</li></li> </ul> </blockquote>2. Write a simple example under Test:<p><p><strong>Run:</strong></p></p><p><p></p></p><p><p><strong>Code implementation:</strong></p></p><pre class="prettyprint"><code class="language-Java hljs avrasm">public class Mainactivity extends appcompatactivity {@Override protected void OnCreate (Bundle Savedinstancestate) { Super<span class="hljs-preprocessor"><span class="hljs-preprocessor">. OnCreate</span></span>(savedinstancestate)<span class="hljs-comment"><span class="hljs-comment">;</span></span>Setcontentview (R<span class="hljs-preprocessor"><span class="hljs-preprocessor">. Layout</span></span><span class="hljs-preprocessor"><span class="hljs-preprocessor">. Activity</span></span>_main)<span class="hljs-comment"><span class="hljs-comment">;</span></span>TextView Txtresult = (TextView) Findviewbyid (R<span class="hljs-preprocessor"><span class="hljs-preprocessor">. ID</span></span><span class="hljs-preprocessor"><span class="hljs-preprocessor">. Txtresult</span></span>)<span class="hljs-comment"><span class="hljs-comment">;</span></span>StringBuffer status = new StringBuffer ()<span class="hljs-comment"><span class="hljs-comment">;</span></span>① getting the configuration object of the system cfg = getresources ()<span class="hljs-preprocessor"><span class="hljs-preprocessor">. GetConfiguration</span></span>()<span class="hljs-comment"><span class="hljs-comment">;</span></span>② want to check what status?<span class="hljs-preprocessor"><span class="hljs-preprocessor">. Append</span></span>(<span class="hljs-string"><span class="hljs-string">"densitydpi:"</span></span>+ CFG<span class="hljs-preprocessor"><span class="hljs-preprocessor">. densitydpi</span></span>+<span class="hljs-string"><span class="hljs-string">"\ n"</span></span>)<span class="hljs-comment"><span class="hljs-comment">;</span></span>Status<span class="hljs-preprocessor"><span class="hljs-preprocessor">. Append</span></span>(<span class="hljs-string"><span class="hljs-string">"fontscale:"</span></span>+ CFG<span class="hljs-preprocessor"><span class="hljs-preprocessor">. Fontscale</span></span>+<span class="hljs-string"><span class="hljs-string">"\ n"</span></span>)<span class="hljs-comment"><span class="hljs-comment">;</span></span>Status<span class="hljs-preprocessor"><span class="hljs-preprocessor">. Append</span></span>(<span class="hljs-string"><span class="hljs-string">"hardkeyboardhidden:"</span></span>+ CFG<span class="hljs-preprocessor"><span class="hljs-preprocessor">. Hardkeyboardhidden</span></span>+<span class="hljs-string"><span class="hljs-string">"\ n"</span></span>)<span class="hljs-comment"><span class="hljs-comment">;</span></span>Status<span class="hljs-preprocessor"><span class="hljs-preprocessor">. Append</span></span>(<span class="hljs-string"><span class="hljs-string">"keyboard:"</span></span>+ CFG<span class="hljs-preprocessor"><span class="hljs-preprocessor">. Keyboard</span></span>+<span class="hljs-string"><span class="hljs-string">"\ n"</span></span>)<span class="hljs-comment"><span class="hljs-comment">;</span></span>Status<span class="hljs-preprocessor"><span class="hljs-preprocessor">. Append</span></span>(<span class="hljs-string"><span class="hljs-string">"keyboardhidden:"</span></span>+ CFG<span class="hljs-preprocessor"><span class="hljs-preprocessor">. Keyboardhidden</span></span>+<span class="hljs-string"><span class="hljs-string">"\ n"</span></span>)<span class="hljs-comment"><span class="hljs-comment">;</span></span>Status<span class="hljs-preprocessor"><span class="hljs-preprocessor">. Append</span></span>(<span class="hljs-string"><span class="hljs-string">"locale:"</span></span>+ CFG<span class="hljs-preprocessor"><span class="hljs-preprocessor">. Locale</span></span>+<span class="hljs-string"><span class="hljs-string">"\ n"</span></span>)<span class="hljs-comment"><span class="hljs-comment">;</span></span>Status<span class="hljs-preprocessor"><span class="hljs-preprocessor">. Append</span></span>(<span class="hljs-string"><span class="hljs-string">"mcc:"</span></span>+ CFG<span class="hljs-preprocessor"><span class="hljs-preprocessor">. MCC</span></span>+<span class="hljs-string"><span class="hljs-string">"\ n"</span></span>)<span class="hljs-comment"><span class="hljs-comment">;</span></span>Status<span class="hljs-preprocessor"><span class="hljs-preprocessor">. Append</span></span>(<span class="hljs-string"><span class="hljs-string">"mnc:"</span></span>+ CFG<span class="hljs-preprocessor"><span class="hljs-preprocessor">. MNC</span></span>+<span class="hljs-string"><span class="hljs-string">"\ n"</span></span>)<span class="hljs-comment"><span class="hljs-comment">;</span></span>Status<span class="hljs-preprocessor"><span class="hljs-preprocessor">. Append</span></span>(<span class="hljs-string"><span class="hljs-string">"navigation:"</span></span>+ CFG<span class="hljs-preprocessor"><span class="hljs-preprocessor">. Navigation</span></span>+<span class="hljs-string"><span class="hljs-string">"\ n"</span></span>)<span class="hljs-comment"><span class="hljs-comment">;</span></span>Status<span class="hljs-preprocessor"><span class="hljs-preprocessor">. Append</span></span>(<span class="hljs-string"><span class="hljs-string">"navigationhidden:"</span></span>+ CFG<span class="hljs-preprocessor"><span class="hljs-preprocessor">. Navigationhidden</span></span>+<span class="hljs-string"><span class="hljs-string">"\ n"</span></span>)<span class="hljs-comment"><span class="hljs-comment">;</span></span>Status<span class="hljs-preprocessor"><span class="hljs-preprocessor">. Append</span></span>(<span class="hljs-string"><span class="hljs-string">"orientation:"</span></span>+ CFG<span class="hljs-preprocessor"><span class="hljs-preprocessor">. Orientation</span></span>+<span class="hljs-string"><span class="hljs-string">"\ n"</span></span>)<span class="hljs-comment"><span class="hljs-comment">;</span></span>Status<span class="hljs-preprocessor"><span class="hljs-preprocessor">. Append</span></span>(<span class="hljs-string"><span class="hljs-string">"screenheightdp:"</span></span>+ CFG<span class="hljs-preprocessor"><span class="hljs-preprocessor">. SCREENHEIGHTDP</span></span>+<span class="hljs-string"><span class="hljs-string">"\ n"</span></span>)<span class="hljs-comment"><span class="hljs-comment">;</span></span>Status<span class="hljs-preprocessor"><span class="hljs-preprocessor">. Append</span></span>(<span class="hljs-string"><span class="hljs-string">"screenwidthdp:"</span></span>+ CFG<span class="hljs-preprocessor"><span class="hljs-preprocessor">. SCREENWIDTHDP</span></span>+<span class="hljs-string"><span class="hljs-string">"\ n"</span></span>)<span class="hljs-comment"><span class="hljs-comment">;</span></span>Status<span class="hljs-preprocessor"><span class="hljs-preprocessor">. Append</span></span>(<span class="hljs-string"><span class="hljs-string">"screenlayout:"</span></span>+ CFG<span class="hljs-preprocessor"><span class="hljs-preprocessor">. Screenlayout</span></span>+<span class="hljs-string"><span class="hljs-string">"\ n"</span></span>)<span class="hljs-comment"><span class="hljs-comment">;</span></span>Status<span class="hljs-preprocessor"><span class="hljs-preprocessor">. Append</span></span>(<span class="hljs-string"><span class="hljs-string">"smallestscreenwidthdp:"</span></span>+ CFG<span class="hljs-preprocessor"><span class="hljs-preprocessor">. densitydpi</span></span>+<span class="hljs-string"><span class="hljs-string">"\ n"</span></span>)<span class="hljs-comment"><span class="hljs-comment">;</span></span>Status<span class="hljs-preprocessor"><span class="hljs-preprocessor">. Append</span></span>(<span class="hljs-string"><span class="hljs-string">"touchscreen:"</span></span>+ CFG<span class="hljs-preprocessor"><span class="hljs-preprocessor">. densitydpi</span></span>+<span class="hljs-string"><span class="hljs-string">"\ n"</span></span>)<span class="hljs-comment"><span class="hljs-comment">;</span></span>Status<span class="hljs-preprocessor"><span class="hljs-preprocessor">. Append</span></span>(<span class="hljs-string"><span class="hljs-string">"uimode:"</span></span>+ CFG<span class="hljs-preprocessor"><span class="hljs-preprocessor">. densitydpi</span></span>+<span class="hljs-string"><span class="hljs-string">"\ n"</span></span>)<span class="hljs-comment"><span class="hljs-comment">;</span></span>Txtresult<span class="hljs-preprocessor"><span class="hljs-preprocessor">. SetText</span></span>(status<span class="hljs-preprocessor"><span class="hljs-preprocessor">. toString</span></span>())<span class="hljs-comment"><span class="hljs-comment">;</span></span>}}</code></pre>3. Override onconfigurationchanged Response system settings Change <blockquote> <blockquote> <p>This method is used to monitor the changes of the system settings, which is based on the callback time processing method, which automatically triggers when the system settings Change.<br>however, It is important to note that the Targetsdkversion property can only be set to 12, which is higher than 12 if the following method is used to monitor it, and the method will not be fired! Here to write a screen switch between the two examples for reference, others can self-google ~</p> </blockquote> </blockquote><p><p><strong>code Example</strong> : A simple button, click after toggle screen, then toast prompt</p></p><p><p><strong>Run:</strong></p></p><p><p></p></p><p><p><strong>Implementation Code:</strong></p></p><pre class="prettyprint"><code class="language-Java hljs java"><span class="hljs-keyword"><span class="hljs-keyword"></span> public</span> <span class="hljs-class"><span class="hljs-class"> <span class="hljs-keyword">class</span> <span class="hljs-title">mainactivity</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">Activity</span> {</span></span> <span class="hljs-annotation"><span class="hljs-annotation">@Override</span></span> <span class="hljs-keyword"><span class="hljs-keyword">protected</span></span> <span class="hljs-keyword"><span class="hljs-keyword">void</span></span> <span class="hljs-title"><span class="hljs-title">onCreate</span></span>(Bundle Savedinstancestate) {<span class="hljs-keyword"><span class="hljs-keyword">Super</span></span>. onCreate (savedinstancestate); Setcontentview (r.layout.activity_main); Button btn = (button) Findviewbyid (r.id.btncahange); Btn.setonclicklistener (<span class="hljs-keyword"><span class="hljs-keyword">New</span></span>Onclicklistener () {<span class="hljs-annotation"><span class="hljs-annotation">@Override</span></span> <span class="hljs-keyword"><span class="hljs-keyword"></span> public</span> <span class="hljs-keyword"><span class="hljs-keyword">void</span></span> <span class="hljs-title"><span class="hljs-title">OnClick</span></span>(View V) {Configuration config = getresources (). getconfiguration ();<span class="hljs-comment"><span class="hljs-comment">//if It is horizontal screen, switch to vertical screen</span></span> <span class="hljs-keyword"><span class="hljs-keyword">if</span></span>(config.orientation = = Configuration.orientation_landscape) {mainactivity.<span class="hljs-keyword"><span class="hljs-keyword"></span> this</span>. setrequestedorientation (activityinfo.screen_orientation_portrait); }<span class="hljs-comment"><span class="hljs-comment">//if the vertical screen words switch Cheng</span></span> <span class="hljs-keyword"><span class="hljs-keyword">if</span></span>(config.orientation = = Configuration.orientation_portrait) {mainactivity.<span class="hljs-keyword"><span class="hljs-keyword"></span> this</span>. setrequestedorientation (activityinfo.screen_orientation_landscape); } } }); }<span class="hljs-annotation"><span class="hljs-annotation">@Override</span></span> <span class="hljs-keyword"><span class="hljs-keyword"></span> public</span> <span class="hljs-keyword"><span class="hljs-keyword">void</span></span> <span class="hljs-title"><span class="hljs-title">onconfigurationchanged</span></span>(Configuration Newconfig) {<span class="hljs-keyword"><span class="hljs-keyword">Super</span></span>. onconfigurationchanged (newconfig); String screen = Newconfig.orientation = = configuration.orientation_landscape?<span class="hljs-string"><span class="hljs-string">"horizontal screen"</span></span>:<span class="hljs-string"><span class="hljs-string">"vertical screen"</span></span>; Toast.maketext (mainactivity.<span class="hljs-keyword"><span class="hljs-keyword"></span> this</span>,<span class="hljs-string"><span class="hljs-string">"the System screen orientation has changed \ n the modified direction is"</span></span>+ screen, toast.length_short). Show (); } }</code></pre><p><p>In addition, you need to add the following in Androidmanifest.xml:</p></p> <blockquote> <blockquote> <p>Permissions:<br><strong>< uses-permission android:name= "android.permission.CHANGE_CONFIGURATION"/></strong><br>In the < activity tab, add:<strong>android:configchanges= "orientation"</strong><br>Change the targetsdkversion to more than 12, 12 can also</p> </blockquote> </blockquote>This section summarizes:<p><p>This section explains: configuration class and onconfigurationchanged response system settings change, There is a general understanding can<br>Follow up with us to continue in depth ~</p></p> <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 Basics Getting Started tutorial--3.6 responding to System-set events (configuration Class)</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