0

I want to turn off device screen when incoming call made.

I tried :

Turn off screen on Android

How to turn screen off or send device to sleep

Android: How to turn screen on and off programmatically?

Android - Turn off display without triggering sleep/lock screen - Turn on with Touchscreen

How to distinguish the screen on/off status while incoming call?

Actually I don't have Window object to turn off in my incoming call receiver.

Here is my code :

public class MyCallReceiver extends BroadcastReceiver {

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

        if (intent.getStringExtra(TelephonyManager.EXTRA_STATE).equals(
                TelephonyManager.EXTRA_STATE_RINGING)) {
            // This code will execute when the phone has an incoming call

            // get the phone number
            String incomingNumber = intent
                    .getStringExtra(TelephonyManager.EXTRA_INCOMING_NUMBER);
            Toast.makeText(context, "Call from:" + incomingNumber,
                    Toast.LENGTH_LONG).show();

             // I tried code here


        } else if (intent.getStringExtra(TelephonyManager.EXTRA_STATE).equals(
                TelephonyManager.EXTRA_STATE_IDLE)
                || intent.getStringExtra(TelephonyManager.EXTRA_STATE).equals(
                        TelephonyManager.EXTRA_STATE_OFFHOOK)) {
            // This code will execute when the call is disconnected
            Toast.makeText(context, "Detected call hangup event",
                    Toast.LENGTH_LONG).show();

        }
    }
}

Can anybody help plz.

Community
  • 1
  • 1
DreamsNeverDie
  • 542
  • 2
  • 6
  • 14

1 Answers1

1

This is at least two part probelm, first you need to detect incoming calls then blank the screen, and later I think you will want to unblank it too.

First thing you will need to do is detect incoming phone call. So I would suggest you start there. Here is a blogpost that explains it.

And here is a link to Android Documentation for PhoneStateListener.

You could for example blank the screen by lowering the brightness as in this question. Example code:

WindowManager.LayoutParams layoutParam = getWindow().getAttributes();
oldBrightness = android.provider.Settings.System.getInt(getContentResolver(), android.provider.Settings.System.SCREEN_BRIGHTNESS)/255f;
layoutParam.screenBrightness = 0; 
layoutParam.flags |= WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON;
getWindow().setAttributes(layoutParam);
Community
  • 1
  • 1
nana
  • 4,426
  • 1
  • 34
  • 48