锁屏解锁
Posted 码上加油站
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了锁屏解锁相关的知识,希望对你有一定的参考价值。
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("解锁了"); } } }
以上是关于锁屏解锁的主要内容,如果未能解决你的问题,请参考以下文章