Android 锁屏监听

package com.example.lockscreenlistenerdemo;

public interface LockScreenListener {
    String Tag="LockScreenListener";
    public void onScreenOn();
    public void onScreenOff();
    public void onUserPresent();
}

做一个监听接口。

监听广播,监听锁屏 解锁  点亮屏幕的Action

package com.example.lockscreenlistenerdemo;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;

public class LockScreenBroadcastReceiver extends BroadcastReceiver {
    LockScreenListener lockScreenListener;

    public LockScreenBroadcastReceiver(LockScreenListener lockScreenListener) {
        super();
        this.lockScreenListener = lockScreenListener;
    }

    @Override
    public void onReceive(Context content, Intent intent) {
        // TODO Auto-generated method stub
        String action=intent.getAction();
        if(action.equals(Intent.ACTION_SCREEN_ON)){
            lockScreenListener.onScreenOn();
        }else if(action.equals(Intent.ACTION_SCREEN_OFF)){
            lockScreenListener.onScreenOff();
        }else if(action.equals(Intent.ACTION_USER_PRESENT)){
            lockScreenListener.onUserPresent();
        }
    }
}

接口的实现

package com.example.lockscreenlistenerdemo;

import android.app.Activity;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.util.Log;

public class MainActivity extends Activity {
    LockScreenBroadcastReceiver lockScreenBroadcastReceiver;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        lockScreenBroadcastReceiver = new LockScreenBroadcastReceiver(
                new LockScreenListener() {

                    @Override
                    public void onUserPresent() {
                        // TODO Auto-generated method stub
                        Log.i(Tag,"onUserPresent");
                    }

                    @Override
                    public void onScreenOn() {
                        // TODO Auto-generated method stub
                        Log.i(Tag,"onScreenOn");
                    }

                    @Override
                    public void onScreenOff() {
                        // TODO Auto-generated method stub
                        Log.i(Tag,"onScreenOff");
                    }
                });
        IntentFilter intentFilter=new IntentFilter();
        intentFilter.addAction(Intent.ACTION_SCREEN_OFF);
        intentFilter.addAction(Intent.ACTION_SCREEN_ON);
        intentFilter.addAction(Intent.ACTION_USER_PRESENT);
        registerReceiver(lockScreenBroadcastReceiver, intentFilter);
    }
    @Override
    protected void onDestroy() {
        // TODO Auto-generated method stub
        unregisterReceiver(lockScreenBroadcastReceiver);
        super.onDestroy();
    }
}

效果

01-04 08:29:23.432: I/LockScreenListener(12615): onScreenOff
01-04 08:29:24.062: I/LockScreenListener(12615): onScreenOn
01-04 08:29:25.612: I/LockScreenListener(12615): onUserPresent
01-04 08:29:35.512: I/LockScreenListener(12615): onScreenOff
时间: 2024-10-11 01:10:47

Android 锁屏监听的相关文章

android基础之如何实现触屏监听和鼠标点击事件

直接上代码: android基础之如何实现触屏监听和鼠标点击事件

android锁屏软件制作

转载请标明出处http://blog.csdn.net/mingyue_1128 一.首先要创建一个Service保证Service一直开启,同时不被一键清理掉. 二.屏蔽掉系统的锁屏 三.创建锁屏的Activity,在Activity中屏蔽掉back,menu,home键 四.保证自己的进程不在最近任务中出现 一.创建一个Service名字为LockService public class LockService extends Service{ private KeyguardManager

Android的Button监听

1.android简单按钮监听----单个监听 start = (Button)findViewById(R.id.btnStart); start.setOnClickListener(new OnClickListener(){            @Override            public void onClick(View v) {                //---do something            }            }); 2.View.OnC

quick-cocos2d-x android返回键监听并实现原生退出对话框

这两天终于闲了一下,就顺手又把quick捡起来又学了学,一直都觉得quick比cocos2dx那套lua绑定要方便许多,今天试了下android返回键的监听,还是挺好弄的,所以就有了这篇. 首先说明一下使用的quick版本--2.2.5. 直接上代码 function MainScene:addBackEvent() if device.platform == "android" then self.touchLayer = display.newLayer() self.touchL

Android短信监听(三)——利用Loader实现短信监听

MainActivity如下: package cc.c; import android.net.Uri; import android.os.Bundle; import android.app.Activity; import android.app.LoaderManager.LoaderCallbacks; import android.content.Context; import android.content.Loader; import android.database.Curs

Android短信监听(二)——利用ContentObserver实现短信监听

MainActivity如下: package cc.testsmslistener; import cc.testsmslistener.SMSContentObserver.MessageListener; import android.net.Uri; import android.os.Bundle; import android.os.Handler; import android.widget.TextView; import android.app.Activity; /** *

Android短信监听(一)——利用BroadcastReceiver实现短信监听

MainActivity如下: package cc.testsmsbroadcastreceiver; import cc.testsmsbroadcastreceiver.SMSBroadcastReceiver.MessageListener; import android.os.Bundle; import android.widget.TextView; import android.app.Activity; /** * Demo描述: * 利用BroadcastReceiver实现

android应用程序监听SMS Intent广播

当设备接收到一条新的SMS消息时,就会广播一个包含了android.provider.Telephony.SMS_RECEIVED动作的Intent. 对于应用程序监听SMS Intent广播,首先需要添加RECEIVE_SMS权限.通过在应用程序manifest中添加一个uses-permission,如下面的片段所示: <uses-permission android:name="android.permission.RECEIVE_SMS"/> AndroidMani

jQuery仿Android锁屏图案应用插件

<!doctype html> <html> <head> <meta charset="utf-8"> <title>jQuery仿Android锁屏图案应用插件DEMO演示</title> <link href="css/patternLock.css" rel="stylesheet" type="text/css" /> <scr