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

Source: Internet
Author: User

Get System Setup Status
We can get the state of some system settings through the Settings.system class, so we can get the state of the system through the following code, such as Accelerometer_rotation
And so on, but one thing to note is that there are too many Android ROMs, and different vendors may be able to get rid of the Android implementation, and then implement one of their own, then, then, the collapse.
So in the real process, we need to catch the exception 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 "Auto rotate" 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
Observer, that is, the observer, the specific code is as follows.
1. Create Observer

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

2, registered Observer
The above just creates a observer to the content, but the specific need to listen to what, this is the time we register to decide.
In the previous blog post, has written an article, is the text message inbox to listen to, everyone is interested can look at that article, will better understand 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 customize a class by inheriting Orientationeventlistener, where the Onorientationchanged method is called when the screen orientation changes.
The value of the parameter orientation is 0-359, which indicates the angle at which the screen rotates.

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.