<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" > <!-- 定義得到屏幕狀態的按鈕 --> <Button android:id="@+id/reenableKeyguard" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="屏幕的狀態" /> </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 { // 定義鎖屏的按鈕 private Button btnKeyguard; //聲明KeyguardManager對象 private KeyguardManager keyguardManager; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // 得到KeyguardManager服務 keyguardManager=(KeyguardManager)getSystemService(Context.KEYGUARD_SERVICE); //獲得佈局中的全部對象 findView(); //設置對象的監聽器 setListener(); } private void findView() { // 獲得佈局中的全部對象 btnKeyguard = (Button) findViewById(R.id.reenableKeyguard); } private void setListener() { // 設置對象的監聽器 btnKeyguard.setOnClickListener(new OnClickListener() { @Override public void onClick(View arg0) { // TODO Auto-generated method stub //判斷當前屏幕的狀態 if(keyguardManager.isKeyguardLocked()) { Toast.makeText(MainActivity.this, "鎖屏", Toast.LENGTH_SHORT).show(); } else { Toast.makeText(MainActivity.this, "沒有鎖屏", Toast.LENGTH_SHORT).show(); } } }); } }
以上,另外對APP進行在線全方位的安全性、兼容性測試,我都會用這個:www.ineice.com。java