简体   繁体   中英

Android BroadcastReceiver NOT working with IntentFilter

The control is not Entering inside the --> receiver = (BroadcastReceiver) (context, intent) -> { ===== loop

I am trying to lock the screen(android) and its not entering the loop after creating BroadcastReceiver object. I am trying to lock the screen using the proximity sensor.

public class ScreenLockerService extends Service {

    protected static final String TAG = ScreenLockerService.class.getCanonicalName();
    BroadcastReceiver receiver;

    @Override
    public void onCreate() {
        super.onCreate();
        Log.d(TAG, "onCreate");
        ProximitySensor.getInstance(getApplicationContext());
        IntentFilter filter = new IntentFilter();
        filter.addAction(Intent.ACTION_SCREEN_ON);

        System.out.println("Screen is being locked");
//        ScreenLock.lockScreen(getApplicationContext());
  //      Vibration.vibrate(getApplicationContext());
receiver = (BroadcastReceiver) (context, intent) -> {
                System.out.println("Screen....");

                try {
                    ProximitySensor proximitySensor = ProximitySensor.getInstance(context);
                    if (proximitySensor.hasProximitySensorHardware() && proximitySensor.isClose()) {
                        Log.d(TAG, "lock");
                        if (CallStatus.isCallStateIdle(context)) {
                            ScreenLock.lockScreen(context);
                            Vibration.vibrate(context);
                        }
                    }
                } catch (Exception e) {
                    Log.d(TAG, "Exception occurred: " + e);
                }
            }
        };
        registerReceiver(receiver, filter);
    }

您是否已经注册了广播接收机?如果没有,请先注册,如果您只是想锁定您的设备,则可以使用设备策略管理器或密钥进行操作

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM