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
 
posted @ 2014-09-19 18:10  q429786006  阅读(1899)  评论(0编辑  收藏  举报