Android gets the screen status, android gets the screen
<RelativeLayout xmlns: android = "http://schemas.android.com/apk/res/android" xmlns: tools = "http://schemas.android.com/tools" android: layout_width = "match_parent" android: layout_height = "match_parent" android: orientation = "vertical"> <! -- Define the Button for obtaining the screen status --> <Button android: id = "@ + id/reenableKeyguard" android: layout_width = "match_parent" android: layout_height = "wrap_content" android: text = "screen status"/> </RelativeLayout>
Package com. example. yanlei. yl; import android. app. keyguardManager; import android. content. context; import android. OS. bundle; import android. support. v7.app. appCompatActivity; import android. view. view; import android. view. view. onClickListener; import android. widget. button; import android. widget. toast; public class MainActivity extends AppCompatActivity {// define the lock screen Button private Button btnKeyguard; // declare the KeyguardManager object private KeyguardManager keyguardManager; @ Override public void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); // obtain the KeyguardManager service keyguardManager = (KeyguardManager) getSystemService (Context. KEYGUARD_SERVICE); // obtain all objects in the layout. findView (); // set the object listener setListener ();} private void findView () {// get all objects in the layout btnKeyguard = (Button) findViewById (R. id. reenableKeyguard);} private void setListener () {// sets the object listener btnKeyguard. setOnClickListener (new OnClickListener () {@ Override public void onClick (View arg0) {// TODO Auto-generated method stub // determine the status of the current screen if (keyguardManager. isKeyguardLocked () {Toast. makeText (MainActivity. this, "lock screen", Toast. LENGTH_SHORT ). show ();} else {Toast. makeText (MainActivity. this, "no screen lock", Toast. LENGTH_SHORT ). show ();}}});}}