A good memory is better than a bad one (a)

Source: Internet
Author: User

Get System Setup Status
We are able to get the state of some system settings through the Settings.system class, so we can get the state of the system by using the following code, for example Accelerometer_rotation
such as Just one thing to note is that there are too many Android ROM, different vendors may be directly rid of the Android implementation, and then implement a set of their own. And then. It's blown up.
So at the time of real processing. We need to catch exceptions to ensure the robustness of the code.

/*** 获取系统屏幕方向是否被锁定的状态* @return*/publicstaticbooleanisLockint lock = Settings.System.getInt(context.getContentResolver(), Settings.System.ACCELEROMETER_ROTATION, -1);return1;}returnfalse;}

Monitoring System Settings "lock direction" or "self-active rotation" button change
We all know that the content of contentobserver monitoring refers to some of the underlying databases provided by Android itself, such as text messages, telephones, and, of course, our settings,
So, through the above method. We can get to the state of the system settings, but how to change the state, but also the first time to know, then asked us to register a
It's observer. Which is the observer. Detailed code such as the following.
1. Create Observer

privatenew ContentObserver(new Handler()) {@OverridepublicvoidonChange(boolean selfChange) {      // 能够进入到这个函数,就说明相应的Content的内容已经发生变化了,也即设置项的值已经被改变了。     // 而 selfChange在这里表明是否当前App改变设置的值的}};

2, register Observer
The above simply creates a observer to the content. But the details need to listen to which content, this is the time we are in the register to decide.


In the previous blog post. Have written an article, is the text message Inbox to listen to. If you are interested, you can read that article. will be better informed about the use of this contentobserver.

Android Learning Little Demo (Android) about the use of Contentobserver

getContentResolver().registerContentObserver(Settings.System.getUriFor(Settings.System.ACCELEROMETER_ROTATION),true, mRotationLockObserver);

3. Undo Observer

getContentResolver().unregisterContentObserver(mRotationLockObserver);

Monitor Phone screen orientation Change
You can define a class by inheriting Orientationeventlistener, where the Onorientationchanged method is called when the screen orientation changes.


The value of the orientation is 0-359, indicating the angle at which the screen is rotated.

publicclass OrientationDetector extends OrientationEventListener {publicOrientationDetector(Context context) {super(context);}@OverridepublicvoidonOrientationChanged(int orientation) {if (orientation == OrientationEventListener.ORIENTATION_UNKNOWN) {}

A good memory is better than a bad one (a)

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.