码迷,mamicode.com
首页 > 其他好文 > 详细

锁屏解锁

时间:2017-02-19 16:48:27      阅读:170      评论:0      收藏:0      [点我收藏+]

标签:过滤器   int   oncreate   java   etc   logs   注册   and   oid   

package com.example.lock_screen;
import android.app.Activity;
import android.content.IntentFilter;
import android.os.Bundle;
public class MainActivity extends Activity {
  private LockScreenReceiver  lockScreenReceiver;
   @Override
   protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
      //代码中注册
      //节约系统开销
      //创建意图过滤器
      IntentFilter filter = new IntentFilter();
      filter.addAction("android.intent.action.SCREEN_OFF");
      filter.addAction("android.intent.action.SCREEN_ON");
      lockScreenReceiver = new LockScreenReceiver();
      //注册锁屏解锁广播接收者
      registerReceiver(lockScreenReceiver, filter);
   }
   @Override
   protected void onDestroy() {
      super.onDestroy();
      //activity销毁前注销广播
      unregisterReceiver(lockScreenReceiver);
   }
}
package com.example.lockphone;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
public class LockPhoneReceiver extends BroadcastReceiver{
   @Override
   public void onReceive(Context context, Intent intent) {
      if("android.intent.action.SCREEN_OFF".equals(intent.getAction())){
         System.out.println("锁屏了");
      }else if("android.intent.action.SCREEN_ON".equals(intent.getAction())){
         System.out.println("解锁了");
      }
   }
}

 

锁屏解锁

标签:过滤器   int   oncreate   java   etc   logs   注册   and   oid   

原文地址:http://www.cnblogs.com/loaderman/p/6416081.html

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