• 安卓开发之动态注册广播(锁屏和解锁案例)


    package com.lidaochen.test001;
    
    import android.content.Intent;
    import android.content.IntentFilter;
    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    import android.view.View;
    
    public class MainActivity extends AppCompatActivity {
        private ScreenReceiver screenReceiver;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            // 动态注册广播接收者
            screenReceiver = new ScreenReceiver();
            // 创建IntentFilter对象
            IntentFilter filter = new IntentFilter();
            // 添加要注册的action
            filter.addAction("android.intent.action.SCREEN_OFF");
            filter.addAction("android.intent.action.SCREEN_ON");
            // 动态注册广播接收者
            registerReceiver(screenReceiver, filter);
        }
    
    }
    package com.lidaochen.test001;
    
    import android.content.BroadcastReceiver;
    import android.content.Context;
    import android.content.Intent;
    import android.util.Log;
    
    public class ScreenReceiver extends BroadcastReceiver {
    
        // 当我们进行屏幕锁屏和解锁时,执行下面的方法
        @Override
        public void onReceive(Context context, Intent intent) {
            // 获取当前广播的事件类型
            String action = intent.getAction();
            if("android.intent.action.SCREEN_OFF".equals(action))
            {
                System.out.println("锁屏!");
                Log.e("screen","锁屏");
            }
            else if("android.intent.action.SCREEN_ON".equals(action))
            {
                System.out.println("解锁!");
                Log.e("screen","解锁");
            }
        }
    }
  • 相关阅读:
    ios 学习小笔记
    object c 2.0 @property属性
    uiPickerView 滚动盘
    ios 文件操作
    ios多窗体项目
    Linux命令
    Mina的使用
    设计模式学习总结访问者模式(Visitor Method)
    socket, nio socket 及nio socket框架MINA总结
    UML类图与类的关系详解UML一波流系列(转转)
  • 原文地址:https://www.cnblogs.com/duxie/p/11029196.html
Copyright © 2020-2023  润新知