Listener for parameter change settings in Android (taking time synchronization as an example)

Source: Internet
Author: User
Tags change settings

 

Import Java. text. simpledateformat; import Java. util. date; import android. app. activity; import android. database. contentobserver; import android. OS. bundle; import android. OS. handler; import android. provider. settings; import android. util. log; import android. view. view; import android. view. view. onclicklistener; import android. widget. button; import android. widget. textview; public class mainactivity extends Activity {Private Static final string tag = "mainactivity"; Private Static final int on = 1; Private Static final int off = 0; private button mbtngetstatus; private button mbtnautotime; private button mbtnautotimelisten; private textview mtvinfo;/*** whether to enable time synchronization listener */private Boolean islistening = false;/** called when the activity is first created. * // @ override public void oncreate (bundle savedinstancesta Te) {super. oncreate (savedinstancestate); setcontentview (R. layout. main); mtvinfo = (textview) findviewbyid (R. id. multiautocompletetextview1); mtvinfo. settext (""); mbtngetstatus = (button) findviewbyid (R. id. button1); mbtngetstatus. setonclicklistener (New onclicklistener () {@ override public void onclick (view v) {// todo auto-generated method stub int nautotimestatus = settings. system. getint (getcont Entresolver (), settings. system. auto_time, off); outprintlog ("current time sync status:" + nautotimestatus) ;}}; mbtnautotime = (button) findviewbyid (R. id. button2); mbtnautotime. setonclicklistener (New onclicklistener () {@ override public void onclick (view v) {// todo auto-generated method stub int nautotimestatus = settings. system. getint (getcontentresolver (), settings. system. auto_time, off); settings. system. pu Tint (getcontentresolver (), settings. system. auto_time, nautotimestatus = off? On: Off); refresh () ;}}); mbtnautotimelisten = (button) findviewbyid (R. id. button3); mbtnautotimelisten. setonclicklistener (New onclicklistener () {private contentobserver mautotimeobserver = new contentobserver (new handler () {@ override public void onchange (Boolean selfchange) {log. I (TAG, "auto time state changed"); int nautotimestatus = settings. system. getint (getcontentresolver (), settings. s Ystem. auto_time, off); outprintlog ("current time sync status:" + nautotimestatus) ;};@ override public void onclick (view V) {// todo auto-generated method stub if (! Islistening) {getapplication (). getcontentresolver (). registercontentobserver (settings. system. geturifor (settings. system. auto_time), true, mautotimeobserver);} else {getapplicationcontext (). getcontentresolver (). unregistercontentobserver (this. mautotimeobserver);} islistening =! Islistening; refresh () ;}}); refresh () ;}private void refresh () {int nautotimestatus = settings. system. getint (getcontentresolver (), settings. system. auto_time, off); mbtnautotime. settext (nautotimestatus = off? R. String. app_autotime_start: R. String. app_autotime_end); mbtnautotimelisten. settext (islistening? R. string. app_autotimelisten_end: R. string. app_autotimelisten_start);} private void outprintlog (charsequence text) {mtvinfo. settext (mtvinfo. gettext ()). tostring () + new simpledateformat ("\ n mm: SS "). format (new date () + text. tostring ());}

 

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.