【Android 】零基础到飞升 | 响应系统设置的事件(Configuration类)

3.6 响应系统设置的事件(Configuration类)

分类 Android 基础入门教程

本节引言:

本节给大家介绍的Configuration类是用来描述手机设备的配置信息的,比如屏幕方向, 触摸屏的触摸方式等,相信定制过ROM的朋友都应该知道我们可以在: frameworks/base/core/java/android/content/res/Configuration.java 找到这个类,然后改下相关设置,比如调整默认字体的大小!有兴趣可自行了解! 本节讲解的Configuration类在我们Android开发中的使用~ API文档:Configuration


1.Configuration给我们提供的方法列表

  • densityDpi:屏幕密度
  • fontScale:当前用户设置的字体的缩放因子
  • hardKeyboardHidden:判断硬键盘是否可见,有两个可选值:HARDKEYBOARDHIDDEN_NO,HARDKEYBOARDHIDDEN_YES,分别是十六进制的0和1
  • keyboard:获取当前关联额键盘类型:该属性的返回值:KEYBOARD_12KEY(只有12个键的小键盘)、KEYBOARD_NOKEYS、KEYBOARD_QWERTY(普通键盘)
  • keyboardHidden:该属性返回一个boolean值用于标识当前键盘是否可用。该属性不仅会判断系统的硬件键盘,也会判断系统的软键盘(位于屏幕)。
  • locale:获取用户当前的语言环境
  • mcc:获取移动信号的国家码
  • mnc:获取移动信号的网络码
    ps:国家代码和网络代码共同确定当前手机网络运营商
  • navigation:判断系统上方向导航设备的类型。该属性的返回值:NAVIGATION_NONAV(无导航)、 NAVIGATION_DPAD(DPAD导航)NAVIGATION_TRACKBALL(轨迹球导航)、NAVIGATION_WHEEL(滚轮导航)
  • orientation:获取系统屏幕的方向。该属性的返回值:ORIENTATION_LANDSCAPE(横向屏幕)、ORIENTATION_PORTRAIT(竖向屏幕)
  • screenHeightDpscreenWidthDp:屏幕可用高和宽,用dp表示
  • touchscreen:获取系统触摸屏的触摸方式。该属性的返回值:TOUCHSCREEN_NOTOUCH(无触摸屏)、TOUCHSCREEN_STYLUS(触摸笔式触摸屏)、TOUCHSCREEN_FINGER(接收手指的触摸屏)

2.写个简单例子测试下:

运行截图:

【Android 】零基础到飞升 | 响应系统设置的事件(Configuration类)

代码实现:

