android中的来电时关闭设备屏幕

时间:2014-03-28 17:49:34

标签: android

我想在拨打电话时关闭设备屏幕。

我试过了:

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?

实际上我没有在我的来电接收器中关闭Window对象。

这是我的代码:

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();

        }
    }
}

任何人都可以帮助PLZ。

1 个答案:

答案 0 :(得分:1)

这至少是两部分问题,首先你需要检测来电然后使屏幕空白,后来我想你也想要取消它。

您需要做的第一件事就是检测来电。所以我建议你从那里开始。 Here is a blogpost解释了它。

以下是PhoneStateListener的Android文档链接。

您可以通过降低this问题中的亮度来消隐屏幕。示例代码:

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);