System-set events for Android responses

Source: Internet
Author: User

1. The configuration class is used to describe the configuration information on mobile devices. The configuration information includes user-specific and system-specific dynamic device configurations.

The program can call the following method of activity to obtain the System Configuration object:

Configuration CFG = getresources (). getconfiguration ();

String screen = cfg. Orientation = configuration. orientation_landscape? "Horizontal screen": "vertical screen ";
String mnccode = cfg. MNC + ""; // gets the direction of the System screen. orientation_landscape is horizontal, orientation_portrait is vertical, and orientation_square is square;

String naviname = cfg. Orientation = configuration. navigation_nonav? "No Direction Control": cfg. Orientation = configuration. navigation_wheel? "Wheel control direction": cfg. Orientation
= Configuration. navigation_dpad
? "": ""; // You can specify the type of the System navigation device. Navigation_nonav: no navigation, navigation_dpad (DPAD navigation), navigation_trackball trackball navigation, and navigation_wheel wheel navigation;


String touchname = cfg. touchscreen = configuration. touchscreen_notouch? "No touch screen": cfg. touchscreen = configuration. touchscreen_stylus? "Touch pen touch screen": "Touch Screen for finger reception"; // gets the touch method for System touch shielding, touchscreen_notouch
Touch screen without touch screen touchscreen_stylus touch pen touch screen, touch screen with fingers;

String mnccode = cfg. MNC + ""; obtain the network code of the mobile signal

String mnccode = cfg. MCC + "" gets the country code of the mobile signal;


2. If the program needs to listen for system settings changes, you can consider rewriting the onconfigurationchanged method of the activity. This method is a callback-based event processing method: when the system settings are changed, this method is automatically triggered;

Next, we will explain it with a simple program:

The interface is very simple. A normal button is used to change the screen direction, so the interface file is not written;

Bind a listening event to the button:

Bn. setonclicklistener (New onclicklistener () {@ overridepublic void onclick (View Source) {configuration Config = getresources (). getconfiguration (); // if the current screen is landscape if (config. orientation = configuration. orientation_landscape) {// set it to landscape changecfg. this. setrequestedorientation (activityinfo. screen_orientation_portrait);} // if the current screen is landscape if (config. orientation = configuration. orientation_portrait) {// set it to horizontal screen changecfg. this. setrequestedorientation (activityinfo. screen_orientation_landscape );}}});

Override onconfigurationchanged method of activity: Public void onconfigurationchanged (configuration newconfig)
{
Super. onconfigurationchanged (newconfig );
String screen = newconfig. Orientation =
Configuration. orientation_landscape? "Horizontal screen": "vertical screen ";
Toast. maketext (this, "the system's screen direction has changed"
+ "\ N:" + screen"
, Toast. length_long)
. Show ();

}

Because the program needs to dynamically change the system settings, You need to grant the guest program the permission to change the system settings in the androidmanifest. xml file, and set the system settings Dynamically Modified by the activity to the screen direction;

<! -- Grant the application the permission to modify system settings -->
<Uses-Permission Android: Name = "android. Permission. change_configuration"/>

Related Article

Contact Us

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

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.