public class MainActivity extends AppCompatActivity {
</span><span class="lit">@Override</span><span class="pln">
</span><span class="kwd">protected</span><span class="pln"> </span><span class="kwd">void</span><span class="pln"> onCreate</span><span class="pun">(</span><span class="typ">Bundle</span><span class="pln"> savedInstanceState</span><span class="pun">)</span><span class="pln"> </span><span class="pun">{</span><span class="pln">
    </span><span class="kwd">super</span><span class="pun">.</span><span class="pln">onCreate</span><span class="pun">(</span><span class="pln">savedInstanceState</span><span class="pun">);</span><span class="pln">
    setContentView</span><span class="pun">(</span><span class="pln">R</span><span class="pun">.</span><span class="pln">layout</span><span class="pun">.</span><span class="pln">activity_main</span><span class="pun">);</span><span class="pln">
    </span><span class="typ">TextView</span><span class="pln"> txtResult </span><span class="pun">=</span><span class="pln"> </span><span class="pun">(</span><span class="typ">TextView</span><span class="pun">)</span><span class="pln"> findViewById</span><span class="pun">(</span><span class="pln">R</span><span class="pun">.</span><span class="pln">id</span><span class="pun">.</span><span class="pln">txtResult</span><span class="pun">);</span><span class="pln">
    </span><span class="typ">StringBuffer</span><span class="pln"> status </span><span class="pun">=</span><span class="pln"> </span><span class="kwd">new</span><span class="pln"> </span><span class="typ">StringBuffer</span><span class="pun">();</span><span class="pln">
    </span><span class="com">//①获取系统的Configuration对象</span><span class="pln">
    </span><span class="typ">Configuration</span><span class="pln"> cfg </span><span class="pun">=</span><span class="pln"> getResources</span><span class="pun">().</span><span class="pln">getConfiguration</span><span class="pun">();</span><span class="pln">
    </span><span class="com">//②想查什么查什么</span><span class="pln">
    status</span><span class="pun">.</span><span class="pln">append</span><span class="pun">(</span><span class="str">"densityDpi:"</span><span class="pln"> </span><span class="pun">+</span><span class="pln"> cfg</span><span class="pun">.</span><span class="pln">densityDpi </span><span class="pun">+</span><span class="pln"> </span><span class="str">"\n"</span><span class="pun">);</span><span class="pln">
    status</span><span class="pun">.</span><span class="pln">append</span><span class="pun">(</span><span class="str">"fontScale:"</span><span class="pln"> </span><span class="pun">+</span><span class="pln"> cfg</span><span class="pun">.</span><span class="pln">fontScale </span><span class="pun">+</span><span class="pln"> </span><span class="str">"\n"</span><span class="pun">);</span><span class="pln">
    status</span><span class="pun">.</span><span class="pln">append</span><span class="pun">(</span><span class="str">"hardKeyboardHidden:"</span><span class="pln"> </span><span class="pun">+</span><span class="pln"> cfg</span><span class="pun">.</span><span class="pln">hardKeyboardHidden </span><span class="pun">+</span><span class="pln"> </span><span class="str">"\n"</span><span class="pun">);</span><span class="pln">
    status</span><span class="pun">.</span><span class="pln">append</span><span class="pun">(</span><span class="str">"keyboard:"</span><span class="pln"> </span><span class="pun">+</span><span class="pln"> cfg</span><span class="pun">.</span><span class="pln">keyboard </span><span class="pun">+</span><span class="pln"> </span><span class="str">"\n"</span><span class="pun">);</span><span class="pln">
    status</span><span class="pun">.</span><span class="pln">append</span><span class="pun">(</span><span class="str">"keyboardHidden:"</span><span class="pln"> </span><span class="pun">+</span><span class="pln"> cfg</span><span class="pun">.</span><span class="pln">keyboardHidden </span><span class="pun">+</span><span class="pln"> </span><span class="str">"\n"</span><span class="pun">);</span><span class="pln">
    status</span><span class="pun">.</span><span class="pln">append</span><span class="pun">(</span><span class="str">"locale:"</span><span class="pln"> </span><span class="pun">+</span><span class="pln"> cfg</span><span class="pun">.</span><span class="pln">locale </span><span class="pun">+</span><span class="pln"> </span><span class="str">"\n"</span><span class="pun">);</span><span class="pln">
    status</span><span class="pun">.</span><span class="pln">append</span><span class="pun">(</span><span class="str">"mcc:"</span><span class="pln"> </span><span class="pun">+</span><span class="pln"> cfg</span><span class="pun">.</span><span class="pln">mcc </span><span class="pun">+</span><span class="pln"> </span><span class="str">"\n"</span><span class="pun">);</span><span class="pln">
    status</span><span class="pun">.</span><span class="pln">append</span><span class="pun">(</span><span class="str">"mnc:"</span><span class="pln"> </span><span class="pun">+</span><span class="pln"> cfg</span><span class="pun">.</span><span class="pln">mnc </span><span class="pun">+</span><span class="pln"> </span><span class="str">"\n"</span><span class="pun">);</span><span class="pln">
    status</span><span class="pun">.</span><span class="pln">append</span><span class="pun">(</span><span class="str">"navigation:"</span><span class="pln"> </span><span class="pun">+</span><span class="pln"> cfg</span><span class="pun">.</span><span class="pln">navigation </span><span class="pun">+</span><span class="pln"> </span><span class="str">"\n"</span><span class="pun">);</span><span class="pln">
    status</span><span class="pun">.</span><span class="pln">append</span><span class="pun">(</span><span class="str">"navigationHidden:"</span><span class="pln"> </span><span class="pun">+</span><span class="pln"> cfg</span><span class="pun">.</span><span class="pln">navigationHidden </span><span class="pun">+</span><span class="pln"> </span><span class="str">"\n"</span><span class="pun">);</span><span class="pln">
    status</span><span class="pun">.</span><span class="pln">append</span><span class="pun">(</span><span class="str">"orientation:"</span><span class="pln"> </span><span class="pun">+</span><span class="pln"> cfg</span><span class="pun">.</span><span class="pln">orientation </span><span class="pun">+</span><span class="pln"> </span><span class="str">"\n"</span><span class="pun">);</span><span class="pln">
    status</span><span class="pun">.</span><span class="pln">append</span><span class="pun">(</span><span class="str">"screenHeightDp:"</span><span class="pln"> </span><span class="pun">+</span><span class="pln"> cfg</span><span class="pun">.</span><span class="pln">screenHeightDp </span><span class="pun">+</span><span class="pln"> </span><span class="str">"\n"</span><span class="pun">);</span><span class="pln">
    status</span><span class="pun">.</span><span class="pln">append</span><span class="pun">(</span><span class="str">"screenWidthDp:"</span><span class="pln"> </span><span class="pun">+</span><span class="pln"> cfg</span><span class="pun">.</span><span class="pln">screenWidthDp </span><span class="pun">+</span><span class="pln"> </span><span class="str">"\n"</span><span class="pun">);</span><span class="pln">
    status</span><span class="pun">.</span><span class="pln">append</span><span class="pun">(</span><span class="str">"screenLayout:"</span><span class="pln"> </span><span class="pun">+</span><span class="pln"> cfg</span><span class="pun">.</span><span class="pln">screenLayout </span><span class="pun">+</span><span class="pln"> </span><span class="str">"\n"</span><span class="pun">);</span><span class="pln">
    status</span><span class="pun">.</span><span class="pln">append</span><span class="pun">(</span><span class="str">"smallestScreenWidthDp:"</span><span class="pln"> </span><span class="pun">+</span><span class="pln"> cfg</span><span class="pun">.</span><span class="pln">densityDpi </span><span class="pun">+</span><span class="pln"> </span><span class="str">"\n"</span><span class="pun">);</span><span class="pln">
    status</span><span class="pun">.</span><span class="pln">append</span><span class="pun">(</span><span class="str">"touchscreen:"</span><span class="pln"> </span><span class="pun">+</span><span class="pln"> cfg</span><span class="pun">.</span><span class="pln">densityDpi </span><span class="pun">+</span><span class="pln"> </span><span class="str">"\n"</span><span class="pun">);</span><span class="pln">
    status</span><span class="pun">.</span><span class="pln">append</span><span class="pun">(</span><span class="str">"uiMode:"</span><span class="pln"> </span><span class="pun">+</span><span class="pln"> cfg</span><span class="pun">.</span><span class="pln">densityDpi </span><span class="pun">+</span><span class="pln"> </span><span class="str">"\n"</span><span class="pun">);</span><span class="pln">
    txtResult</span><span class="pun">.</span><span class="pln">setText</span><span class="pun">(</span><span class="pln">status</span><span class="pun">.</span><span class="pln">toString</span><span class="pun">());</span><span class="pln">
</span><span class="pun">}</span><span class="pln">

}


