首页 > 移动平台 > 详细

android 获得屏幕状态

时间:2015-12-01 14:23:50      阅读:258      评论:0      收藏:0      [点我收藏+]
<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();
                }
            }
        });
    }
}

 

android 获得屏幕状态

原文:http://www.cnblogs.com/gisoracle/p/5009721.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!