码迷,mamicode.com
首页 > 移动开发 > 详细

Android 锁屏监听

时间:2014-09-19 19:16:45      阅读:192      评论:0      收藏:0      [点我收藏+]

标签:des   android   blog   io   os   div   sp   cti   log   

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
 

Android 锁屏监听

标签:des   android   blog   io   os   div   sp   cti   log   

原文地址:http://www.cnblogs.com/yangqian/p/3982032.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!