上海广告展2018:【转】Android中系统设置参数改变监听(以时间同步为例)

来源:百度文库 编辑:中财网 时间:2024/05/09 09:59:57

Android中系统设置参数改变监听(以时间同步为例)

分类: Android 2011-10-12 10:28 210人阅读 评论(0) 收藏 举报

 Result:

Source code:

[java] view plaincopyprint?
  1. package com.inanwong.main;  
  2.   
  3. import java.text.SimpleDateFormat;  
  4. import java.util.Date;  
  5.   
  6. import android.app.Activity;  
  7. import android.database.ContentObserver;  
  8. import android.os.Bundle;  
  9. import android.os.Handler;  
  10. import android.provider.Settings;  
  11. import android.util.Log;  
  12. import android.view.View;  
  13. import android.view.View.OnClickListener;  
  14. import android.widget.Button;  
  15. import android.widget.TextView;  
  16.   
  17. public class MainActivity extends Activity {  
  18.   
  19.     private static final String TAG = "MainActivity";  
  20.   
  21.     private static final int ON = 1;  
  22.     private static final int OFF = 0;  
  23.   
  24.     private Button mbtnGetStatus;  
  25.     private Button mbtnAutoTime;  
  26.     private Button mbtnAutoTimeListen;  
  27.     private TextView mtvInfo;  
  28.   
  29.     /** 
  30.      * 是否开启时间同步监听 
  31.      */  
  32.     private boolean isListening = false;  
  33.   
  34.     /** Called when the activity is first created. */  
  35.     @Override  
  36.     public void onCreate(Bundle savedInstanceState) {  
  37.         super.onCreate(savedInstanceState);  
  38.         setContentView(R.layout.main);  
  39.   
  40.         mtvInfo = (TextView) findViewById(R.id.multiAutoCompleteTextView1);  
  41.         mtvInfo.setText("");  
  42.   
  43.         mbtnGetStatus = (Button) findViewById(R.id.button1);  
  44.         mbtnGetStatus.setOnClickListener(new OnClickListener() {  
  45.   
  46.             @Override  
  47.             public void onClick(View v) {  
  48.                 // TODO Auto-generated method stub  
  49.                 int nAutoTimeStatus = Settings.System.getInt(  
  50.                         getContentResolver(), Settings.System.AUTO_TIME, OFF);  
  51.                 OutPrintLog("当前时间同步状态:" + nAutoTimeStatus);  
  52.             }  
  53.         });  
  54.   
  55.         mbtnAutoTime = (Button) findViewById(R.id.button2);  
  56.         mbtnAutoTime.setOnClickListener(new OnClickListener() {  
  57.   
  58.             @Override  
  59.             public void onClick(View v) {  
  60.                 // TODO Auto-generated method stub  
  61.   
  62.                 int nAutoTimeStatus = Settings.System.getInt(  
  63.                         getContentResolver(), Settings.System.AUTO_TIME, OFF);  
  64.   
  65.                 Settings.System.putInt(getContentResolver(),  
  66.                         Settings.System.AUTO_TIME, nAutoTimeStatus == OFF ? ON  
  67.                                 : OFF);  
  68.   
  69.                 Refresh();  
  70.             }  
  71.         });  
  72.   
  73.         mbtnAutoTimeListen = (Button) findViewById(R.id.button3);  
  74.         mbtnAutoTimeListen.setOnClickListener(new OnClickListener() {  
  75.             private ContentObserver mAutoTimeObserver = new ContentObserver(  
  76.                     new Handler()) {  
  77.                 @Override  
  78.                 public void onChange(boolean selfChange) {  
  79.                     Log.i(TAG, "Auto time state changed");  
  80.                     int nAutoTimeStatus = Settings.System.getInt(  
  81.                             getContentResolver(), Settings.System.AUTO_TIME,  
  82.                             OFF);  
  83.                     OutPrintLog("当前时间同步状态:" + nAutoTimeStatus);  
  84.                 }  
  85.             };  
  86.   
  87.             @Override  
  88.             public void onClick(View v) {  
  89.                 // TODO Auto-generated method stub  
  90.                 if (!isListening) {  
  91.                     getApplication()  
  92.                             .getContentResolver()  
  93.                             .registerContentObserver(  
  94.                                     Settings.System  
  95.                                             .getUriFor(Settings.System.AUTO_TIME),  
  96.                                     true, mAutoTimeObserver);  
  97.                 } else {  
  98.                     getApplicationContext().getContentResolver()  
  99.                             .unregisterContentObserver(this.mAutoTimeObserver);  
  100.   
  101.                 }  
  102.                 isListening = !isListening;  
  103.                 Refresh();  
  104.             }  
  105.         });  
  106.   
  107.         Refresh();  
  108.   
  109.     }  
  110.   
  111.     private void Refresh() {  
  112.   
  113.         int nAutoTimeStatus = Settings.System.getInt(getContentResolver(),  
  114.                 Settings.System.AUTO_TIME, OFF);  
  115.   
  116.         mbtnAutoTime  
  117.                 .setText(nAutoTimeStatus == OFF ? R.string.app_autotime_start  
  118.                         : R.string.app_autotime_end);  
  119.   
  120.         mbtnAutoTimeListen  
  121.                 .setText(isListening ? R.string.app_autotimelisten_end  
  122.                         : R.string.app_autotimelisten_start);  
  123.   
  124.     }  
  125.   
  126.     private void OutPrintLog(CharSequence text) {  
  127.   
  128.         mtvInfo.setText((mtvInfo.getText()).toString()  
  129.                 + new SimpleDateFormat("\n mm:ss").format(new Date())  
  130.                 + text.toString());  
  131.     }  
  132. }