Set screentimeout in Android

Source: Internet
Author: User

I recently modified the lock screen time of Android and found that there are only interfaces for reading and writing databases in settings, but I didn't find out how these values actually work.

Finally, we can use contentobserver in phonewindow to listen to setting. DB.

I tested a small APK for verification and confirmed it was like this.

The attachment contains the specific source code (the suffix of the attachment must be changed to RAR), which is shown below:

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, "deliverselfications ications ");
Return super. deliverselfications ications ();
}

@ Override
Public void onchange (Boolean selfchange ){
Log. D (TAG, "onchange ");


}

Settingsobserver (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 = "set the system lock time"/>
<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 = "Settings"/>

<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" type = "codeph" text = "/codeph">

<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>

 

 

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.