-3

I want to turn screen off and disable UI when it close to user's face.I've done first part. but I have problem on second part. I must to know how to get current view and use my_view.setClickable(false); , However I developing this application with Qt and some java file attached to Qt
I've tried this but it's not working and I had an error: non-static method getCurrentFocus() cannot be referenced from a static context

   @Override
    public void onSensorChanged(SensorEvent event)
    {
         WindowManager.LayoutParams params = this.getWindow().getAttributes();
         if(event.sensor.getType()==Sensor.TYPE_PROXIMITY)
         {

          if(event.values[0]==0)
             {

                 CustomMainActivity.getCurrentFocus().setClickable(false);
                 params.flags |= LayoutParams.FLAG_KEEP_SCREEN_ON;
                 params.screenBrightness = 0;
                 getWindow().setAttributes(params);
             }
             else
             {
                 params.flags |= LayoutParams.FLAG_KEEP_SCREEN_ON;
                 params.screenBrightness = -1f;
                 getWindow().setAttributes(params);
             }
        }
    }
Farzan Najipour
  • 2,442
  • 7
  • 42
  • 81

2 Answers2

0

You should probably change this

CustomMainActivity.getCurrentFocus()
// Calling the method on the class

to this

CustomMainActivity.this.getCurrentFocus()
// Calling the method on an instance of the class

This answer explains the difference between a class and its instances, in case you want more details ;)

Community
  • 1
  • 1
2Dee
  • 8,609
  • 7
  • 42
  • 53
-1

Change onSensorChanged to :

    public void onSensorChanged(SensorEvent event)
    {
        WindowManager.LayoutParams params = this.getWindow().getAttributes();
     if(event.sensor.getType()==Sensor.TYPE_PROXIMITY)
     {

         if(event.values[0]==0)
         {

           params.flags |= LayoutParams.FLAG_KEEP_SCREEN_ON;
           params.screenBrightness = 0;
           getWindow().setAttributes(params);
           getWindow().setFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE,
           WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE);

         }
         else
         {
           params.flags |= LayoutParams.FLAG_KEEP_SCREEN_ON;
           params.screenBrightness = -1f;
           getWindow().setAttributes(params);
           getWindow().clearFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE);

         }
     }
    }
Farzan Najipour
  • 2,442
  • 7
  • 42
  • 81