Uses ContentObserver to dynamically cancel or add screen timeout tasks.

Source: Internet
Author: User

Uses ContentObserver to dynamically cancel or add screen timeout tasks.

As mentioned above, ContentObserver can monitor the changes of a data item in the database, and of course can also monitor the changes of multiple data items at the same time. I need to modify the screen timeout requirements in the project. For example, in the on-board business, when a reversing event occurs, the screen timeout is not required to become black, which is equivalent to a Reset of the timer, we also need to Reset the Bluetooth phone. It is best that we do not run this screen timeout timing task during this special task. How can we achieve this?

By studying phonewindowsmanger. cpp, I found that all of them ultimately drive an mScreenLockTimeout. How does it come from? See the following code:

    ScreenLockTimeout mScreenLockTimeout = new ScreenLockTimeout();    class ScreenLockTimeout implements Runnable {        Bundle options;        @Override        public void run() {            synchronized (this) {                if (localLOGV) Log.v(TAG, "mScreenLockTimeout activating keyguard");                if (mKeyguardDelegate != null) {                    mKeyguardDelegate.doKeyguardTimeout(options);                }                mLockScreenTimerActive = false;                options = null;            }        }        public void setLockOptions(Bundle options) {            this.options = options;        }    }
Runnable is the focus!
In the final analysis, it is still a runnable driver. How can we control it? We can see the source code in it, and there are also a lot of operations on this mScreenLockTimeout, which can be dynamically removed and then started dynamically. The author reminds me of using contentobserver for inter-process communication. For example, writing a value indicates canceling the task and adding the task when writing another value. The author's rough code is as follows:

    private final class CalcScnTimeoutObserver extends ContentObserver {        public CalcScnTimeoutObserver(Handler handler) {            super(handler);        }        @Override        public void onChange(boolean selfChange) {            ContentResolver resolver = mContext.getContentResolver();mCalcScnTimeoutValue = Settings.System.getIntForUser(resolver,Settings.System.SCREEN_TIMEOUT_CALC_INFO, 0,UserHandle.USER_CURRENT);Slog.d(TAG,"##CalcScnTimeoutObserver: " + mCalcScnTimeoutValue);            if(1 == mCalcScnTimeoutValue){synchronized (mScreenLockTimeout) {if (mLockScreenTimerActive) {// reset the timermHandler.removeCallbacks(mScreenLockTimeout);//mHandler.postDelayed(mScreenLockTimeout, mLockScreenTimeout);mLockScreenTimerActive = false;}}            } else if(2 == mCalcScnTimeoutValue){synchronized (mScreenLockTimeout) {if (mLockScreenTimerActive) {// reset the timermHandler.removeCallbacks(mScreenLockTimeout);mHandler.postDelayed(mScreenLockTimeout, mLockScreenTimeout);} else {mHandler.postDelayed(mScreenLockTimeout, mLockScreenTimeout);mLockScreenTimerActive = true;}}            }else {Slog.e(TAG,"default novalid value ");            }        }        void observe() {            // Observe all users' changes            ContentResolver resolver = mContext.getContentResolver();            resolver.registerContentObserver(Settings.System.getUriFor(                    Settings.System.SCREEN_TIMEOUT_CALC_INFO), false, this,                    UserHandle.USER_ALL);        }    }
As a result, it is very simple and practical to dynamically control the task. There are still a lot of essential code and processing methods in the source code. The key is to be familiar with, understand, master, and use it flexibly! The big android, the control is the beauty, the control is the magic! Continue to work!




ContentObserver in Android

Uri. parse ("content: // sms/inbox ")

At first glance, there should be no such thing as inbox in android. Sms follows the ID in contract.

How does android solve the problem of repeatedly listening to ContentObserver?

You can directly determine the text message date. Only the latest one can be deleted.
 

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.