3.重写onConfigurationChanged响应系统设置更改

该方法用于监听系统设置的更改,是基于回调的时间处理方法,当系统的设置发生改变时就会自动触发; 但是要注意一点,使用下面的方法监控的话,targetSdkVersion属性最高只能设置为12,高于12的话,该方法不会被激发!这里写个横竖屏切换的例子给大家参考参考,其他的可自行谷歌~

代码示例:简单的一个按钮,点击后切换横竖屏,然后Toast提示

运行效果图:

【Android 】零基础到飞升 | 响应系统设置的事件(Configuration类)【Android 】零基础到飞升 | 响应系统设置的事件(Configuration类)

实现代码:

public class MainActivity extends Activity {  
</span><span class="lit">@Override</span><span class="pln">  
</span><span class="kwd">protected</span><span class="pln"> </span><span class="kwd">void</span><span class="pln"> onCreate</span><span class="pun">(</span><span class="typ">Bundle</span><span class="pln"> savedInstanceState</span><span class="pun">)</span><span class="pln"> </span><span class="pun">{</span><span class="pln">  
    </span><span class="kwd">super</span><span class="pun">.</span><span class="pln">onCreate</span><span class="pun">(</span><span class="pln">savedInstanceState</span><span class="pun">);</span><span class="pln">  
    setContentView</span><span class="pun">(</span><span class="pln">R</span><span class="pun">.</span><span class="pln">layout</span><span class="pun">.</span><span class="pln">activity_main</span><span class="pun">);</span><span class="pln">  
      
    </span><span class="typ">Button</span><span class="pln"> btn </span><span class="pun">=</span><span class="pln"> </span><span class="pun">(</span><span class="typ">Button</span><span class="pun">)</span><span class="pln"> findViewById</span><span class="pun">(</span><span class="pln">R</span><span class="pun">.</span><span class="pln">id</span><span class="pun">.</span><span class="pln">btncahange</span><span class="pun">);</span><span class="pln">  
    btn</span><span class="pun">.</span><span class="pln">setOnClickListener</span><span class="pun">(</span><span class="kwd">new</span><span class="pln"> </span><span class="typ">OnClickListener</span><span class="pun">()</span><span class="pln"> </span><span class="pun">{</span><span class="pln">  
          
        </span><span class="lit">@Override</span><span class="pln">  
        </span><span class="kwd">public</span><span class="pln"> </span><span class="kwd">void</span><span class="pln"> onClick</span><span class="pun">(</span><span class="typ">View</span><span class="pln"> v</span><span class="pun">)</span><span class="pln"> </span><span class="pun">{</span><span class="pln">  
            </span><span class="typ">Configuration</span><span class="pln"> config </span><span class="pun">=</span><span class="pln"> getResources</span><span class="pun">().</span><span class="pln">getConfiguration</span><span class="pun">();</span><span class="pln">  
            </span><span class="com">//如果是横屏的话切换成竖屏  </span><span class="pln">
            </span><span class="kwd">if</span><span class="pun">(</span><span class="pln">config</span><span class="pun">.</span><span class="pln">orientation </span><span class="pun">==</span><span class="pln"> </span><span class="typ">Configuration</span><span class="pun">.</span><span class="pln">ORIENTATION_LANDSCAPE</span><span class="pun">)</span><span class="pln">  
            </span><span class="pun">{</span><span class="pln">  
                </span><span class="typ">MainActivity</span><span class="pun">.</span><span class="kwd">this</span><span class="pun">.</span><span class="pln">setRequestedOrientation</span><span class="pun">(</span><span class="typ">ActivityInfo</span><span class="pun">.</span><span class="pln">SCREEN_ORIENTATION_PORTRAIT</span><span class="pun">);</span><span class="pln">  
            </span><span class="pun">}</span><span class="pln">  
            </span><span class="com">//如果竖屏的话切换成横屏  </span><span class="pln">
            </span><span class="kwd">if</span><span class="pun">(</span><span class="pln">config</span><span class="pun">.</span><span class="pln">orientation </span><span class="pun">==</span><span class="pln"> </span><span class="typ">Configuration</span><span class="pun">.</span><span class="pln">ORIENTATION_PORTRAIT</span><span class="pun">)</span><span class="pln">  
            </span><span class="pun">{</span><span class="pln">  
                </span><span class="typ">MainActivity</span><span class="pun">.</span><span class="kwd">this</span><span class="pun">.</span><span class="pln">setRequestedOrientation</span><span class="pun">(</span><span class="typ">ActivityInfo</span><span class="pun">.</span><span class="pln">SCREEN_ORIENTATION_LANDSCAPE</span><span class="pun">);</span><span class="pln">  
            </span><span class="pun">}</span><span class="pln">  
        </span><span class="pun">}</span><span class="pln">  
    </span><span class="pun">});</span><span class="pln">  
</span><span class="pun">}</span><span class="pln">  
  
</span><span class="lit">@Override</span><span class="pln">  
</span><span class="kwd">public</span><span class="pln"> </span><span class="kwd">void</span><span class="pln"> onConfigurationChanged</span><span class="pun">(</span><span class="typ">Configuration</span><span class="pln"> newConfig</span><span class="pun">)</span><span class="pln"> </span><span class="pun">{</span><span class="pln">  
    </span><span class="kwd">super</span><span class="pun">.</span><span class="pln">onConfigurationChanged</span><span class="pun">(</span><span class="pln">newConfig</span><span class="pun">);</span><span class="pln">  
    </span><span class="typ">String</span><span class="pln"> screen </span><span class="pun">=</span><span class="pln"> newConfig</span><span class="pun">.</span><span class="pln">orientation </span><span class="pun">==</span><span class="pln"> </span><span class="typ">Configuration</span><span class="pun">.</span><span class="pln">ORIENTATION_LANDSCAPE</span><span class="pun">?</span><span class="str">"横屏"</span><span class="pun">:</span><span class="str">"竖屏"</span><span class="pun">;</span><span class="pln">  
    </span><span class="typ">Toast</span><span class="pun">.</span><span class="pln">makeText</span><span class="pun">(</span><span class="typ">MainActivity</span><span class="pun">.</span><span class="kwd">this</span><span class="pun">,</span><span class="pln"> </span><span class="str">"系统屏幕方向发生改变 \n 修改后的方向为"</span><span class="pln"> </span><span class="pun">+</span><span class="pln"> screen</span><span class="pun">,</span><span class="pln"> </span><span class="typ">Toast</span><span class="pun">.</span><span class="pln">LENGTH_SHORT</span><span class="pun">).</span><span class="pln">show</span><span class="pun">();</span><span class="pln">  
</span><span class="pun">}</span><span class="pln">  

}

另外,还需要在AndroidManifest.xml添加下述内容:

权限: < uses-permission android:name="android.permission.CHANGE_CONFIGURATION" /> 在< activity标签中添加:android:configChanges="orientation" 将targetSdkVersion改为12以上的,12也可以


本节小结:

本节给大家讲解了:Configuration类以及onConfigurationChanged响应系统设置更改,有个大概了解即可 后续用到我们再继续深入~

上一篇:设备事件


下一篇:C# 窗体知识