Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7410597
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T06:15:27+00:00 2026-05-29T06:15:27+00:00

Hy, I have a problem related to the proximity sensor. When I put the

  • 0

Hy,

I have a problem related to the proximity sensor. When I put the finger on it, I want to turn the screen off and when I take the finger, I want to turn the screen on.
I successfully did the turning off part, but when I take the finger off the sensor, it does not seem to execute the onSensorChanged method. Here is the code for it:

public void onSensorChanged(SensorEvent event) {
        float distance = event.values[0];
        boolean active = (distance >= 0.0 && distance < PROXIMITY_THRESHOLD && distance < event.sensor.getMaximumRange());
        boolean isValidCallState = false;

        if (callsInfo != null) {
            for (SipCallSession callInfo : callsInfo) {
                int state = callInfo.getCallState();
                isValidCallState |= ((state == SipCallSession.CallState.CONFIRMED)
                        || (state == SipCallSession.CallState.CONNECTING)
                        || (state == SipCallSession.CallState.CALLING) || (state == SipCallSession.CallState.EARLY && !callInfo
                        .isIncoming()));
            }
        }
        if (isValidCallState && active) {
            Log.e("", "turn off");
            WindowManager.LayoutParams lp = getWindow().getAttributes();
            lp.buttonBrightness = WindowManager.LayoutParams.BRIGHTNESS_OVERRIDE_OFF;
            lp.screenBrightness = WindowManager.LayoutParams.BRIGHTNESS_OVERRIDE_OFF;

            getWindow().setAttributes(lp);
            lockOverlay.show();
        } else {
            Log.e("", "turn on");
            WindowManager.LayoutParams lp = getWindow().getAttributes();

            lp.buttonBrightness = WindowManager.LayoutParams.BRIGHTNESS_OVERRIDE_NONE;
            lp.screenBrightness = WindowManager.LayoutParams.BRIGHTNESS_OVERRIDE_NONE;
            getWindow().setAttributes(lp);
        }
        Log.e("", "turn ???"); // --> does not print this when releasing the sensor
    }

Any ideas? Thanks.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T06:15:28+00:00Added an answer on May 29, 2026 at 6:15 am

    I found the answer to my question and managed to figure out the main flow of the proximity sensor.

    First, there are 2 ways to handle the proximity sensor:
    1. Make a lock on PROXIMITY_SCREEN_OFF_WAKE_LOCK. From what I read this is somehow a hidden constant used by android, that has the value 32(at least for the moment – might or might not be changed in future versions). If this lock succeeds, the proximity sensor, will behave as you are in a call.

    try {
                        Method method = powerManager.getClass().getDeclaredMethod(
                                "getSupportedWakeLockFlags");
                        int supportedFlags = (Integer) method.invoke(powerManager);
                        Field f = PowerManager.class.getDeclaredField("PROXIMITY_SCREEN_OFF_WAKE_LOCK");
                        int proximityScreenOffWakeLock = (Integer) f.get(null);
                        if ((supportedFlags & proximityScreenOffWakeLock) != 0x0) {
                            proximityWakeLock = powerManager.newWakeLock(proximityScreenOffWakeLock,
                                    "com.voalte.CallProximity");
                            proximityWakeLock.setReferenceCounted(false);
                        }
    
                    } catch (Exception e) {
                    }
    
    1. Register a listener for the Sensor. In this case the proximity sensor actions must be handled in the onSensorChanged method(screenBrigthness can be set).

    Note:

    1. When the screen is turned off, the onPause is called.
    2. If u use the first method, u should consider to have also a back-up in case the PROXIMITY_SCREEN_OFF_WAKE_LOCK cannot be set. So I recommend to try and instantiate the lock in onStart and in onResume to check it against null. If not null, aquire the lock, otherwise register listener for the sensor.
    3. In the onSensorChanged I set the screenBrightness to BRIGHTNESS_OVERRIDE_OFF, when covering the sensor and when relesing I set the screenBrightness to BRIGHTNESS_OVERRIDE_FULL and/or BRIGHTNESS_OVERRIDE_NONE, but it didn’t turn the screen on. So a workaround was to make a black layout that is set invisible by default, to be visible and occupy the full screen.

    Hope this is of help for somebody.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a problem related to graph. I am not a computer science grad
I have a problem related to the subset sum problem and am wondering if
I have a performance problem related to string comparison (in Java). I'm working on
I have a problem which I think is related to forward declarations, but perhaps
I have a serious performance problem. I have a database with (related to this
This is basically a math problem, but very programing related: if I have 1
I have problem with return statment >.< I want to store all magazine names
I have a problem related to Memory Leak. In my app I have to
I have a problem related to pdf drawing in iPhone. When I draw the
I have the problem related to showing the button on the page. There are

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.