Activity handle when screen unlocked

浪子不回头ぞ 提交于 2019-11-29 04:37:13

for detect screen on and screen off register a broadcast reciver like:

AndroidManifest.xml:

    <receiver android:name="receiverScreen">
        <intent-filter> 
            <action android:name="android.intent.action.SCREEN_ON" />
            <action android:name="android.intent.action.SCREEN_OFF" />
<action android:name="android.Intent.ACTION_USER_PRESENT" />
        </intent-filter> 
    </receiver>

In Activity or Service:

    try {
              IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_ON);

              filter.addAction(Intent.ACTION_SCREEN_OFF);
filter.addAction(Intent.ACTION_USER_PRESENT);

              BroadcastReceiver mReceiver = new receiverScreen();

              registerReceiver(mReceiver, filter);
         } catch (Exception e) {

         }

receiver code where System inform you if Screen on/off happen:

 public class receiverScreen extends BroadcastReceiver {

     @Override
     public void onReceive(Context context, Intent intent) {

         if (intent.getAction().equals(Intent.ACTION_SCREEN_ON)){

         }
        if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)){

         }
if (intent.getAction().equals(Intent.ACTION_USER_PRESENT)){

         }
     }

    }
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!