锁屏解锁
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("解锁了"); } } }
最后,关注【码上加油站】微信公众号后,有疑惑有问题想加油的小伙伴可以码上加入社群,让我们一起码上加油吧!!!