锁屏解锁

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("解锁了");
      }
   }
}

 

posted on 2017-02-19 16:14  LoaderMan  阅读(259)  评论(0编辑  收藏  举报

导航