android裡設定screentimeout

來源:互聯網
上載者:User

最近在修改android的鎖屏時間,發現在settings裡只有讀寫資料庫的介面,卻沒發現這些值究竟是怎麼起作用的

最後全域搜尋,才發現會在phonewindow裡用ContentObserver來對setting.db做監聽

測試了個小APK來驗證,證實是這樣的

附件中為具體源碼(附件尾碼要修改rar),下面貼出來如下:

ContentobserveActivity.java

 

package com.android;
import android.app.Activity;
import android.content.ContentResolver;
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.EditText;

public class ContentobserveActivity extends Activity implements OnClickListener {
 
 String TAG="sano";
 
 Handler mHandler;
 
 SettingsObserver Observer;
 
 private EditText eSetting;
 
 private EditText eShow;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
       
        init_ui();
       
        mHandler = new Handler();
       
        Observer =new SettingsObserver(mHandler);
        Observer.registerobserves();
       
    }
   
    public void init_ui(){
     eSetting = (EditText) findViewById(R.id.editText1);
     eShow = (EditText) findViewById(R.id.editText2);
     
     findViewById(R.id.button1).setOnClickListener(this);
    }
   
   
    class SettingsObserver extends ContentObserver {

  @Override
  public boolean deliverSelfNotifications() {
   
   Log.d(TAG,"deliverSelfNotifications");
   return super.deliverSelfNotifications();
  }

  @Override
  public void onChange(boolean selfChange) {
   Log.d(TAG,"onChange");
   
   
  }

  SettingsObserver(Handler handler) {
            super(handler);
        }

  
  public void registerobserves(){
   Log.d(TAG,"registerobserves");
   
       ContentResolver resolver = getContentResolver();
          resolver.registerContentObserver(Settings.System.getUriFor(Settings.System.SCREEN_OFF_TIMEOUT), false, this);
  }
     
    }

 @Override
 public void onClick(View v) {
  
  Log.d(TAG,"write "+eSetting.getText().toString());
        Settings.System.putInt(getContentResolver(),Settings.System.SCREEN_OFF_TIMEOUT, Integer.parseInt(eSetting.getText().toString()));
 }
}

 

 

main.xml

 

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:baselineAligned="false"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="設定系統鎖屏時間" />
    <EditText
        android:id="@+id/editText1"
        android:layout_width="244dp"
        android:layout_height="wrap_content" >
    </EditText>

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="設定" />

    <EditText
        android:id="@+id/editText2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

</LinearLayout>

 

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.android"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk android:minSdkVersion="9" />
  
    <uses-permission android:name="android.permission.WRITE_SETTINGS" />
  <uses-permission android:name="android.permission.WRITE_SECURE_SETTINGS" />
    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <activity
            android:label="@string/app_name"
            android:name=".ContentobserveActivity" >
            <intent-filter >
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

 

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.